portalniy-dev commited on
Commit
600bbe2
·
verified ·
1 Parent(s): 98b407d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -75
app.py CHANGED
@@ -1,85 +1,78 @@
1
  import os
2
- import logging
3
  import subprocess
4
- from tempfile import mkstemp
5
- from telegram import Update
6
- from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
7
 
8
- # Настройки
9
  TOKEN = "7709328099:AAHvvz2Dqkzb2c0lNh9OViTBnUoW6ZcpreA"
10
  ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov', 'mkv'}
11
  TEMP_DIR = "temp_files"
12
 
13
- # Настройка логирования
14
- logging.basicConfig(
15
- format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
16
- level=logging.INFO
17
- )
18
 
19
- async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
20
- await update.message.reply_text(
21
- "Привет! Отправь мне видео, и я интерполирую кадры до нужного FPS. "
22
- "Используй команду /help для справки."
23
- )
24
 
25
- async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
26
- await update.message.reply_text(
27
- "Как использовать бота:\n"
28
- "1. Отправь мне видео файл\n"
 
 
29
  "2. Укажи желаемый FPS (целое число)\n"
30
- "3. Жди результат обработки\n\n"
31
- "Ограничения: максимальный размер файла 20MB"
32
  )
33
 
34
- async def handle_video(update: Update, context: ContextTypes.DEFAULT_TYPE):
35
- if not update.message.video:
36
- await update.message.reply_text("Пожалуйста, отправьте видео файл")
37
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- # Создаем временную директорию
40
- os.makedirs(TEMP_DIR, exist_ok=True)
41
-
42
- # Сохраняем файл
43
- video_file = await update.message.video.get_file()
44
- ext = video_file.file_path.split('.')[-1] if video_file.file_path else 'mp4'
45
- input_path = os.path.join(TEMP_DIR, f"input_{update.update_id}.{ext}")
46
- await video_file.download_to_drive(input_path)
47
-
48
- context.user_data['input_path'] = input_path
49
- await update.message.reply_text("Видео получено. Теперь введите желаемый FPS:")
50
 
51
- async def handle_fps(update: Update, context: ContextTypes.DEFAULT_TYPE):
52
  try:
53
- target_fps = int(update.message.text)
 
 
54
  if target_fps <= 0:
55
- raise ValueError
56
- except ValueError:
57
- await update.message.reply_text("Пожалуйста, введите корректное целое число FPS")
58
- return
 
59
 
60
- input_path = context.user_data.get('input_path')
61
- if not input_path or not os.path.exists(input_path):
62
- await update.message.reply_text("Ошибка: видео не найдено")
63
- return
64
 
65
- try:
66
  # Получаем исходный FPS
67
  cmd = f"ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 {input_path}"
68
  original_fps = subprocess.check_output(cmd, shell=True).decode().strip().split('/')
69
  original_fps = float(original_fps[0])/float(original_fps[1])
70
 
71
  if target_fps <= original_fps:
72
- await update.message.reply_text(f"Целевой FPS должен быть больше исходного ({original_fps:.2f})")
73
  return
74
- except Exception as e:
75
- await update.message.reply_text("Ошибка при определении исходного FPS видео")
76
- logging.error(e)
77
- return
78
 
79
- # Обработка видео
80
- output_path = os.path.join(TEMP_DIR, f"output_{update.update_id}.mp4")
81
-
82
- try:
83
  cmd = [
84
  'ffmpeg',
85
  '-i', input_path,
@@ -94,28 +87,22 @@ async def handle_fps(update: Update, context: ContextTypes.DEFAULT_TYPE):
94
  subprocess.run(cmd, check=True, timeout=300)
95
 
96
  # Отправка результата
97
- await update.message.reply_video(video=open(output_path, 'rb'))
 
98
 
 
 
99
  except subprocess.TimeoutExpired:
100
- await update.message.reply_text("Обработка заняла слишком много времени")
101
  except Exception as e:
102
- await update.message.reply_text("Ошибка при обработке видео")
103
- logging.error(e)
104
  finally:
105
- # Удаление временных файлов
106
- for path in [input_path, output_path]:
107
- if path and os.path.exists(path):
108
- os.remove(path)
109
-
110
- def main():
111
- application = Application.builder().token(TOKEN).build()
112
-
113
- application.add_handler(CommandHandler("start", start))
114
- application.add_handler(CommandHandler("help", help_command))
115
- application.add_handler(MessageHandler(filters.VIDEO, handle_video))
116
- application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_fps))
117
-
118
- application.run_polling()
119
 
