Resizing and shrinking videos with ffmpeg

ffmpeg resizing

You can reduce the size of a video by reducing the resolution with the ffmpeg program.

Install:

sudo apt install ffmpeg

Usage example, resizes to max 360p in vertical screen resolution, or height, in pixels, lowering the quality a bit with -crf 26:

ffmpeg -i video.mp4 -vcodec libx264 -crf 26 -c:a aac -b:a 128k -vf scale=-2:360,format=yuv420p video-360p.mp4

Lower quality, to get smaller files

Add for example -crf 26 to reduce the quality, where 23 is the default, 0 is lossless and 51 is worst quality.

Batch compress movies files with ffmpeg

Shrink multiple files with these commands. The libx265 codec is faster, but the result can be choppy.

Inside a folder with mp4 movies, create a folder called "encoded"

mkdir encoded;

Compress mp4 files in current folder into "encoded" folder

Compressing a video with -crf 26 can reduce a video size with around 60%.

for f in *.mp4; do ffmpeg -i "$f" -vcodec libx264 -acodec copy -crf 26 "encoded/${f%.mp4}.mp4"; done

Compress videos of all formats in current folder into "encoded" folder

for f in *.*; do ffmpeg -i "$f" -vcodec libx264 -acodec copy -crf 26 "encoded/${f%}.mp4"; done

Edit video length

To shorten a video, define starting position with -ss and duration with -t.
-ss: First argument time is from time
-t: Second argument is duration (not end time) in seconds

ffmpeg -ss 25 -t 13 -i my-movie.mp4 -vcodec copy -acodec copy my-movie-edited.mp4

Bulk create thumbnails

Ratio based on 1920x1080, 16:9 aspect ratio:

for f in *.mp4; do ffmpeg -i "$f" -ss 00:00:03 -vframes 1 -s 640x360 "${f%.mp4}.jpg"; done

From: https://medium.com/@luongnhutoan/auto-generate-thumbnails-for-all-videos-inside-folder-with-ffmpeg-623164986dfa