get-video-speed / app.py
soiz1's picture
Update app.py
067cc9e verified
import gradio as gr
import tempfile
import os
import ffmpeg # より正確なメタデータ取得のため
def get_duration(file_path):
"""ffprobe を使って正確な動画長を取得"""
try:
probe = ffmpeg.probe(file_path)
video_streams = [stream for stream in probe['streams'] if stream['codec_type'] == 'video']
if not video_streams:
return None
return float(video_streams[0]['duration'])
except Exception as e:
return None
def calculate_speedup(video1_bytes, video2_bytes):
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp1, \
tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp2:
temp1.write(video1_bytes)
temp2.write(video2_bytes)
temp1_path = temp1.name
temp2_path = temp2.name
try:
duration1 = get_duration(temp1_path)
duration2 = get_duration(temp2_path)
if duration1 is None or duration2 is None:
return "エラー: 動画の再生時間を取得できませんでした。"
if duration2 == 0:
return "エラー: 2つ目の動画の長さが 0 秒です。"
speed = duration1 / duration2
return f"動画2は動画1の約 {speed} 倍速です。"
finally:
os.remove(temp1_path)
os.remove(temp2_path)
# Gradio UI
demo = gr.Interface(
fn=calculate_speedup,
inputs=[
gr.File(label="動画1(元動画)", type="binary"),
gr.File(label="動画2(倍速動画)", type="binary")
],
outputs="text",
title="精密!動画倍速計算ツール",
description="2つの動画の長さから、2つ目の動画が何倍速かを小数第4位まで正確に計算します。"
)
if __name__ == "__main__":
demo.launch()