120
  if __name__ == '__main__':
121
- main()
 
1
  import os
 
2
  import subprocess
3
+ import telebot
4
+ from telebot import types
 
5
 
 
6
  TOKEN = "7709328099:AAHvvz2Dqkzb2c0lNh9OViTBnUoW6ZcpreA"
7
  ALLOWED_EXTENSIONS = {'mp4', 'avi', 'mov', 'mkv'}
8
  TEMP_DIR = "temp_files"
9
 
10
+ bot = telebot.TeleBot(TOKEN)
 
 
 
 
11
 
12
+ # Хранилище данных пользователя
13
+ user_data = {}
 
 
 
14
 
15
+ @bot.message_handler(commands=['start', 'help'])
16
+ def send_welcome(message):
17
+ bot.reply_to(message,
18
+ "Привет! Отправь мне видео, и я интерполирую кадры до нужного FPS.\n"
19
+ "Инструкция:\n"
20
+ "1. Отправь видео файл\n"
21
  "2. Укажи желаемый FPS (целое число)\n"
22
+ "3. Жди результат обработки"
 
23
  )
24
 
25
+ @bot.message_handler(content_types=['video'])
26
+ def handle_video(message):
27
+ try:
28
+ # Скачивание файла
29
+ file_info = bot.get_file(message.video.file_id)
30
+ downloaded_file = bot.download_file(file_info.file_path)
31
+
32
+ # Создание временной директории
33
+ os.makedirs(TEMP_DIR, exist_ok=True)
34
+
35
+ # Сохранение файла
36
+ ext = file_info.file_path.split('.')[-1] if file_info.file_path else 'mp4'
37
+ input_path = os.path.join(TEMP_DIR, f"input_{message.chat.id}.{ext}")
38
+
39
+ with open(input_path, 'wb') as new_file:
40
+ new_file.write(downloaded_file)
41
+
42
+ user_data[message.chat.id] = {'input_path': input_path}
43
+
44
+ # Запрос FPS
45
+ msg = bot.send_message(message.chat.id, "Видео получено. Введите желаемый FPS:")
46
+ bot.register_next_step_handler(msg, process_fps)
47
 
48
+ except Exception as e:
49
+ bot.reply_to(message, f"Ошибка: {str(e)}")
 
 
 
 
 
 
 
 
 
50
 
51
+ def process_fps(message):
52
  try:
53
+ chat_id = message.chat.id
54
+ target_fps = int(message.text)
55
+
56
  if target_fps <= 0:
57
+ raise ValueError("FPS должен быть положительным числом")
58
+
59
+ if chat_id not in user_data or not os.path.exists(user_data[chat_id]['input_path']):
60
+ bot.send_message(chat_id, "Ошибка: видео не найдено")
61
+ return
62
 
63
+ input_path = user_data[chat_id]['input_path']
64
+ output_path = os.path.join(TEMP_DIR, f"output_{chat_id}.mp4")
 
 
65
 
 
66
  # Получаем исходный FPS
67
  cmd = f"ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 {input_path}"
68
  original_fps = subprocess.check_output(cmd, shell=True).decode().strip().split('/')
69
  original_fps = float(original_fps[0])/float(original_fps[1])
70
 
71
  if target_fps <= original_fps:
72
+ bot.send_message(chat_id, f"Целевой FPS должен быть больше исходного ({original_fps:.2f})")
73
  return
 
 
 
 
74
 
75
+ # Обработка видео
 
 
 
76
  cmd = [
77
  'ffmpeg',
78
  '-i', input_path,
 
87
  subprocess.run(cmd, check=True, timeout=300)
88
 
89
  # Отправка результата
90
+ with open(output_path, 'rb') as video_file:
91
+ bot.send_video(chat_id, video_file)
92
 
93
+ except ValueError:
94
+ bot.send_message(chat_id, "Пожалуйста, введите корректное целое число FPS")
95
  except subprocess.TimeoutExpired:
96
+ bot.send_message(chat_id, "Обработка заняла слишком много времени")
97
  except Exception as e:
98
+ bot.send_message(chat_id, f"Ошибка при обработке: {str(e)}")
 
99
  finally:
100
+ # Очистка временных файлов
101
+ if chat_id in user_data:
102
+ for path in [user_data[chat_id].get('input_path'), output_path]:
103
+ if path and os.path.exists(path):
104
+ os.remove(path)
105
+ del user_data[chat_id]
 
 
 
 
 
 
 
 
106
 
107
  if __name__ == '__main__':
108
+ bot.polling(none_stop=True)