Editing video using ffmpeg
Usage examples from own experience.
Convert to another codec
ffmpeg -i original_video.mp4 -vcodec libx265 -crf 20
- "-vcodec libx265" is the codec to use.
- "-crf 20" is the compression rate (?) or quality factor, the higher, the worse the quality.
Resize
fmpeg -i original_video.mp4 -vf "scale=-2:720" video_720.mp4
- "-vf" means "apply filter".
- "scale=-2:720" is a filter to scale video to proportional (-2) horizontally and 720 pixels vertically.
Crop a rectangular area
ffmpeg -i original_video.mp4 -c:v copy -c:a copy -vf "crop=120:180:640:480;eq=brightness=0.15:contrast=1.3" cropped_video.mp4
- "-c:v copy" means "use the same video codec"
- "-c:a copy" means "use the same audio codec"
- "-vf" means "apply filter". Filters are separated by ";" and arguments are separated by ":".
- "crop=120:180:640:480" means "crop a 640*480 rectangle that is offset 120 pixels left and 180 pixels down from upper left corner"
- "brightness=0.15" is the brightness adjustment. Base value is 0 and expected values are somewhere in the -0.5 to 0.5 range.
- "contrast=1.3" is the contrast adjustment. Base value is 1 and expected values are somewhere in the 0.5 to 1.5 range.
Trim by time
ffmpeg -i original_video.mp4 -ss 00:05:30 -t 00:01:00 -c:v copy -an trimmed_video.mp4
Where:
- "-ss 00:05:30" means seek (move) to 05:30 in the original video
- "-t 00:01:00" means take one minute starting at the seek position
- "-c:v copy" means copy (use the same) video codecs
- "-an" means "audio none"
Play video with applied filters
ffplay -ss 00:16:00 -t 00:05:00 -vf "eq=brightness=0.2:contrast=1.5" video_to_play.mp4
Useful for previewing filters you are about to apply.