ffmpeg命令使用

19 ffmpeg commands for all needs


ffmpeg is a multiplatform, open-source library for video and audio files. I have compiled 19 useful and amazing commands covering almost all needs: video conversion, sound extraction, encoding file for iPod or PSP, and more.

Getting infos from a video file
ffmpeg -i video.avi
Turn X images to a video sequence
ffmpeg -f image2 -i image%d.jpg video.mpg

This command will transform all the images from the current directory (named image1.jpg, image2.jpg, etc…) to a video file named video.mpg.

Turn a video to X images
ffmpeg -i video.mpg image%d.jpg

This command will generate the files named image1.jpg, image2.jpg, …

The following image formats are also availables : PGM, PPM, PAM, PGMYUV, JPEG, GIF, PNG, TIFF, SGI.

Encode a video sequence for the iPpod/iPhone
ffmpeg -i source_video.avi input -acodec aac -ab 128kb -vcodec mpeg4 -b 1200kb -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4

Explanations :

  • Source : source_video.avi
  • Audio codec : aac
  • Audio bitrate : 128kb/s
  • Video codec : mpeg4
  • Video bitrate : 1200kb/s
  • Video size : 320px par 180px
  • Generated video : final_video.mp4
Encode video for the PSP
ffmpeg -i source_video.avi -b 300 -s 320x240 -vcodec xvid -ab 32 -ar 24000 -acodec aac final_video.mp4

Explanations :

  • Source : source_video.avi
  • Audio codec : aac
  • Audio bitrate : 32kb/s
  • Video codec : xvid
  • Video bitrate : 1200kb/s
  • Video size : 320px par 180px
  • Generated video : final_video.mp4
Extracting sound from a video, and save it as Mp3
ffmpeg -i source_video.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 sound.mp3

Explanations :

  • Source video : source_video.avi
  • Audio bitrate : 192kb/s
  • output format : mp3
  • Generated sound : sound.mp3
Convert a wav file to Mp3
ffmpeg -i son_origine.avi -vn -ar 44100 -ac 2 -ab 192 -f mp3 son_final.mp3
Convert .avi video to .mpg
ffmpeg -i video_origine.avi video_finale.mpg
Convert .mpg to .avi
ffmpeg -i video_origine.mpg video_finale.avi
Convert .avi to animated gif(uncompressed)
ffmpeg -i video_origine.avi gif_anime.gif
Mix a video with a sound file
ffmpeg -i son.wav -i video_origine.avi video_finale.mpg
Convert .avi to .flv
ffmpeg -i video_origine.avi -ab 56 -ar 44100 -b 200 -r 15 -s 320x240 -f flv video_finale.flv
Convert .avi to dv
ffmpeg -i video_origine.avi -s pal -r pal -aspect 4:3 -ar 48000 -ac 2 video_finale.dv

Or:

ffmpeg -i video_origine.avi -target pal-dv video_finale.dv
Convert .avi to mpeg for dvd players
ffmpeg -i source_video.avi -target pal-dvd -ps 2000000000 -aspect 16:9 finale_video.mpeg

Explanations :

  • target pal-dvd : Output format
  • ps 2000000000 maximum size for the output file, in bits (here, 2 Gb)
  • aspect 16:9 : Widescreen
Compress .avi to divx
ffmpeg -i video_origine.avi -s 320x240 -vcodec msmpeg4v2 video_finale.avi
Compress Ogg Theora to Mpeg dvd
ffmpeg -i film_sortie_cinelerra.ogm -s 720x576 -vcodec mpeg2video -acodec mp3 film_terminée.mpg
Compress .avi to SVCD mpeg2

NTSC format:

ffmpeg -i video_origine.avi -target ntsc-svcd video_finale.mpg

PAL format:

ffmpeg -i video_origine.avi -target pal-svcd video_finale.mpg
Compress .avi to VCD mpeg2

NTSC format:

ffmpeg -i video_origine.avi -target ntsc-vcd video_finale.mpg

PAL format:

