File size: 1,489 Bytes
8822b3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# prompt: make a gradio app using ffmpeg.probe to display media info

import gradio as gr
import ffmpeg


def get_media_info(file_path):
  """
  Uses ffmpeg to probe the media file and extract information.
  """
  try:
    probe = ffmpeg.probe(file_path)
    video_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
    audio_stream = next((stream for stream in probe['streams'] if stream['codec_type'] == 'audio'), None)

    if video_stream:
      width = int(video_stream['width'])
      height = int(video_stream['height'])
      frame_rate = video_stream['avg_frame_rate']
      frame_count = int(video_stream['nb_frames'])
    else:
      width, height, frame_rate, frame_count = None, None, None, None

    if audio_stream:
      sample_rate = int(audio_stream['sample_rate'])
      channels = int(audio_stream['channels'])
    else:
      sample_rate, channels = None, None

    return {
        'width': width,
        'height': height,
        'frame_rate': frame_rate,
        'frame_count': frame_count,
        'sample_rate': sample_rate,
        'channels': channels,
    }

  except Exception as e:
    return {'error': str(e)}


iface = gr.Interface(
    fn=get_media_info,
    inputs=gr.inputs.File(label="Upload Media File"),
    outputs=gr.outputs.JSON(label="Media Information"),
    title="Media Info Extractor",
    description="Upload a media file (video or audio) to extract information using ffmpeg.",
)

iface.launch()