Featured image of post 一些FFmpeg指令

一些FFmpeg指令

picture to Gif

Define custom palette The converted GIF has only three colors: white and two tints of blue, therefore, the default 256 color palette can be reduced to 16. The custom palette is created with the palettegen filter.

1
ffmpeg -i frames/200.png -vf palettegen=16 palette.png

After the palette was created it can be used in the GIF conversion.

1
ffmpeg -i frames/%03d.png -i palette.png -filter_complex "fps=20,scale=720:-1:flags=lanczos[x];[x][1:v]paletteuse" logo.gif

圖片壓縮

1
ffmpeg -i image_source -q compress_level out_source

compress_level:壓縮等級為正整數,數值越大壓縮等級越高

約束比例壓縮

1
ffmpeg -i image_source -vf scale=width:height out_source

width為壓縮後的圖片寬度 height為壓縮後的圖片高度 當width或height其中有一個數值為-1時將保持原來圖片尺寸比例壓縮

圖片裁切

1
ffmpeg -i image_source -vf crop=width:height:from_x:from_y out_source

width是要裁剪出的圖片的寬度 height是要裁剪出的圖片的高度 from_x是裁剪的起始X軸的位置 from_y是裁剪的起始Y軸的位置 如果不指定from_x和from_y表示從原始圖片中心位置開始裁剪

添加水印圖片

1
ffmpeg -i image_source -vf movie=logo_source,scale=logo_width:logo_height,lut=a=val*opacity_num[watermask];[in][watermask] overlay=from_x:from_y[out] -y  out_source

logo_source為水印圖片地址 logo_width為水印圖片的寬度 logo_height為水印圖片的高度 opacity_num為水印圖片的透明度 from_x為水印的起始X軸的位置 from_y為水印的起始Y軸的位置

添加文字水印

1
ffmpeg -i image_source -vf drawtext=fontfile=font_ttf_path:fontcolor=font_color:fontsize=font_size:text=message_info:x=from_x:y=from_y  out_source

font_ttf_path為字體路徑,此項必須設置否則會出現字體無法找到的錯誤 字體路徑要設置絕對路徑並且要注意路徑正反斜線轉義(例如C\:/Windows/Fonts/simhei.ttf) font_color為字體的顏色 font_size為字體的大小 message_info為水印文字內容 from_x為水印的起始X軸的位置 from_y為水印的起始Y軸的位置 如果水印內容是中文需要設置中文字體否則會文字顯示亂碼

圖片旋轉指定的角度

1
ffmpeg -i image_source -vf rotate=route_num*PI/180 -y  out_source

route_num是要旋轉的角度 當route_num大於0的時候順時針旋轉,當route_num小於0的時候逆時針旋轉

圖片旋轉特殊簡便命令

1
ffmpeg -i image_source -vf transpose=route_number -y out_source

route_number=0順時針旋轉90度再左右鏡像 route_number=1順時針旋轉90度 route_number=2逆時針旋轉90度 route_number=3逆時針旋轉90度再左右鏡像

圖片左右鏡像

1
ffmpeg -i image_source -vf hflip out_source

將圖片上下鏡像

1
ffmpeg -i image_source -vf vflip out_source

指定長度和寬度

1
ffmpeg -i input.jpg -vf scale=320:240 output_320x240.png

指定要按比例縮放的長度和高度

1
ffmpeg -i input.jpg -vf scale=320:-1 output_320x240.png

縮放兩倍於以前

1
ffmpeg -i input.jpg -vf scale=iw*2:ih input_double_width.png

縮放到上一個的二分之一

1
ffmpeg -i input.jpg -vf “scale=iw*.5:ih*.5” input_half_size.png
1
ffmpeg -i input.jpg -vf “scale=iw/2:ih/2” input_half_size.png
Built with Hugo
Theme Stack designed by Jimmy