ffmpeg 之亂七八糟命令

如果是選擇的圖片背景:

  1. 將圖片變成視頻大小,例如視頻 360480 則取480, 720480 則取 720.
    ffmpeg -i tp.jpg -vf scale=480:480 tp.jpg
    ffmpeg -i cut.mp4 -vf 'movie=bg_out_image.png [bg]; [bg][in] overlay=(W-w)/2:(H-h)' -y out.mp4

如果是顏色背景
顏色也將它存為圖片格式,然后將它scale到視頻最大寬高
ffmpeg -i tp.jpg -vf scale=480:480 tp.jpg
ffmpeg -i video.mp4 -vf "movie=x.png [bg]; [bg][in] overlay=(W-w)/2:(H-h)" -y out.mp4

如果是視頻高斯模糊效果 boxblur=1:5 清晰 boxblur=20:5 模糊
ffmpeg -i video.mp4 -vf "split[a][b];[a]scale=480x480,setdar=1:1,boxblur=10:5[1];[b]scale=224x480[2];[1][2]overlay=(W-w)/2"
-y out.mp4

ffmpeg -i cut.mp4 -vf 'split[a][b];[a]scale=960x1280,setdar='r=3/4',boxblur=10:5[1];[b]scale=960x540[2];[1][2]overlay=0:(H-h)/2' -y bg_video.mp4

如果僅僅單純改變一下比例?
默認指定黑色背景作為它的背景 !!

給視頻添加文本和圖片,可以用一個命令搞定嗎?

ffmpeg -i edit.mp4 -vf "drawtext=fontcolor=white:fontsize=28:fontfile=font.ttf:text='中國你好':x=50:y=50[text];movie=shuiying1.png[wm];[text][wm]overlay=0:0[out]" xx.mp4

同時添加文本和圖片!!!

ffmpeg -i edit.mp4 -vf "
drawtext=enable='between(t,0,2)':fontcolor=white:fontsize=28:fontfile=font.ttf:text='中國你好':x=0:y=0[text1],
movie=shuiying1.png[wm1];[text1][wm1]overlay=30:30:enable='between(t,0,2)'[out1],
[out1]
drawtext=enable='between(t,2,4)':fontcolor=white:fontsize=28:fontfile=font.ttf:text='xxx':x=0:y=0[text2],
movie=shuiying1.png[wm2];[text2][wm2]overlay=40:60:enable='between(t,2,3)'[out2]
" xx.mp4

ffmpeg -i edit.mp4 -vf "drawtext=fontcolor=white:fontsize=28:fontfile=font.ttf:text='中國你好':x=50:y=50" xx.mp4

  1. FFmpeg 命令
    448:960
    ///-r 0.1 表示10秒鐘截圖一次
    String command = "-i " + inputVideoPath + " -r 0.1 -f image2 $cachePath/image-%05d.jpeg";
///壓縮視頻
String command = "-i " + inputVideoPath + " -b:v 30000k -s 216x384 " + compressVideoPath;

///添加水印
String command2 = "-i " +
    compressVideoPath +
    " -i " +
    waterMarkPath2 +
    " -i " +
    waterMarkPath +
    " -filter_complex overlay=10:10,overlay=5:5 " +
    outVideoPath +
    "";

指定時間加入水印
ffmpeg -i yasuo.mp4 -i test.png -i kongbai.png -filter_complex overlay=0:0:enable='between(t,3,5)',overlay=10:0:enable='between(t,5,8)' out.mp4

-i edit.mp4 -i test.png -i test.png -filter_complex "[1:v]scale=110.8910891089109:110.8910891089109[img1],[2:v]scale=237.30693069306932:169.6633663366337[img2],
[0:v][img1]overlay=77:100[v1],[v1][img2]overlay=18:348" -q 0 -y gt_process_done_video.mp4

-i input.mp4 -i test.png -filter_complex "[1:v]scale=100.0:100.0[img1],[0:v][img1]overlay=108.56615193684901:343.51793416341184[v1]" -q 0 -y gt_process_done_video.mp4

///加入多個水印,并且改變水印大小和顯示時間
ffmpeg -i input.mp4 -i test.png -i test2.png -i test3.png -filter_complex
"[1:v]scale=50:50[img1],[2:v]scale=50:50[img2],[3:v]scale=-1:-1[img3],
[0:v][img1]overlay=0:0:enable='between(t,3,5)'[v1],
[v1][img2]overlay=10:0:enable='between(t,5,8)'[v2],
[v2][img3]overlay=70:0:enable='between(t,5,8)'"
-y out.mp4

