之前在项目中一直用mediainfo来获取媒体文件或流的信息,比如封装格式、音视频编码格式、码率等信息。
但实际使用中发现mediainfo输出的字段不容易被解析,而且表述方法不统一。例如,对于h264这种编码格式,mediainfo可能输出的表述为H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10;还比如,对于mp3这样的音频格式,居然会分两个字段进行描述,分别说明mpeg和layer3。所以在项目中苦于这种不确定的因素。
之前就在ffmpeg官网上看过ffprobe,不过名气被ffmpeg盖过了,没多少人使用,网上也没有什么介绍的资料。算了,自己写一点,记录一下吧。
官网说明:http://ffmpeg.org/ffprobe.html
ffprobe的编译就不说了,编译ffmpeg时控制一下configure的参数即可。
OK,下面简单使用一下。
命令行:
./ffprobe -print_format json -show_format -show_streams -i ./video/c.ts
其中:
-print_format json(以json格式输出),
-show_format(输出封装格式信息),
-show_streams(输出流信息),
-i ./video/c.ts(输入文件)
Input #0, mpegts, from './video/c.ts': Duration: 00:00:59.67, start: 1.172511, bitrate: 919 kb/s Program 1 Metadata: service_name : Service01 service_provider: FFmpeg Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 640x360, 25 fps, 25 tbr, 90k tbn, 50 tbc Stream #0:1[0x101]: Audio: aac ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 103 kb/s "streams": [ { "index": 0, "codec_name": "h264", "codec_long_name": "H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10", "profile": "High", "codec_type": "video", "codec_time_base": "1/50", "codec_tag_string": "[27][0][0][0]", "codec_tag": "0x001b", "width": 640, "height": 360, "has_b_frames": 2, "sample_aspect_ratio": "0:1", "display_aspect_ratio": "0:1", "pix_fmt": "yuv420p", "level": 30, "is_avc": "0", "nal_length_size": "0", "id": "0x100", "r_frame_rate": "25/1", "avg_frame_rate": "25/1", "time_base": "1/90000", "start_pts": 108000, "start_time": "1.200000", "duration_ts": 5367600, "duration": "59.640000", "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 } }, { "index": 1, "codec_name": "aac", "codec_long_name": "AAC (Advanced Audio Coding)", "codec_type": "audio", "codec_time_base": "1/44100", "codec_tag_string": "[15][0][0][0]", "codec_tag": "0x000f", "sample_fmt": "fltp", "sample_rate": "44100", "channels": 2, "bits_per_sample": 0, "id": "0x101", "r_frame_rate": "0/0", "avg_frame_rate": "0/0", "time_base": "1/90000", "start_pts": 105526, "start_time": "1.172511", "duration_ts": 5340780, "duration": "59.342000", "bit_rate": "103359", "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 } } ], "format": { "filename": "./video/c.ts", "nb_streams": 2, "format_name": "mpegts", "format_long_name": "MPEG-TS (MPEG-2 Transport Stream)", "start_time": "1.172511", "duration": "59.667489", "size": "6859932", "bit_rate": "919754" } }
嗯,效果还是不错的。