目錄php
前言json
實戰bash
分析音頻app
分析視頻ide
總結工具
前言
FFmpeg是一個完整的跨平臺音視頻解決方案,它能夠用於處理音頻和視頻的轉碼、錄製、流化處理等應用場景。官網:http://ffmpeg.org/。FFmpeg有三大利器,分別是ffmpeg、ffprobe、ffplay。今天主要介紹ffprobe,它是FFmpeg用於查看媒體文件格式信息的強大工具。ui
實戰
分析音頻
話很少說,先來看ffprobe的簡單使用實例:編碼
ffprobe 少年.mp3
在 少年.mp3 文件的同級目錄運行上述命令,會有以下輸出信息:url
[mp3 @ 0x7fdf85801400] Skipping 0 bytes of junk at 417. Input #0, mp3, from '少年.mp3': Duration: 00:03:56.15, start: 0.025057, bitrate: 128 kb/s Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s Metadata: encoder : LAME3.99r Side data: replaygain: track gain - -8.000000, track peak - unknown, album gain - unknown, album peak - unknown,
它們都是什麼意思呢?咱們接下來逐行分析一下,首先看第一行:spa
Duration: 00:03:56.15, start: 0.025057, bitrate: 128 kb/s
代表了音頻文件(少年.mp3)的主要信息,音樂時長是3分56.15秒,開始播放聲音的時間是0.025057秒,碼率是128kb/s。
接下來看下一行:
Stream #0:0: Audio: mp3, 44100 Hz, stereo, s16p, 128 kb/s
代表音頻文件只有一路音頻流,音頻編碼格式是mp3,採樣率是44.1kHz,聲道數是立體聲雙聲道,採樣格式是16位的平鋪格式,碼率是128kb/s。
再看下一行:
Metadata: encoder : LAME3.99r
代表音頻文件的音頻編碼器是LAME,版本號是3.99r,LAME 是最好的MP3編碼器,一段時間內被業界認爲是編碼高品質MP3的最好也是惟一的選擇。若是想具體瞭解LAME,能夠點這裏。
分析視頻
接下來,咱們看一個視頻文件,本身比較喜歡的涼涼的MV。
運行以下命令:
ffprobe 涼涼-MV.mp4
命令執行結果,輸出內容以下:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '涼涼-MV.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42isom creation_time : 2018-06-21 06:42:54 Duration: 00:03:24.13, start: 0.000000, bitrate: 507 kb/s Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 64 kb/s (default) Metadata: creation_time : 2018-06-21 06:42:54 Stream #0:1(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x368, 439 kb/s, 23 fps, 23 tbr, 90k tbn, 46 tbc (default) Metadata: creation_time : 2018-06-21 06:42:54 encoder : JVT/AVC Coding
接下來咱們看看這些信息的具體意思,先看第一行:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '涼涼-MV.mp4':
代表視頻文件的封裝格式屬於 mov,mp4,m4a,3gp,3g2,mj2 格式集合。
接下來看下一行:
Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42isom creation_time : 2018-06-21 06:42:54
其中,major_brand代表視頻文件屬於mp4的子格式mp42,兼容格式有mp42isom,視頻文件建立時間是2018年6月21日6時42分54秒,具體Metadata數據格式能夠參考:
aligned(8) class FileTypeBox extends Box(‘ftyp’) { unsigned int(32) major_brand; unsigned int(32) minor_version; unsigned int(32) compatible_brands[]; // to end of the box }
接下來看下一行:
Duration: 00:03:24.13, start: 0.000000, bitrate: 507 kb/s
代表視頻文件時間長度是3分鐘24.13秒,開始播放時間是0.0秒,視頻碼率是507kb/s。
繼續分析下一行:
Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 64 kb/s (default) Metadata: creation_time : 2018-06-21 06:42:54
代表視頻文件中包含一路音頻流,音頻編碼格式是aac,採樣率是44.1kHz,聲道數是立體聲雙聲道,採樣格式是32位的平鋪格式,碼率是默認的64kb/s(注意:這裏只是音頻碼率,總碼率的一部分)。其中fltp具體格式能夠參考:
static const SampleFmtInfo sample_fmt_info[AV_SAMPLE_FMT_NB] = { [AV_SAMPLE_FMT_U8] = { .name = "u8", .bits = 8, .planar = 0, .altform = AV_SAMPLE_FMT_U8P }, [AV_SAMPLE_FMT_S16] = { .name = "s16", .bits = 16, .planar = 0, .altform = AV_SAMPLE_FMT_S16P }, [AV_SAMPLE_FMT_S32] = { .name = "s32", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_S32P }, [AV_SAMPLE_FMT_S64] = { .name = "s64", .bits = 64, .planar = 0, .altform = AV_SAMPLE_FMT_S64P }, [AV_SAMPLE_FMT_FLT] = { .name = "flt", .bits = 32, .planar = 0, .altform = AV_SAMPLE_FMT_FLTP }, [AV_SAMPLE_FMT_DBL] = { .name = "dbl", .bits = 64, .planar = 0, .altform = AV_SAMPLE_FMT_DBLP }, [AV_SAMPLE_FMT_U8P] = { .name = "u8p", .bits = 8, .planar = 1, .altform = AV_SAMPLE_FMT_U8 }, [AV_SAMPLE_FMT_S16P] = { .name = "s16p", .bits = 16, .planar = 1, .altform = AV_SAMPLE_FMT_S16 }, [AV_SAMPLE_FMT_S32P] = { .name = "s32p", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_S32 }, [AV_SAMPLE_FMT_S64P] = { .name = "s64p", .bits = 64, .planar = 1, .altform = AV_SAMPLE_FMT_S64 }, [AV_SAMPLE_FMT_FLTP] = { .name = "fltp", .bits = 32, .planar = 1, .altform = AV_SAMPLE_FMT_FLT }, [AV_SAMPLE_FMT_DBLP] = { .name = "dblp", .bits = 64, .planar = 1, .altform = AV_SAMPLE_FMT_DBL }, };
繼續分析下一行:
Stream #0:1(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x368, 439 kb/s, 23 fps, 23 tbr, 90k tbn, 46 tbc (default) Metadata: creation_time : 2018-06-21 06:42:54 encoder : JVT/AVC Coding
代表MV文件中還包含另外一路視頻流,視頻編碼格式是h264,子類型是high類型,顏色空間是yuv420p,分辨率是640*368,碼率是439kb/s,幀率是23幀每秒,tbr也是幀率的意思,tbn表示時間精度是1/90k,即1s=90000時間單位,tbc是編碼時間精度,即1s=46時間單位。
注意:這裏有一個你們須要注意的地方,不說的話,你們極可能忽略。上邊咱們看到了整個視頻文件的碼率是507kb/s,其中音頻碼率是64kb/s,視頻碼率是439kb/s,發現沒有64kb/s+439kb/s≈507kb/s。總體視頻文件的碼率=音頻碼率+視頻碼率。
若是想獲取更多的媒體信息能夠使用以下命令:
ffprobe -show_format 涼涼-MV.mp4
輸出的結果比以前多了其餘的format信息:
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '涼涼-MV.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42isom creation_time : 2018-06-21 06:42:54 Duration: 00:03:24.13, start: 0.000000, bitrate: 507 kb/s Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 64 kb/s (default) Metadata: creation_time : 2018-06-21 06:42:54 Stream #0:1(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x368, 439 kb/s, 23 fps, 23 tbr, 90k tbn, 46 tbc (default) Metadata: creation_time : 2018-06-21 06:42:54 encoder : JVT/AVC Coding [FORMAT] filename=涼涼-MV.mp4 nb_streams=2 nb_programs=0 format_name=mov,mp4,m4a,3gp,3g2,mj2 format_long_name=QuickTime / MOV start_time=0.000000 duration=204.130433 size=12957343 bit_rate=507806 probe_score=100 TAG:major_brand=mp42 TAG:minor_version=0 TAG:compatible_brands=mp42isom TAG:creation_time=2018-06-21 06:42:54 [/FORMAT]
好比nb_streams=2表示包含兩路媒體流,一路音頻流,一路視頻流。
duration=204.130433,表示視頻文件長204.130433秒。
size=12957343,表示視頻文件大小約爲13MB≈12957343/1000/1000,注意單位是字節Byte,不是bit,另外FFmpeg中的b/s單位,儘管是小b,但表示的意義是字節byte。FFmpeg這樣作很容易讓初學者混淆字節和比特的概念,通常來講咱們都是說大B是字節,小b是比特。你們注意區分就好。
bit_rate=507806,表示碼率是507kb/s≈507806/1000,注意進制沒有使用1024,爲了便於計算直接使用1000。
而後看一下系統給出的媒體概要信息,和咱們用ffproge分析的結果是一致的。
另外,仍是更詳細的輸出命令,而且還能夠指定json格式化輸出。命令以下:
ffprobe -print_format json -show_streams 涼涼-MV.mp4
輸出結果以下:
{ Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '涼涼-MV.mp4': Metadata: major_brand : mp42 minor_version : 0 compatible_brands: mp42isom creation_time : 2018-06-21 06:42:54 Duration: 00:03:24.13, start: 0.000000, bitrate: 507 kb/s Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 64 kb/s (default) Metadata: creation_time : 2018-06-21 06:42:54 Stream #0:1(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x368, 439 kb/s, 23 fps, 23 tbr, 90k tbn, 46 tbc (default) Metadata: creation_time : 2018-06-21 06:42:54 encoder : JVT/AVC Coding "streams": [ { "index": 0, "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": 9001984, "duration": "204.126621", "bit_rate": "64000", "max_bit_rate": "70384", "nb_frames": "8791", "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 }, "tags": { "creation_time": "2018-06-21 06:42:54", "language": "und" } }, { "index": 1, "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/46", "codec_tag_string": "avc1", "codec_tag": "0x31637661", "width": 640, "height": 368, "coded_width": 640, "coded_height": 368, "has_b_frames": 2, "sample_aspect_ratio": "0:1", "display_aspect_ratio": "0:1", "pix_fmt": "yuv420p", "level": 30, "chroma_location": "left", "refs": 4, "is_avc": "true", "nal_length_size": "4", "r_frame_rate": "23/1", "avg_frame_rate": "140850000/6123913", "time_base": "1/90000", "start_pts": 7826, "start_time": "0.086956", "duration_ts": 18371739, "duration": "204.130433", "bit_rate": "439965", "bits_per_raw_sample": "8", "nb_frames": "4695", "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 }, "tags": { "creation_time": "2018-06-21 06:42:54", "language": "und", "encoder": "JVT/AVC Coding" } } ] }
總結
至此,咱們已經見識了ffprobe的強大之處,更多技能尚未陳述完,你們本身嘗試解鎖吧。 最後推薦你們使用以下命令:
ffprobe -h
or
man ffprobe
獲取更多用法,好比:
FFPROBE(1) NAME ffprobe - ffprobe media prober SYNOPSIS ffprobe [options] [input_url] DESCRIPTION ffprobe gathers information from multimedia streams and prints it in human- and machine-readable fashion. For example it can be used to check the format of the container used by a multimedia stream and the format and type of each media stream contained in it. If a url is specified in input, ffprobe will try to open and probe the url content. If the url cannot be opened or recognized as a multimedia file, a positive exit code is returned. ffprobe may be employed both as a standalone application or in combination with a textual filter, which may perform more sophisticated processing, e.g. statistical processing or plotting. Options are used to list some of the formats supported by ffprobe or for specifying which information to display, and for setting how ffprobe will show it. ffprobe output is designed to be easily parsable by a textual filter, and consists of one or more sections of a form defined by the selected writer, which is specified by the print_format option. Sections may contain other nested sections, and are identified by a name (which may be shared by other sections), and an unique name. See the output of sections. Metadata tags stored in the container or in the streams are recognized and printed in the corresponding "FORMAT", "STREAM" or "PROGRAM_STREAM" section.