ffmpeg -i edit.mp4 -i 1.jpg -filter_complex "[1:v]scale=110:110[img1],\
[0:v][img1]overlay=77:100:enable='between(t,3,5)'" \
-q 0 -y gt_process_done_video.mp4

ffmpeg -i input.mp4 -i test.png -i test2.png -filter_complex "[1:v]scale=50:50[img1];[2:v]scale=40:40[img2];[0:v][img1]overlay=2:3[bkg];[bkg][img2]overlay=40:40" -q 0 -y out.mp4

///改變視頻到指定尺寸!!!
ffmpeg -i mycut.mp4 -vf scale=300:400 scale.mp4

ffmpeg -i video.mp4 -vcodec h264 -vf "scale='max(480,iw*0.5)':-1" -r 15 -acodec libmp3lame -ac 2 -ar 22050 -f mp4 -y 3.mp4

ffmpeg -i tp1.jpg -vf drawtext=fontcolor=white:fontsize=28:fontfile=font.ttf:text='中國
你好':x=50:y=50 -y tp2.jpg

電腦端命令~~~~
ffmpeg -i /Users/mac/Desktop/ffmpge/input.mp4 -vf "drawtext=fontcolor=white:fontsize=40:fontfile=/Users/mac/Desktop/ffmpge/gt_video_font.ttf:text='ceshi':x=50:y=50:enable='lte(t,5)'" -y /Users/mac/Desktop/ffmpge/out.mp4

ffmpeg -i /Users/mac/Desktop/ffmpge/input.mp4 -vf "drawtext=fontcolor=white:fontsize=40:fontfile=/Users/mac/Desktop/ffmpge/gt_video_font.ttf:text='ceshi':x=50:y=50'" -q 0 -y /Users/mac/Desktop/ffmpge/out.mp4

///添加一個文本
ffmpeg -i /Users/mac/Desktop/ffmpge/video.mp4 -vf drawtext=enable='between(t,3,5)':fontcolor=white:fontsize=40:fontfile=/Users/mac/Desktop/ffmpge/font/youyuan.ttf:text='ceshi':x=50:y=50 -y /Users/mac/Desktop/ffmpg/out.mp4

ffmpeg -i tp1.jpg -vf drawtext=fontcolor=white:fontsize=28:fontfile=font.ttf:text='中國你好':x=50:y=50 -y tp2.jpg

///一句命令給視頻添加多個text
ffmpeg -i /Users/mac/Desktop/ffmpge/video.mp4 -vf "drawtext=enable='between(t,3,5)':fontcolor=white:fontsize=40:fontfile=/Users/mac/Desktop/ffmpge/font/youyuan.ttf:text='ceshi':x=50:y=50, drawtext=enable='between(t,7,10)':fontcolor=white:fontsize=40:fontfile=/Users/mac/Desktop/ffmpge/font/youyuan.ttf:text='haha':x=50:y=50" -y /Users/mac/Desktop/ffmpge/out.mp4

///音頻部分
http://ffmpeg.org/ffmpeg-filters.html#Audio-Sources

///將mp4提取mp3.
ffmpeg -i /Users/mac/Desktop/ffmpge/video.mp4 -f mp3 -vn /Users/mac/Desktop/ffmpge/apple.mp3

///實現(xiàn)混音效果

ffmpeg -i yasuo.mp4 -stream_loop -1 -i duan_music.mp3 -filter_complex
"[0:a]volume=0.2[a0];
[1:a]volume=0.8[a1];
[a0][a1]amix=inputs=2[a]"
-map 0:v -map "[a]" -c:v copy -c:a aac -shortest -t 11 -y output.mp4

分離視頻中的音頻 和 視頻,這時候視頻沒聲音
ffmpeg -i video.mp4 -f mp3 -vn gt_video_audio.mp3 -an gt_video.mp4

獲取視頻中的音頻
ffmpeg -i input.mp4 -vn -y -acodec copy output.m4a

將聲音變成0.5
ffmpeg -i input.wav -filter:a "volume=0.5" output.wav

合并兩個音頻
ffmpeg -i bgmusic.mp3 -i gt_video_audio.mp3 -filter_complex amix=inputs=2:duration=first:dropout_transition=2 gt_audio.mp3

ffmpeg -i bgmusic.mp3 -i gt_video_audio.mp3 -filter_complex amerge -ac 2 -c:a libmp3lame -q:a 4 gt_audio.mp3

ffmpeg -i bgmusic.mp3 -i gt_video_audio.mp3 -filter_complex amix=inputs=2:duration=first -c:a libmp3lame -q:a 4 gt_audio.mp3

