Spaces:
Sleeping
Sleeping
File size: 1,818 Bytes
7fb2f0e b96c956 7fb2f0e f739e3a b789881 f739e3a 7fb2f0e f739e3a b96c956 7fb2f0e b96c956 7fb2f0e f739e3a 7fb2f0e f739e3a 067cc9e 7fb2f0e f739e3a b789881 f739e3a 7fb2f0e b96c956 7fb2f0e b96c956 7fb2f0e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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()
|