接著上文寫:
寫在前面
如果對(duì)FFmpeg有需要更多了解的請(qǐng)訂閱我的專題:音視頻專輯
2.3.1 FFmpeg 對(duì)編碼解碼器的支持
ffmpeg 支持的編解碼器種類共有 280 多種, 涵蓋了幾乎所有常見音視頻編碼格式, 能解碼幾乎所有的音視頻, 每種音視頻編解碼器的實(shí)現(xiàn)都在 libavcodec 目錄下有具體的 C 語(yǔ)言實(shí)現(xiàn)。
- 注:編碼器和解碼器的名稱不是完全匹配的,因此有些編碼器沒有對(duì)應(yīng)相同名稱的解碼器,反之, 解碼器也一樣。即使編碼和解碼都支持也不一定是完全對(duì)應(yīng)的,例如 h263 解碼器對(duì)應(yīng)有 h263p 和 h263 編碼器。
2.3.2 FFmpeg 對(duì)容器格式的支持
ffmpeg 支持對(duì)絕大多數(shù)的容器格式的讀寫操作,共計(jì) 190 多種, 涵蓋了互聯(lián)網(wǎng)上各 種常見媒體格式及日常生活中及專業(yè)應(yīng)用中的各種媒體格式。
2.3.3 FFmpeg 對(duì)過(guò)濾器的支持
| Filters | 說(shuō)明 |
|---|---|
| aformat | Convert the input audio to one of the specified formats. |
| anull | Pass the source unchanged to the output. |
| aresample | Resample audio data. |
| ashowinfo | Show textual information for each audio frame. |
| abuffer | Buffer audio frames, and make them accessible to the filterchain. |
| anullsrc | Null audio source, never return audio frames. |
| abuffersink | Buffer audio frames, and make them available to the end of the filter graph. |
| anullsink | Do absolutely nothing with the input audio. |
| copy | Copy the input video unchanged to the output. |
| crop | Crop the input video to width:height:x:y. |
| drawbox | Draw a colored box on the input video. |
| fade | Fade in/out input video |
| fieldorder | Set the field order. |
| fifo | Buffer input images and send them when they are requested. |
| format | Convert the input video to one of the specified pixel formats. |
| gradfun | Debands video quickly using gradients. |
| hflip | Horizontally flip the input video. |
| lut | Compute and apply a lookup table to the RGB/YUV input video. |
| lutrgb | Compute and apply a lookup table to the RGB input video. |
| lutyuv | Compute and apply a lookup table to the YUV input video. |
| negate | Negate input video. |
| noformat | Force libavfilter not to use any of the specified pixel formats for the input to the next filter. |
| null | Pass the source unchanged to the output. |
| overlay | Overlay a video source on top of the input. |
| pad | Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black). |
| pixdesctest | Test pixel format definitions. |
| scale | Scale the input video to width:height size and/or convert the image format. |
| select | Select frames to pass in output. |
| setdar | Set the frame display aspect ratio. |
| setpts | Set PTS for the output video frame. |
| setsar | Set the pixel sample aspect ratio. |
| settb | Set timebase for the output link. |
| showinfo | Show textual information for each video frame. |
| slicify | Pass the images of input video on to next video filter as multiple slices. |
| split | Pass on the input to two outputs. |
| transpose | Transpose input video. |
| unsharp | Sharpen or blur the input video. |
| vflip | Flip the input video vertically. |
| buffer | Buffer video frames, and make them accessible to the filterchain. |
| color | Provide an uniformly colored input, syntax is: [color[:size[:rate]]] |
| movie | Read from a movie source. |
| nullsrc | Null video source, never return images. |
| rgbtestsrc | Generate RGB test pattern. |
| testsrc | Generate test pattern. |
| buffersink | Buffer video frames, and make them available to the end of the filter graph. |
| nullsink | Do absolutely nothing with the input video. |
2.3.4 FFmpeg 對(duì)圖像顏色空間的支持
ffmpeg 支持常見的圖像顏色空間,并且在 libavswcale 中定義了顏色空間轉(zhuǎn)換的相關(guān) 函數(shù)實(shí)現(xiàn)各種顏色模式的互轉(zhuǎn)。
2.4 FFmpeg 功能及使用說(shuō)明
2.4.1 ffplay 對(duì)多媒體的支持能力驗(yàn)證
一、視頻
3gp 177X144 支持播放, 在 windows 下播放正常, 但是在 linux 下面偶有 BUG 如果 發(fā)現(xiàn)畫面無(wú)法顯示而聲音可以播放的情況下 可以試著切換全屏或者切換分辨率。
| AVI | 208X176 | 支持 |
|---|---|---|
| 320X240 | 支持 | |
| 720X400 | 支持 | |
| 720X576 | 支持 | |
| DAT | 352X288 | 支持 |
| DiVX | 720X576 | 支持 |
| MKV | 320X240 | 支持 |
| 352X288 | 支持 | |
| 704X304 | 支持 | |
| 720X576 | 支持 | |
| MP4 | 320X240 | 支持 |
| 352X288 | 支持 | |
| 720X400 | 支持 | |
| MPG | 320X240 | 支持 |
| 352X288 | 支持 | |
| 480X576 | 支持 | |
| 720X576 | 支持 | |
| 720X480 | 支持 | |
| VOB | 352X288 | 支持 |
| XVID | 720X576 | 支持 |
| MOV | 支持 | |
| RMVB | 支持 |
二、音頻
| AC3 | 48KHZ | 支持 | |
|---|---|---|---|
| APE | 11KHZ | 支持 | |
| 22KHZ | 支持 | ||
| 44KHZ | 支持 | ||
| 48KHZ | 支持 | ||
| MP3 | 32KHZ | 64Kbps | 支持 |
| 32KHZ | 128KbpS | 支持 | |
| 32KHZ | 160Kbps | 支持 | |
| 32KHZ | 192Kbps | 支持 | |
| 32KHZ | 320Kbps | 支持 | |
| 44KHZ | 64Kbps | 支持 | |
| 44KHZ | 128Kbps | 支持 | |
| 44KHZ | 160Kbps | 支持 | |
| 44KHZ | 192Kbps | 支持 | |
| 44KHZ | 320Kbps | 支持 | |
| 48KHZ | 64Kbps | 支持 | |
| 48KHZ | 128Kbps | 支持 | |
| 48KHZ | 160Kbps | 支持 | |
| 48KHZ | 192Kbps | 支持 | |
| 48KHZ | 320Kbps | 支持 | |
| OGG | 32KHZ | 128Kbps | 支持 |
| 32KHZ | 192Kbps | 支持 | |
| 44KHZ | 64Kbps | 支持 | |
| 44KHZ | 128Kbps | 支持 | |
| 44KHZ | 192Kbps | 支持 | |
| 48KHZ | 64Kbps | 支持 | |
| 44KHZ | 128Kbps | 支持 | |
| 44KHZ | 192Kbps | 支持 | |
| WAV | 11KHZ | 支持 | |
| 22KHZ | 16Kbps | 支持 | |
| 44KHZ | 16Kbps | 支持 | |
| 48KHZ | 16Kbps | 支持 | |
| WMA | 8KHZ | 16Kbps | 支持 |
| 11KHZ | 16Kbps | 支持 | |
| 16KHZ | 16Kbps | 支持 | |
| 22KHZ | 16Kbps | 支持 | |
| 44KHZ | 16Kbps | 支持 | |
| 48KHZ | 16Kbps | 支持 |
三、圖像
| PNG | 支持 |
|---|---|
| JPG | 支持 |
| JPEG | 支持 |
| GIF | 支持 |
| BMP | 支持 |
2.4.2 FFmpeg 格式轉(zhuǎn)換
第一步:準(zhǔn)備媒體
前面已經(jīng)講的很清楚了, ffmpeg 如何安裝不在贅述。 準(zhǔn)備好相應(yīng)的文件, 如圖 2-1 所示。

