2020年3月16日 星期一

ffmpeg 常用命令

ffmpeg 基本參數:
-y : 如果輸出檔案已存在,不詢問直接覆蓋
-n : 如果輸出檔案已存在,不詢問直接忽略不覆蓋

注意:
如果輸出檔案跟輸入檔案相同,
在為 video 檔案時,-y 會失效,
會印出
FFmpeg cannot edit existing files in-place.
錯誤

ffmpeg 常用命令:
=============================================================
將圖或影片 Resize 成特定尺寸:
Ex : 將圖或影片 resize 成 320x240
D:\\ffmpeg\\bin\\ffmpeg -i "input.jpg" -vf scale=320:240 "output_320x240.png"
** 注意:
因為 h264 encoder 規定輸出影片長寬須為偶數,
如指定奇數會有 "ffmpeg height not divisible by 2" 的錯誤訊息,可將指令改成:
D:\\ffmpeg\\bin\\ffmpeg -i "input.jpg" -vf scale=floor(321/2)*2:floor(241/2)*2 "output_321x241.png"

=============================================================
將圖或影片在保持長寬比 ration 的情況下 Resize 到能塞到特別尺寸的矩形中,
如原圖檢來就可塞得下,就不 resize
Ex: 將圖或影片保持長寬比並 resize 至塞得下 300x500 的矩形中
D:\ffmpeg\bin\ffmpeg -i "input.jpg" -vf "scale='min(iw, 300)':'min(ih, 500)':force_original_aspect_ratio=decrease" "output_boundedBy_300x500.png"
** 注意:
因為 h264 encoder 規定輸出影片長寬須為偶數,
如指定奇數會有 "ffmpeg height not divisible by 2" 的錯誤訊息,可將指令改成:
D:\ffmpeg\bin\ffmpeg -i "input.jpg" -vf "scale='floor(iw*min(1,min(301/iw,501/ih))/2)*2':'floor(ih*min(1,min(301/iw,501/ih))/2)*2'" "output_boundedBy_301x501.png"

參考:

  1. FFMPEG (libx264) “height not divisible by 2”
  2. ffmpeg转码遇见错误height not divisible by 2的解决方案
  3. [FFmpeg] error about width or height not divisible by 2
=============================================================
將影片壓縮
D:\\ffmpeg\\bin\\ffmpeg -i "inputVideo.mp4" -r 10 -b:a 32k "outputVideo.mp4"
=============================================================
得到影片中,特定秒數幀(frame)的圖片
Ex: 取得 22:11:00 的 frame
D:\\ffmpeg\\bin\\ffmpeg -i "inputVideo.mp4" -y -f  image2  -ss 22:11:00 -vframes 1 "outputFrame.jpg"
=============================================================
將影片中的 "影片資訊 (moov box 或稱 movie box)" 放到檔案二進位較前的位置,讓例如瀏覽器能夠先讀到馬上開始邊播放邊下載影片 (通常瀏覽器需得到如影片長度等資訊後才會開始播放影片),
而不用等到讀到 "影片資訊 (moov box)" 後才開始播放
參考 : MP4 moov box解析
D:\\ffmpeg\\bin\\ffmpeg -i "inputVideo.mp4" -movflags faststart -acodec copy -vcodec copy "outputVideo.mp4"
=============================================================
取得資源 (影片檔或圖片檔,也可為外部網址 url),的資訊,並以 json 格式輸出,
例如長、寬、影片長度等
D:\\ffmpeg\\bin\\ffprobe -show_streams -print_format json "imageOrVideoUrl"
=============================================================
對圖片或影片進行截剪
Ex: 將圖片在 x=135, y=75 的位置(以左上為原點,向右向下為正),以width=270, height = 150 進行截剪
D:\\ffmpeg\\bin\\ffmpeg -i "input.jpg" -vf crop=270:150:135:75 "out.jpg"
=============================================================
將GIF動圖的某個幀(frame)取出,
或者是將所有幀全數取出 (輸出檔名用 %d ,之後會得到多個圖,檔名中的 %d 會被幀數代替 )
Ex:
D:\\ffmpeg\\bin\\ffmpeg -i "input.gif" -vsync 0 "output.png"
D:\\ffmpeg\\bin\\ffmpeg -i "input.gif" -vsync 0 "output_%d.png"
=============================================================
旋轉或翻轉圖片和影片
D:\\ffmpeg\\bin\\ffmpeg -i -vf "transpose=1" output.mp4
Note:
transpose = 0 : 順時鐘團 90 度後,水平左右翻轉
transpose = 1 : 順時鐘團 90 度
transpose = 2 : 逆時鐘團 90 度
transpose = 3 : 順時鐘團 90 度,水平左右翻轉

可以連續旋轉翻轉,用transpose 指令用雙引號(")包住並用逗號(,)隔開,例如
D:\\ffmpeg\\bin\\ffmpeg -i -vf "transpose=1,transpose=1" output.mp4
參考:
How To Rotate Videos Using FFMpeg From Commandline


沒有留言 :

張貼留言