Yaroslav commited on
Commit
1ce5c4e
·
verified ·
1 Parent(s): dbc5df6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -17
app.py CHANGED
@@ -1,13 +1,20 @@
1
  import gradio as gr
2
  import ffmpeg
3
- import time
4
  import os
 
5
 
6
  def interpolate_video(video, fps):
7
  input_path = video.name
8
  output_path = "interpolated_video.mp4"
9
 
10
- # Интерполяция кадров с использованием фильтра minterpolate
 
 
 
 
 
 
 
11
  process = (
12
  ffmpeg
13
  .input(input_path)
@@ -17,33 +24,42 @@ def interpolate_video(video, fps):
17
  .run_async(pipe_stdout=True, pipe_stderr=True)
18
  )
19
 
20
- # Вставим progress bar
21
- total_duration = float(ffmpeg.probe(input_path)['format']['duration'])
22
  progress = 0
23
  while process.poll() is None:
24
  time.sleep(0.5) # Обновление каждые 0.5 секунды
25
- stderr_output = process.stderr.read().decode('utf-8')
26
- if "frame=" in stderr_output:
27
- current_time = float(stderr_output.split('time=')[-1].split(' ')[0].replace(':','')) # Извлечение текущего времени кадра
28
- progress = (current_time / total_duration) * 100
29
- yield progress # Обновление значения progress bar
 
 
 
 
 
 
 
30
 
31
  process.wait()
32
-
33
- return output_path
34
 
 
 
 
 
 
 
35
  iface = gr.Interface(
36
  fn=interpolate_video,
37
  inputs=[
38
- gr.File(label="Upload Video"),
39
- gr.Slider(minimum=24, maximum=60, step=1, value=60, label="Frame Rate (FPS)")
40
  ],
41
  outputs=[
42
- gr.Video(label="Interpolated Video"),
43
- gr.Progress(label="Processing Progress")
44
  ],
45
- title="Frame Interpolation App",
46
- description="Upload a video and adjust the slider to set the target frame rate (FPS) for interpolation."
47
  )
48
 
49
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import ffmpeg
 
3
  import os
4
+ import time
5
 
6
  def interpolate_video(video, fps):
7
  input_path = video.name
8
  output_path = "interpolated_video.mp4"
9
 
10
+ # Проверяем входное видео
11
+ try:
12
+ probe = ffmpeg.probe(input_path)
13
+ total_duration = float(probe['format']['duration'])
14
+ except ffmpeg.Error as e:
15
+ return f"Ошибка при анализе видео: {e}"
16
+
17
+ # Начинаем процесс интерполяции
18
  process = (
19
  ffmpeg
20
  .input(input_path)
 
24
  .run_async(pipe_stdout=True, pipe_stderr=True)
25
  )
26
 
 
 
27
  progress = 0
28
  while process.poll() is None:
29
  time.sleep(0.5) # Обновление каждые 0.5 секунды
30
+ # Чтение строки stderr
31
+ stderr_line = process.stderr.readline().decode('utf-8').strip()
32
+ if "time=" in stderr_line:
33
+ try:
34
+ # Парсим текущий прогресс
35
+ time_match = stderr_line.split("time=")[-1].split(" ")[0]
36
+ h, m, s = map(float, time_match.split(":"))
37
+ current_time = h * 3600 + m * 60 + s
38
+ progress = (current_time / total_duration) * 100
39
+ except Exception:
40
+ pass
41
+ yield f"Прогресс: {progress:.2f}%"
42
 
43
  process.wait()
 
 
44
 
45
+ if os.path.exists(output_path):
46
+ return output_path
47
+ else:
48
+ return "Ошибка в обработке видео."
49
+
50
+ # Интерфейс Gradio
51
  iface = gr.Interface(
52
  fn=interpolate_video,
53
  inputs=[
54
+ gr.File(label="Загрузить видео"),
55
+ gr.Slider(minimum=24, maximum=60, step=1, value=60, label="Частота кадров (FPS)")
56
  ],
57
  outputs=[
58
+ gr.Video(label="Интерполированное видео"),
59
+ gr.Text(label="Прогресс обработки")
60
  ],
61
+ title="Приложение для интерполяции видео",
62
+ description="Загрузите видео и выберите желаемую частоту кадров (FPS)."
63
  )
64
 
65
  if __name__ == "__main__":