FFMPEG
Comprehensive guide: ffmpeg.org
Trim videos
ffmpeg -i <input_video> -ss <hh:mm:ss.mmm> -to <hh:mm:ss.mmm> -c copy <output_video>
This trims the video from the time mentioned after -ss
to -to
. -c copy
This is an option to trim via stream copy which prevents re-encoding. Do not use if the input type is different from the output type. You can also use -t
rather than -to
to specify the length of the trim rather than a timestamp.
Crop videos
ffmpeg -i in.mp4 -filter:v "crop=out_w:out_h:x:y" -c:a copy out.mp4
NOTE: DO not use -c:a copy
if the output is not the same type as input. This is used to prevent re-encoding which is necessary in case of changing type.
Where the options are as follows:
out_w
is the width of the output rectangle
out_h
is the height of the output rectangle
x
and y
specify the top left corner of the output rectangle
To crop the bottom right quarter:
ffmpeg -i in.mp4 -filter:v "crop=in_w/2:in_h/2:in_w/2:in_h/2" -c:a copy out.mp4
You can refer to the input image size with in_w
and in_h
as shown in this first example. The output width and height can also be used with out_w and out_h.
Crop 20 pixels from the top, and 20 from the bottom:
ffmpeg -i in.mp4 -filter:v "crop=in_w:in_h-40" -c:a copy out.mp4
The filter will automatically center the crop if x
and y
are omitted such as in this example.
ffmpeg -i <input_video> -vf <transformation>
vf
is an alias -filter:v
.
Options for transformation:
- hflip -> horizontally flip the video.
- vflip -> vertically flip the video
- transpose=x -> rotate a video.
- 0 -> 90CounterClockwise and vertical flip.
- 1 -> 90Clockwise.
- 2 -> 90CounterClockwise.
- 3 -> 90Clockwise and Vertical flip.
Note: This will re-encode the audio and video parts. You can use -c:a copy
to copy the audio without touching it.
Previewing Videos
ffplay -i input -vf "crop=in_w:in_h-40"
Convert video to different types
ffmpeg -i <input.mp4> <output.gif>
Just using the file extensions for the input and output will automatically recognize the type and convert it.
Audio Controls
ffmpeg -i <input.mp4> -f mp3 -ab 192000 -vn <output.mp3>
The -i option in the above command is simple: it is the path to the input file. The second option -f mp3 tells ffmpeg that the output is in mp3 format. The third option i.e -ab 192000 tells ffmpeg that we want the output to be encoded at 192Kbps and -vn tells ffmpeg that we dont want video. The last param is the name of the output file.