ffmpegを使った動画編集
ffmpegを使って動画を編集することがたまにあるのでよく使うコマンドを備忘録としてメモ。
動画形式を変換する
Convert from webm to mp4
ffmpeg -i <INPUT.webm> <OUTPUT.mp4>
Convert from mov to mp4
ffmpeg -i <INPUT.mov> -vcodec h264 -acodec mp2 <OUTPUT.mp4>
Change bitrate
ffmpeg -i <INPUT.mp4> -b <2300k> <OUTPUT.mp4>
Change frame rate
ffmpeg -i <INPUT.mp4> -filter:v fps=fps=<30> <OUTPUT.mp4>
Video to GIF
ffmpeg -ss <30> -t <3> -i <INPUT.mp4> -vf "fps=<10>,scale=<320>:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p]paletteuse" -loop 0 <OUTPUT.gif>
This example will skip the first 30 seconds (-ss 30) of the input and create a 3 second output (-t 3).
fps: sets the frame rate
scale: resize the output to 320 pixels wide and automatically determine the height while preserving the aspect ratio. The lanczos scaling algorithm is used in this example.
pallettegen and palleteuse filters will generate and use a custom palette generated from your input. These filters have many options.
split: will alow everything to be done in one command and avoids having to create a temporary PNG file of the pallete.
Control looping with -loop output option but the values are confusing. A value of 0 is infinite looping, -1 is no looping, and 1 will loop once meanng it will play twice. So a value of 10 will cause the GIF to play 11 times.
動画からクロップする
Crop video
ffmpeg -i <INPUT.mp4> -filter:v "crop=<outw>:<out_h>:<x>:<y>" <OUTPUT.mp4>
out_w/out_h are the width/height of the output rectangle.
x and y specify the top left corner of the output rectangle.
Trim video in time
ffmpeg -ss <00:01:00> -i <INPUT.mp4> -to <00:02:00> -c copy <OUTPUT.mp4>
-i: This specifies the input file.
-ss: used with -i, this seeks in the input file to position
00:01:00: This is the time your trimmed video will start with.
-to: This specify duration from start to end
00:02:00: This is te time your trimmed video will end with.
-c copy: This is an option to trim via stream copy (NB: very fast)
Extract a frame from a video
ffmpeg -i <INPUT.mp4> -vf "select=eg(n\,<34>)" -vframes 1 <OUTPUT.png>
<34>: for 35th frame (0 origin)
動画のサイズを変更する
Resize video
ffmpeg -i <INPUT.mp4> -vf scale="<w>:<h>" <OUTPUT.mp4>
If you want to retain aspect ratio then just give height as -1 and it will automatically resize based on the width.
ffmpeg -i <INPUT.mp4> -vf scale="<w>:-1" <OUTPUT.mp4>
Rotate video
ffmpeg -i <INPUT.mp4> -vf "transpose=<1>" <OUTPUT.mp4>
0 = 90 counter clockwise and vertical flip (default)
1 = 90 clockwise
2 = 90 counter clockwise
3 = 90 clockwise and vertical flip
Use -vf "transpose=2,tranpose=2" for 180 degree rotation
複数の動画をつなげる
Concatenate two videos side by side
ffmpeg -i <LEFT.mp4> -i <RIGHT.mp4> -filter_complex hstack <OUTPUT.mp4>
Concatenate videos in time
# Create File List
echo file <FILE1.mp4> > mylist.txt
echo file <FILE2.mp4> > mylist.txt
echo file <FILE3.mp4> > mylist.txt
for %%i in (*.mp4) do echo file '%%i' >> mylist.txt
#Concatenate files
ffmpeg -f concat -i mylist.txt -c copy <OUTPUT.mp4>
この記事が気に入ったらサポートをしてみませんか?