第二步:?jiǎn)?dòng) ffmpeg
由于做的是格式轉(zhuǎn)換,在 ffserver 上不能直觀的看見結(jié)果,故我是在 linux 下進(jìn)行的。 打開終端,值得一提的是格式轉(zhuǎn)換需要超級(jí)用戶才能進(jìn)行,故在命令行輸入:su,<回車>, 輸入密碼進(jìn)入超級(jí)用戶, 本例中, 以 FFmpeg 將 test.avi 轉(zhuǎn)換為 test.mpg。 在命令行中輸 入:./ffmpeg –i test.avi –r 25 –s 720x400 test.mpg<回車>。 其中原格式分辨率為 320x240, 將轉(zhuǎn)為 720x400, -r 前面已經(jīng)解釋其含義, 表示設(shè)置幀頻為 25。 轉(zhuǎn)換成功后如圖 2-2 所 示,前后兩種格式播放效果如圖 2-3 所示。相應(yīng)的,轉(zhuǎn)換為其他格式做相應(yīng)的變化即可。
同時(shí)還可以在轉(zhuǎn)換格式時(shí)進(jìn)行強(qiáng)制的音視頻轉(zhuǎn)換, 如–vcodec + 格式, 將會(huì)強(qiáng)制將 視頻按指定格式編碼, -acodec +格式, 將會(huì)強(qiáng)制按指定格式編碼音頻信息。 在轉(zhuǎn)換中有 很多其他參數(shù)可以指定,如碼率、分辨率、幀率等,具體按照 ffmpeg 的參數(shù)說(shuō)明指定參 數(shù)即可。 但有一條轉(zhuǎn)低不轉(zhuǎn)高的原則需要注意, 即品質(zhì)差的音視頻轉(zhuǎn)換不建議轉(zhuǎn)換到品 質(zhì)好的音視頻。