ffmpeg -i video_origine.avi -target pal-vcd video_finale.mpg
Multi-pass encoding with ffmpeg
ffmpeg -i fichierentree -pass 2 -passlogfile ffmpeg2pass fichiersortie-2

Ffmpeg快速命令使用

Ffmpeg使用語法

ffmpeg [[options][`-i' input_file]]... {[options] output_file}...

如果沒有輸入文件,那麼視音頻捕捉(只在Linux下有效,因爲Linux下把音視頻設備當作文件句柄來處理)就會起作用。作爲通用的規則,選項一般用於下一個特定的文件。如果你給 –b 64選項,改選會設置下一個視頻速率。對於原始輸入文件,格式選項可能是需要的。缺省情況下,ffmpeg試圖儘可能的無損轉換,採用與輸入同樣的音頻視頻參數來輸出。


ffmpeg轉換所涉及到的選項較多,可參考ffmpeg選項詳解


ffmpeg支持多種文件格式和多種音頻、視頻編碼器,可參考ffmepg格式詳解,(附:常見視頻文件格式詳解

[編輯]視頻文件截圖

  • 截取一張352x240尺寸大小的,格式爲jpg的圖片
ffmpeg -i test.asf -y -f image2 -t 0.001 -s 352x240 a.jpg
  • 把視頻的前30幀轉換成一個Animated Gif
ffmpeg -i test.asf -vframes 30 -y -f gif a.gif
  • 截取指定時間的縮微圖
ffmpeg -i test.avi -y -f image2 -ss 8 -t 0.001 -s 350x240 test.jpg
-ss後跟的時間單位爲秒
  • 轉換文件爲3GP格式
ffmpeg -y -i test.mpeg -bitexact -vcodec h263 -b 128 -r 15 -s 176x144 
 -acodec aac -ac 2 -ar 22500 -ab 24 -f 3gp test.3gp
ffmpeg -y -i test.wmv -ac 1 -acodec libamr_nb -ar 8000 -ab 12200 -s 176x144 -b 128 -r 15 test.3gp

[編輯]視頻格式轉換

  • 如何使用 ffmpeg 編碼得到高質量的視頻
ffmpeg.exe -i "D:\Video\Fearless\Fearless.avi" -target film-dvd -s 720x352
 -padtop 64 -padbottom 64 -maxrate 7350000 -b 3700000 -sc_threshold 1000000000 
 -trellis -cgop -g 12 -bf 2 -qblur 0.3 -qcomp 0.7 -me full -dc 10 -mbd 2
 -aspect 16:9 -pass 2 -passlogfile "D:\Video\ffmpegencode" -an -f mpeg2video "D:\Fearless.m2v"
  • 轉換指定格式文件到FLV格式
ffmpeg.exe -i test.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv 
ffmpeg.exe -i test.wmv -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:\test.flv
  • 轉碼解密的VOB
ffmpeg -i snatch_1.vob -f avi -vcodec mpeg4 -b 800 -g 300 -bf 2 -acodec mp3 -ab 128 snatch.avi
上面的命令行將vob的文件轉化成avi文件,mpeg4的視頻和mp3的音頻。注意命令中使用了B幀,所以mpeg4流是divx5兼容的。GOP大小是300意味着29.97幀頻下每10秒就有INTRA幀。該映射在音頻語言的DVD轉碼時候尤其有用。
  • 同時編碼到幾種格式並且在輸入流和輸出流之間建立映射
ffmpeg -i /tmp/a.wav -ab 64 /tmp/a.mp2 -ab 128 /tmp/b.mp2 -map 0:0 -map 0:0
上面的命令行轉換一個64Kbits 的a.wav到128kbits的a.mp2 ‘-map file:index’在輸出流的順序上定義了哪一路輸入流是用於每一個輸出流的。
  • 轉換文件爲3GP格式
ffmpeg -i test.avi -y -b 20 -s sqcif -r 10 -acodec amr_wb -ab 23.85 -ac 1 -ar 16000 test.3gp
注:如果要轉換爲3GP格式,則ffmpeg在編譯時必須加上–enable-amr_nb –enable-amr_wb,詳細內容可參考:轉換視頻爲3GPP格式
  • 轉換文件爲MP4格式(支持iPhone/iTouch)
ffmpeg  -y  -i input.wmv  -f mp4 -async 1-s 480x320  -acodec libfaac -vcodec libxvid  -qscale 7 -dts_delta_threshold 1 output.mp4
ffmpeg  -y  -i source_video.avi input -acodec libfaac -ab 128000 -vcodec mpeg4 -b 1200000 -mbd 2 -flags +4mv+trell -aic 2 -cmp 2 -subcmp 2 -s 320x180 -title X final_video.mp4
  • 將一段音頻與一段視頻混合
ffmpeg -i son.wav -i video_origine.avi video_finale.mpg
  • 將一段視頻轉換爲DVD格式
ffmpeg -i source_video.avi -target pal-dvd -ps 2000000000 -aspect 16:9 finale_video.mpeg
注:target pal-dvd : Output format ps 2000000000 maximum size for the output file, in bits (here, 2 Gb) aspect 16:9 : Widescreen
  • 轉換一段視頻爲DivX格式
ffmpeg -i video_origine.avi -s 320x240 -vcodec msmpeg4v2 video_finale.avi
  • Turn X images to a video sequence
ffmpeg -f image2 -i image%d.jpg video.mpg
注:This command will transform all the images from the current directory (named image1.jpg, image2.jpg, etc...) to a video file named video.mpg.
  • Turn a video to X images
ffmpeg -i video.mpg image%d.jpg
注:This command will generate the files named image1.jpg, image2.jpg, ...
The following image formats are also availables : PGM, PPM, PAM, PGMYUV, JPEG, GIF, PNG, TIFF, SGI.

[編輯]屏幕錄製

  • 使用ffmpeg錄像屏幕
ffmpeg -vcodec mpeg4 -b 1000 -r 10 -g 300 -vd x11:0,0 -s 1024x768 ~/test.avi

:其中,-vd x11:0,0 指錄製所使用的偏移爲 x=0 和 y=0,-s 1024×768 指錄製視頻的大小爲 1024×768。錄製的視頻文件爲 test.avi,將保存到用戶主目錄中

如果你只想錄製一個應用程序窗口或者桌面上的一個固定區域,那麼可以指定偏移位置和區域大小。使用xwininfo -frame命令可以完成查找上述參數。


  • 重新調整視頻尺寸大小
ffmpeg -vcodec mpeg4 -b 1000 -r 10 -g 300 -i ~/test.avi -s 800×600 ~/test-800-600.avi
注:ffmpeg的屏幕錄製功能只能在Linux環境下有效。

[編輯]視頻採集

  • 把攝像頭的實時視頻錄製下來,存儲爲文件
ffmpeg  -f video4linux -s 320*240 -r 10 -i /dev/video0  test.asf
更多信息可參考ffmpeg工程組論壇的討論貼:有關ffmpeg的視頻採集
注:ffmpeg的視頻採集功能只能在Linux環境下使用

[編輯]使用ffmpeg壓制H.264視頻

ffmpeg -threads 4 -i INPUT -r 29.97 -vcodec libx264 -s 480x272 -flags +loop -cmp +chroma -deblockalpha 0 -deblockbeta 0 -crf 24 
-bt 256k -refs 1 -coder 0 -me umh -me_range 16 -subq 5 -partitions +parti4x4+parti8x8+partp8x8 -g 250 -keyint_min 25 -level 30 
-qmin 10 -qmax 51 -trellis 2 -sc_threshold 40 -i_qfactor 0.71 -acodec libfaac -ab 128k -ar 48000 -ac 2 OUTPUT
注:使用該指令可以壓縮出比較清晰,而且文件轉小的H.264視頻文件

使用VHook爲視頻添加水印

[編輯]使用ffmpeg獲取PCM數據

ffmpeg -i input.mpg -f s16le -ar 44100 -acodec pcm_s16le output.pcm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章