Yaroslav
Update app.py
1ce5c4e verified
raw
history blame
2.24 kB
import gradio as gr
import ffmpeg
import os
import time
def interpolate_video(video, fps):
input_path = video.name
output_path = "interpolated_video.mp4"
# Проверяем входное видео
try:
probe = ffmpeg.probe(input_path)
total_duration = float(probe['format']['duration'])
except ffmpeg.Error as e:
return f"Ошибка при анализе видео: {e}"
# Начинаем процесс интерполяции
process = (
ffmpeg
.input(input_path)
.filter('minterpolate', fps=fps)
.output(output_path)
.overwrite_output()
.run_async(pipe_stdout=True, pipe_stderr=True)
)
progress = 0
while process.poll() is None:
time.sleep(0.5) # Обновление каждые 0.5 секунды
# Чтение строки stderr
stderr_line = process.stderr.readline().decode('utf-8').strip()
if "time=" in stderr_line:
try:
# Парсим текущий прогресс
time_match = stderr_line.split("time=")[-1].split(" ")[0]
h, m, s = map(float, time_match.split(":"))
current_time = h * 3600 + m * 60 + s
progress = (current_time / total_duration) * 100
except Exception:
pass
yield f"Прогресс: {progress:.2f}%"
process.wait()
if os.path.exists(output_path):
return output_path
else:
return "Ошибка в обработке видео."
# Интерфейс Gradio
iface = gr.Interface(
fn=interpolate_video,
inputs=[
gr.File(label="Загрузить видео"),
gr.Slider(minimum=24, maximum=60, step=1, value=60, label="Частота кадров (FPS)")
],
outputs=[
gr.Video(label="Интерполированное видео"),
gr.Text(label="Прогресс обработки")
],
title="Приложение для интерполяции видео",
description="Загрузите видео и выберите желаемую частоту кадров (FPS)."
)
if __name__ == "__main__":
iface.launch()