File size: 2,630 Bytes
14590e3 | 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import json
import os
import shutil
import subprocess
import gradio as gr
def probe_video(video_path):
if not video_path:
return "Please upload a video.", None
if shutil.which("ffprobe") is None:
return (
"ffprobe not found. Add `ffmpeg` to packages.txt at the repo root.",
None,
)
try:
result = subprocess.run(
[
"ffprobe",
"-v", "quiet",
"-print_format", "json",
"-show_format",
"-show_streams",
video_path,
],
capture_output=True,
text=True,
check=True,
)
except subprocess.CalledProcessError as e:
return f"ffprobe failed:\n{e.stderr}", None
info = json.loads(result.stdout)
fmt = info.get("format", {}) or {}
streams = info.get("streams", []) or []
v = next((s for s in streams if s.get("codec_type") == "video"), {})
a = next((s for s in streams if s.get("codec_type") == "audio"), {})
summary = {
"filename": os.path.basename(fmt.get("filename", "")),
"format": fmt.get("format_long_name") or fmt.get("format_name"),
"duration_sec": fmt.get("duration"),
"size_bytes": fmt.get("size"),
"overall_bitrate_bps": fmt.get("bit_rate"),
"video": {
"codec": v.get("codec_name"),
"profile": v.get("profile"),
"width": v.get("width"),
"height": v.get("height"),
"pix_fmt": v.get("pix_fmt"),
"frame_rate": v.get("r_frame_rate"),
"bitrate_bps": v.get("bit_rate"),
},
"audio": {
"codec": a.get("codec_name"),
"sample_rate": a.get("sample_rate"),
"channels": a.get("channels"),
"bitrate_bps": a.get("bit_rate"),
},
}
return json.dumps(summary, indent=2, ensure_ascii=False), video_path
with gr.Blocks(title="OneVision Encoder Codec View") as demo:
gr.Markdown(
"# OneVision Encoder Codec View\n"
"Upload a video to inspect its container / codec metadata via `ffprobe`."
)
with gr.Row():
with gr.Column():
video_in = gr.Video(label="Input video", sources=["upload"])
run_btn = gr.Button("Probe", variant="primary")
with gr.Column():
video_out = gr.Video(label="Preview")
info_out = gr.Code(label="Metadata (JSON)", language="json")
run_btn.click(probe_video, inputs=video_in, outputs=[info_out, video_out])
if __name__ == "__main__":
demo.launch()
|