Skip to content

Instantly share code, notes, and snippets.

@basperheim
Created March 30, 2024 08:31
Show Gist options
  • Save basperheim/a5b1546ab363fee30cb9d46ef97cd326 to your computer and use it in GitHub Desktop.
Save basperheim/a5b1546ab363fee30cb9d46ef97cd326 to your computer and use it in GitHub Desktop.
Use FFmpeg to reduce an MP4 video file size

Use FFmpeg to reduce an MP4 video file size

To scale down the size of the MP4 file using FFmpeg while maintaining as much quality as possible, you can use the following command:

ffmpeg -i input.mp4 -vf "scale=-2:720" -c:v libx264 -crf 20 -preset slow -c:a copy output.mp4

Explanation of the options used:

  • -i input.mp4: Specifies the input file.
  • -vf "scale=-2:720": Specifies the video filter to scale the video. The -2 in the scale filter automatically calculates the width based on the aspect ratio while ensuring that the height is set to 720 pixels. This preserves the aspect ratio of the original video.
  • -c:v libx264: Specifies the video codec to use for encoding. libx264 is a widely used and efficient H.264 encoder.
  • -crf 20: Sets the Constant Rate Factor (CRF) for the video. Lower values will result in higher quality but larger file sizes, while higher values will result in lower quality but smaller file sizes. A value of around 20 is considered to be a good balance between quality and file size.
  • -preset slow: Sets the encoding preset to slow, which will improve the encoding efficiency at the cost of increased encoding time. This helps maintain higher quality.
  • -c:a copy: Copies the audio stream without re-encoding, ensuring that the audio quality remains unchanged.
  • output.mp4: Specifies the output file name.

Adjust the CRF value to balance between quality and file size according to your preference. Lower values will result in larger file sizes but better quality, while higher values will result in smaller file sizes but lower quality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment