FFMPEG | Compress video files

Inhaltsverzeichnis

Compress and Convert MP4 to WMV

Compress and Convert MP4 to Webm for YouTube, Ins, Facebook

ffmpeg -i source.mp4 -c:v libvpx-vp9 -b:v 0.33M -c:a libopus -b:a 96k \<br>-filter:v scale=960x540 target.webm

Compress and Convert H.264 to H.265 for Higher Compression

ffmpeg -i input.mp4 -vcodec libx265 -crf 28 output.mp4

Set CRF in FFmpeg to Reduce Video File Size

ffmpeg -i input.mp4 -vcodec libx264 -crf 24 output.mp4

Reduce video frame size to make 4K/1080P FHD video smaller

ffmpeg -i input.avi -vf scale=1280:720 output.avi

Command-line – resize video in FFmpeg to reduce video size

ffmpeg -i input.avi -vf scale=852×480 output.avi

Resize video in FFmpeg but keep the original aspect ratio

Specify only one component, width or height, and set the other component to -1, for eample,

ffmpeg -i input.jpg -vf scale=852:-1 output_852.png

Converting WebM to MP4

ffmpeg -i video.webm video.mp4

When the WebM file contains VP8 or VP9 video, you have no choice but to transcode both the video and audio.

Video conversion can be a lengthy and CPU intensive process, depending on file size, video and audio quality, video resolution, etc. but FFmpeg provides a series of presets and controls to help you optimize for quality or faster conversions.

A note on video quality

When encoding video with H.264, the video quality can be controlled using a quantizer scale (crf value, crf stands for Constant Rate Factor) which can take values from 0 to 51: 0 being lossless, 23 the default and 51 the worst possible. So the lower the value the better the quality. You can leave the default value or, for a smaller bitrate, you can raise the value:

ffmpeg -i video.webm -crf 26 video.mp4

Video presets

FFmpeg also provides several quality presets which are calibrated for a certain encoding speed to compression ratio. A slower preset will provide a better compression. The following presets are available in descending order: ultrafastsuperfastveryfastfaster, fastmediumslowslower and veryslow. The default preset is medium but you can choose a faster preset:

ffmpeg -i video.webm -preset veryfast video.mp4

Placing the MOOV atom at the beginning

All MP4 files contain a moov atom. The moov atom contains information about the length of the video. If it’s at the beginning it immediately enables a streaming video player to play and scrub the MP4 file. By default FFmpeg places the moov atom at the end of the MP4 file but it can place the mov atom at the beginning with the -movflags faststart option like this:

ffmpeg -i video.webm -movflags faststart video.mp4

Using Levels and Profiles when encoding H.264 video with FFmpeg

To ensure the highest compatibility with older iOS or Android devices you will need to use certain encoding profiles and levels. For example a video encoded with the High Profile and Level 4.2 will work on iPhone 5S and newer but not on older iOS devices.

ffmpeg -i video.webm -movflags faststart -profile:v high -level 4.2 video.mp4

Converting WebM with H.264 video to MP4

In some rare cases the .webm file will contain H.264 video and Vorbis or Opus audio(for example .webm files created using the MediaRecorder API on Chrome 52+ ). In such cases you don’t have to re-encode the video data since it’s already in the desired H.264 format (re-encoding is also not recommended since you’ll be loosing some quality in the process while consuming CPU cycles) so we’re just going to copy over the data

To copy the video data and transcode the audio in FFmpeg you use the -c:v copy option:

ffmpeg -i video.webm -c:v copy video.mp4