去掉視頻中的音頻
ffmpeg -i input.mp4 -an output.mp4

合并音頻和視頻
ffmpeg -i video.mp4 -i audio.wav -c:v copy -c:a aac -strict experimental output.mp4

水印相關(guān)
指定長,高度按比例縮放:
ffmpeg -i input.jpg -vf scale=320:-1 output_320x240.png

///加入水印到視頻, 并且改變水印大小
ffmpeg -i input.mp4 -i test.png -filter_complex "[1:v]scale=80:80[test];[test]overlay=x=20:y=0" -c:v h264 out.mp4

重要參數(shù)講解
-vf 視頻的過濾器
-filter_complex graph_description創(chuàng)建一個復(fù)雜的filter graph.

  1. 將一張png 或者 gif 轉(zhuǎn)成mp4 .
    方案1 ->>>>>>>>>>
    ///將視頻轉(zhuǎn)成gif https://blog.csdn.net/ternence_hsu/article/details/87714244
    ffmpeg -r 28 -i pianwei.gif -pix_fmt yuv420p -s 380x474 video_gif.mp4
  1. 給一個視頻追加片尾mp4, list.txt 里面格式
    方案1 ->>>>>>>>>>
    /// 如果加上 -vcodec copy -acodec copy 速度更快!!這個是直接拷貝視頻數(shù)據(jù) -c copy
    /// -r 是針對基調(diào)不一樣的兩個視頻,很重要, 如果沒加會出現(xiàn)花屏或者第一個視頻播放完等半天才播放第二個,-r 多少可以看下fps
    ffmpeg -r 25 -f concat -i list.txt -vcodec copy -acodec copy -y concat.mp4 上面這個需要格式啥的完全一致
    ffmpeg -r 25 -f concat -i list.txt -y concat.mp4

  2. 給視頻添加一個背景,
    方案1 ->>>>>>>>>>
    ffmpeg -loop 1 -i icon_video_style_big1.png -i video.mp4 -filter_complex "overlay=(W-w)/2" -shortest -y output.mp4
    方案2 ->>>>>>>>>>
    ffmpeg -i video.mp4 -vf "movie=x.png [bg]; [bg][in] overlay=(W-w)/2:(H-h)" -y out.mp4

  3. 高斯模糊視頻

方案1 ->>>>>>>>>>
ffmpeg -i video1.mp4 -vf "split[a][b];[a]scale=1024:768,boxblur=10:5[1];[b]scale=(iw*768)/ih:768[2];[1][2]overlay=(W-w)/2" 2.mp4

方案2 ->>>>>>>>>> :224x480 改變分辨率和寬高比例
ffmpeg -i video.mp4 -vf "split[a][b];[a]scale=480x480,setdar=1:1,boxblur=20:5[1];[b]scale=224x480[2];[1][2]overlay=(W-w)/2" -y out.mp4

  1. 修改視頻尺寸

范例:

?設(shè)定顯示比例為 16:9
setdar='r=16/9'

setdar='dar=16/9'

?設(shè)定像素比例為 40:33
setsar='r=40/33'

setsar='sar=40/33'
說明: >> setdar 是修改尺,如果不改尺寸 單獨修改寬高視頻不會變化多少
ffmpeg -i video_tail.mp4 -vf scale=960:540,setdar='r=16/9' -y tt.mp4

  1. 拷貝某一段
    ffmpeg -ss 2 -to 4 -accurate_seek -i source.mp4 -codec copy -avoid_negative_ts 1 -y xx.mp4
  1. 給圖片添加文字

(1) 改變圖片尺寸!
ffmpeg -i tp.jpg -vf scale=400:-1 tp1.jpg
(2) 圖片添加文字

  1. 創(chuàng)建一個純色視頻 并繪制顏色
    (1)

///創(chuàng)建一個純色的視頻
ffmpeg -f lavfi -i color=c=0x22000000:s=224x480:d=5 output.mp4

///將創(chuàng)建的純色視頻跟原始視頻進行合并.產(chǎn)生新的視頻
ffmpeg -i video.mp4 -i out.mp4 -filter_complex "[0:v]format=yuva444p,colorchannelmixer=aa=0[valpha];[1:v][valpha]overlay=(W-w)/2:(H-h)/2" -y overlay4.mp4

ffmpeg -i output.mp4 -vf "drawtext=fontcolor=white:fontsize=40:fontfile=font.ttf:text='我是測試文本':x=50:y=50" -y out.mp4

參考文檔:
https://ffmpeg.org/ffmpeg-filters.html#drawtext-1
https://www.iteye.com/blog/keren-1773536

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容