1. ffmpeg實(shí)現(xiàn)視頻區(qū)域模糊效果
ffmpeg -i test.mp4 -filter_complex "[0:v]crop=200:200:300:350,boxblur=10[fg]; [0:v][fg]overlay=300:350[v]" -map "[v]" output.mp4
- 模糊區(qū)域是200*200
- 模糊區(qū)域左上角的坐標(biāo)是x=300 y=350
- boxblur代表的是模糊系數(shù)。它的默認(rèn)值是2,這個(gè)值越大,就越模糊。
2. 將視頻中指定區(qū)域之外的部分進(jìn)行模糊
ffmpeg -i test.mp4 -filter_complex "[0:v]boxblur=10[bg];[0:v]crop=200:200:300:350[fg];[bg][fg]overlay=300:350" -map 0:v output.mp4
3. 轉(zhuǎn)換視頻格式,從mp4轉(zhuǎn)換成avi
ffmpeg -i test.mp4 test.avi
4. 視頻截取
ffmpeg -ss 00:00:10 -to 00:00:30 -i test.mp4 -vcodec copy -acodec copy output.mp4
- -ss截取開始時(shí)間:00:00:10
- -to截取結(jié)束時(shí)間:00:00:30
- -vcodec copy:視頻編碼處理方式,copy:不變
- -acodec copy:音頻編碼處理方式,copy:不變
4. 視頻拼接
用一個(gè)txt文件將需要拼接在一起的視頻文件組織起來,格式是
file 'test1.mp4'
file 'test2.mp4'
file 'test3.mp4'
有了上面的文本文件,就可以使用ffmpeg進(jìn)行拼接了,命令是:
ffmpeg -f concat -i filelist.txt -v:c copy output.mp4
5. 更改視頻的分辨率
將原視頻中的分辨率更改為1280x720,音頻編碼方式不變
ffmpeg -i test.mp4 -filter:v scale=1280:720 -c:a copy output.mp4
如果還想更改視頻比例的話,可以這樣
ffmpeg -i test.mp4 -vf scale=1280x720,setdar=16:9 output.mp4
6. 改變音視頻的編碼方式
ffmpeg -i test.avi -c:v libx264 -c:a aac output.mp4
7. 提取視頻中的音頻
使用參數(shù)-vn,就是disable video的意思
ffmpeg -i test.mp4 -vn test.mp3
對(duì)應(yīng)的,如果想去掉音頻,使用參數(shù)-an
ffmpeg -i test.mp4 -an output.mp4
8. 提取圖像
ffmpeg -i test.mp4 -r 1 -f image2 image_%2d.png
其中-r表示幀速度,-r 1就是每一秒提取一張圖片的意思,提取出來的圖片命名格式是image_01.png、image_02.png、image_03.png,以此類推。
9. m3u8切片
m3u8非常適合在網(wǎng)絡(luò)上播放,ffmpeg可以將本地視頻文件轉(zhuǎn)化成m3u8索引加ts視頻的組合。這樣,只要將生成的文件拷貝到類似nginx這樣的web服務(wù)上,就可以實(shí)現(xiàn)視頻點(diǎn)播了。
ffmpeg -i test.mp4 -vcodec libx264 -strict -2 -acodec aac -hls_list_size 0 -f hls index.m3u8
10. 調(diào)整視頻播放速度
下面的示例是2倍速的轉(zhuǎn)換,如果是原來的1/2速度,則參數(shù)變?yōu)?code>setpts=2*PTS
ffmpeg -i test.mp4 -vf "setpts=0.5*PTS" output.mp4
11. 修改視頻的封面
ffmpeg -i test.mp4 -i logo.png -map 1 -map 0 -c copy -disposition:0 attached_pic output.mp4
12. 圖片水印
ffmpeg -i test.mp4 -vf "movie=logo1.png[watermark];[in][watermark] overlay=main_w-overlay_w:main_h-overlay_h-30[out] " output.mp4
13. 淡入淡出
從第0幀開始直到第100幀
ffmpeg -i test.mp4 -vf fade=in:0:50 output.mp4
13. 視頻水平翻轉(zhuǎn)
ffmpeg -i test.mp4 -vf "hflip" output.mp4
13. 視頻垂直翻轉(zhuǎn)
ffmpeg -i test.mp4 -vf "vflip" output.mp4
14. 三段分屏
水平方向:
ffmpeg.exe -i test.mp4 -i test.mp4 -i test.mp4 -filter_complex "[0:v]pad=iw*3:ih[a];[a][1:v]overlay=w[b];[b][2:v]overlay=w*2" output.mp4
其中,[0:v]pad=iw*3:ih[a]指定占用3倍原始視頻的寬度,高度不變,[a]為代號(hào),濾波器疊加從左到右。[1:v]overlay=w[b]指定從w處開始疊加,也就是第二個(gè)視頻的位置,依次類推。
垂直方向,原理類似:
ffmpeg.exe -i test.mp4 -i test.mp4 -i test.mp4 -filter_complex "[0:v]pad=iw:ih*3[a];[a][1:v]overlay=0:h*2[b];[b][2:v]overlay=0:h" output.mp4
15. 區(qū)域視頻剪輯
400:200:100:100代表從(100,100)位置裁剪寬為400,高為200的區(qū)域
ffmpeg -i test.mp4 -filter:v "crop=400:200:100:100" output.mp4
16. 畫中畫
其中test.mp4是畫面視頻,scale設(shè)置大小,overlay設(shè)置位置
ffmpeg -i test.mp4 -i test.mp4 -filter_complex "[1:v]scale=w=176:h=144:force_original_aspect_ratio=decrease[ckout];[0:v][ckout]overlay=x=W-w-10:y=0[out]" -map "[out]" -movflags faststart output.mp4
17. 實(shí)現(xiàn)跑馬燈效果
從左往右:
ffmpeg -i test.mp4 -vf "drawtext=text=Hello world:expansion=normal:fontfile=arial.ttf: y=h-line_h-50:x=(mod(5*n,w+tw)-tw): fontcolor=white: fontsize=40" output.mp4
從右往左
ffmpeg -i test.mp4 -filter:v drawtext="fontfile=arial.ttf:text='Hello World':fontcolor=white@1.0:fontsize=100:y=300:x=w-(t-1)*mod(t,5)*w/20:enable=gt(mod(t,20),0)" output.mp4
使用漢字
這里需要使用系統(tǒng)中可以顯示中文的字體。在windows 10系統(tǒng)中,字體文件存放的路徑是C:\Windows\Fonts,右鍵點(diǎn)擊想要使用的字體文件,選擇屬性,復(fù)制字體名稱
ffmpeg -i test.mp4 -vf "drawtext=text=你好,迷途小書童!:expansion=normal:fontfile=/Windows/Fonts/simhei.ttf: y=h-line_h-50:x=(mod(5*n,w+tw)-tw): fontcolor=white: fontsize=40" output.mp4