ffmpeg格式化输出多媒体文件信息

前言

        前段时间有一批音视频数据需要获取其内容格式等详情信息,打算用python、java等语言实现脚本来处理,后来找了opencv、Multimedia针对语言的一些类库还是不能满足需求,最后目光回到了ffmpeg,作为强大的音视频编码神器,这些信息肯定是有的,因此找了一下格式化输出信息的相关参数,这篇文章内容不多,算是一个记录。

正文

        既然各个生态中对于这块支持不好,那么就直接使用ffmpeg在操作系统cmd中直接调用吧,其中ffprobe能很好的输出相关信息,也可以将数据输出为json格式。因此我们可以通过语言原生调用操作系统的cmd命令进行交互,获取json内容结果后自行解析。

ffprobe -i input.mp4 -v quiet -print_format json -show_streams

音频输出格式

        如下输出为多媒体流json数据信息。

{
    "streams": [
        {
            "index": 0,
            "codec_name": "mp3", 
            "codec_long_name": "MP3 (MPEG audio layer 3)", 
            "codec_type": "audio",   
            "codec_time_base": "1/44100",
            "codec_tag_string": "[0][0][0][0]",
            "codec_tag": "0x0000",
            "sample_fmt": "fltp",
            "sample_rate": "44100",
            "channels": 2,
            "channel_layout": "stereo",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/14112000",
            "start_pts": 353600,
            "start_time": "0.025057",
            "duration_ts": 5898240,
            "duration": "0.417959",
            "bit_rate": "48000",
            "disposition": {
                "default": 0,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "encoder": "Lavc58.35"
            }
        }
    ]
}

视频输出格式

        视频文件显然有视频流与音频流两种,streams中list分为两个部分,mp4视频流和mp3音频流数据。

{
    "streams": [
        {
            "index": 0,
            "codec_name": "mpeg4",
            "codec_long_name": "MPEG-4 part 2",
            "profile": "Simple Profile",
            "codec_type": "video",
            "codec_time_base": "100/2997",
            "codec_tag_string": "mp4v",
            "codec_tag": "0x7634706d",
            "width": 720,
            "height": 480,
            "coded_width": 720,
            "coded_height": 480,
            "closed_captions": 0,
            "has_b_frames": 0,
            "sample_aspect_ratio": "8:9",
            "display_aspect_ratio": "4:3",
            "pix_fmt": "yuv420p",
            "level": 1,
            "chroma_location": "left",
            "refs": 1,
            "quarter_sample": "false",
            "divx_packed": "false",
            "r_frame_rate": "2997/100",
            "avg_frame_rate": "2997/100",
            "time_base": "1/11988",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 2696400,
            "duration": "224.924925",
            "bit_rate": "1013069",
            "max_bit_rate": "1024000",
            "nb_frames": "6741",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "language": "und",
                "handler_name": "VideoHandler"
            }
        },
        {
            "index": 1,
            "codec_name": "aac",
            "codec_long_name": "AAC (Advanced Audio Coding)",
            "profile": "LC",
            "codec_type": "audio",
            "codec_time_base": "1/44100",
            "codec_tag_string": "mp4a",
            "codec_tag": "0x6134706d",
            "sample_fmt": "fltp",
            "sample_rate": "44100",
            "channels": 2,
            "channel_layout": "stereo",
            "bits_per_sample": 0,
            "r_frame_rate": "0/0",
            "avg_frame_rate": "0/0",
            "time_base": "1/44100",
            "start_pts": 0,
            "start_time": "0.000000",
            "duration_ts": 9788083,
            "duration": "221.951995",
            "bit_rate": "126784",
            "max_bit_rate": "128000",
            "nb_frames": "9560",
            "disposition": {
                "default": 1,
                "dub": 0,
                "original": 0,
                "comment": 0,
                "lyrics": 0,
                "karaoke": 0,
                "forced": 0,
                "hearing_impaired": 0,
                "visual_impaired": 0,
                "clean_effects": 0,
                "attached_pic": 0,
                "timed_thumbnails": 0
            },
            "tags": {
                "language": "und",
                "handler_name": "SoundHandler"
            }
        }
    ]
}

参考

http://ffmpeg.org/ffprobe.html

上一篇:change the available bandwidth of tcp flow dynamically in mininet


下一篇:用PyTorch做参数估计