再說(shuō)說(shuō)如何在轉(zhuǎn)換視頻的時(shí)候?qū)⒁纛l合成到視頻中, 且覆蓋其原來(lái)的音頻。 這個(gè)現(xiàn) 在摸索出兩種方法。
方法一:需要兩條命令實(shí)現(xiàn), 先在命令行中輸入:
./ffmpeg –i test.avi -an –r 25 test.mpg <回車>
此時(shí)將生成一個(gè)沒有聲音的 test.mpg 視頻, 再在命令行中輸入:
./ffmpeg –i test.mpg –i test.mp3 –r 25 test1.mpg<回車>
此時(shí)將會(huì)生成一個(gè)名為 test1.mpg 的視頻。 該視頻播放時(shí)視頻為 test.avi 的視頻, 但音頻 變?yōu)榱?test.mp3 的音頻了。
方法二:只要一條指令即可實(shí)現(xiàn)。 在命令行中輸入:
./ffmpeg –i test.avi –i test.mp3 –vcodec copy –acodec copy –r 25 test2.mpg<回車>
此時(shí)將會(huì)生成一個(gè)名為 test2.mpg 的視頻, 播放時(shí)其視頻為 test.avi 的視頻, 音頻為 test.mp3。 –vcodec copy 為 force video codec(‘copy’ to copy stream)。
有一點(diǎn)需要注意, 文件命名不能有空格, 否則會(huì)導(dǎo)致編譯時(shí)不能通過(guò)。 另外, -an 為不能使音頻記錄。
第三步:播放媒體
播放我們轉(zhuǎn)換的媒體, 看看是否滿足我們當(dāng)初的愿望, 不出什么差錯(cuò)的話, 是完全 能夠滿足我們的要求的。
2.4.3 FFmpeg 視頻截圖
截取一張 300x200 尺寸大小的格式為 jpg 的一張圖片:
./ffmpeg –i test.avi –y –f image2 –t 0.001 –s 300x200 test.jpg
要截取指定時(shí)間的圖片, 如 5 秒之后的:
./ffmpeg –i test.avi –y –f image2 –ss 5 –t 0.001 –s 300x200 test.jpg
其中, -ss 后的單位為秒, 也可寫成:-ss 00:00:05。
把視頻的前 30 幀轉(zhuǎn)換為一個(gè)動(dòng)態(tài)的 gif 圖。 需要說(shuō)明的是, 轉(zhuǎn)換成功之后, 如果用 ffplay 播放是看不出效果的, 建議換成其他圖片播放器播放。 其轉(zhuǎn)換命令為:
./ffmpeg –i test.avi –vframes 30 –pix_fmt rgb24–y –f gif test.gif
也可以從視頻中的第 10 秒開始截取后面的 5 秒內(nèi)容轉(zhuǎn)換為一個(gè)無(wú)限重播的動(dòng)態(tài) gif 圖。 其命令為:
./ffmpeg –i test.avi –pix_fmt rgb24 –ss 10 –t 5 –y –f gif test.gif
上面兩種動(dòng)態(tài) gif 都是只播一次, 想讓其一直播, 可再加一個(gè)參數(shù):-loop_output 0。
2.4.4 FFmpeg 屏幕錄制
屏幕錄制其命令為:
./ffmpeg -f x11grab -r 25 -s wxga -i :0.0 /tmp/outputFile.mpg
其他相關(guān)參數(shù)可自行添加。 需要說(shuō)明的是, 各個(gè)版本的 ffmpeg 對(duì)屏幕錄制的命令不一。 如果你只想錄制一個(gè)應(yīng)用程序窗口或者桌面上的一個(gè)固定區(qū)域, 那么可以指定偏移位置 和區(qū)域大小。 使用 xwininfo -frame 命令可以完成查找上述參數(shù)。
注:ffmpeg 的屏幕錄制功能只能在 Linux 環(huán)境下有效。 并且在配置時(shí)需要添加 –enable-x11grub 指令, 默認(rèn)關(guān)閉。
2.4.5 FFmpeg 音視頻采集
把攝像頭的實(shí)時(shí)視頻錄制下來(lái), 存儲(chǔ)為文件
./ffmpeg -f video4linux -s 320x240 -r 10 -i /dev/video0 test.asf
錄音, 其命令為:
./ffmpeg –i /dev/dsp -f oss test.mp3
2.5 FFmpeg 應(yīng)用實(shí)例
2.5.1 用 FFserver 從文件生成流媒體
一、安裝 ffmpeg
在 ubuntu 下, 運(yùn)行 sudo apt-get ffmpeg 安裝 ffmpeg, 在其他 linux 操作系統(tǒng)下, 見 ffmpeg 的編譯過(guò)程(編譯完成后可執(zhí)行自動(dòng)安裝)。
二、準(zhǔn)備預(yù)播放的媒體文件
如 test.Mp3,在本文檔中,默認(rèn)放入用戶文件夾下得 Music 文件夾內(nèi).(直接從設(shè)備采 集不在本文檔敘述范圍之內(nèi))
三、修改 ffserver 配置信息
ffserver 配置文件為: /etc/ffserver.conf 打開, 填寫配置信息.配置信息包括三方面:
- 1.端口綁定等基本信息,在/etc/ffserver.conf 中有詳細(xì)注釋,在此不再重復(fù),最終配 置信息為:
| Port 8090 |
|---|
| BindAddress 0.0.0.0 |
| MaxHTTPConnections 2000 |
| MaxClients 1000 |
| MaxBandwidth 1000 |
-
2.媒體文件配置信息.本信息根據(jù)具體的媒體文件類型直接在配置文件中取消注釋 掉相應(yīng)文件類型的配置信息, 然后填寫文件路徑即可:
# MP3 audio <Stream test.mp3> File "/home/xiaoma/Music/test.mp3" Format mp2 NoVideo </Stream>
四、啟動(dòng) ffserver
在終端中運(yùn)行如下代碼來(lái)啟動(dòng)ffserver
sudo ffserver -f /etc/ffserver.conf
五、播放流媒體
在瀏覽器中輸入http://127.0.0.1:8090/test.mp3即可播放音樂(lè).
在終端中輸入 ffplay http://localhost:8090/test.mp3 可播放流媒體.
2.5.2 用 FFserver 從設(shè)備生成實(shí)時(shí)流
一、準(zhǔn)備媒體
按照上節(jié)步驟安裝 ffmpeg,保證攝像頭和聲卡可用, 將從攝像頭和聲卡獲取音視頻 信息。
二、修改 ffserver 配置信息
ffserver 配置文件為: /etc/ffserver.conf 打開, 填寫配置信息.配置信息包括三方面:
- 1.端口綁定等基本信息,在/etc/ffserver.conf 中有詳細(xì)注釋,在此不再重復(fù),最終配 置信息為:
| Port 8090 |
|---|
| BindAddress 0.0.0.0 |
| MaxHTTPConnections 2000 |
| MaxClients 1000 |
| MaxBandwidth 1000 |
-
2.fend(傳沖信息),在文件播放中,基本不用動(dòng)本配置信息,只需要根據(jù)具體情況分 配緩沖文件.最終配置信息如下:
<Feed feed1.ffm> File /tmp/feed1.ffm FileMaxSize 2M ACL allow 127.0.0.1 </Feed>
(中間會(huì)有很多很多配置信息,都是關(guān)于音視頻的,有些配置還不懂,慢慢摸索吧)
<Stream test1.mpg>
Feed feed1.ffm
Format mpeg
AudioBitRate 32
AudioChannels 1
AudioSampleRate 44100
VideoBitRate 64
VideoBufferSize 40
VideoFrameRate 3
VideoSize 160x128
VideoGopSize 12
</Stream>
# Flash
<Stream test.swf>
Feed feed1.ffm
Format swf
VideoFrameRate 2
VideoIntraOnly
NoAudio
</Stream>
三、啟動(dòng) FFserver
在終端中運(yùn)行如下代碼來(lái)啟動(dòng) ffserver.
sudo ffserver -f /etc/ffserver.conf
四、啟動(dòng) ffmpeg
本例中, 以 ffmpeg 作為實(shí)時(shí)攝像頭采集輸入.在命令行中輸入:
./ffmpeg -f video4linux2 -r 25 -i /dev/video0 /tmp/feed1.ffm
如果有音頻設(shè)備, 則采集音頻的命令如下:
./ffmpeg -f oss -i /dev/dsp -f video4linux2 -r 25 -i /dev/video0 /tmp/feed1.ffm
(音頻格式參數(shù)自己配置)
五、播放流媒體
在瀏覽器中輸入 http://127.0.0.1:8090/test1.mpg 即可播放音樂(lè).
在終端中輸入 ffplay http://localhost:8090/test.swf 可播放流媒體.
寫在后面
如果對(duì)FFmpeg有需要更多了解的請(qǐng)訂閱我的專題:音視頻專輯