Published on

Setting the Aspect Ratio of Videos with FFmpeg

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Why Adjust the Aspect Ratio?

Setting the aspect ratio of a video can help:

  • Match Platform Requirements: Ensure compatibility with social media or streaming platforms.
  • Correct Distorted Videos: Fix videos that appear stretched or squished.
  • Improve Viewer Experience: Provide a consistent and professional look.

What Is Aspect Ratio?

The aspect ratio is the proportional relationship between a video's width and height, expressed as width:height. Common aspect ratios include:

  • 16:9: Standard for HD videos.
  • 4:3: Traditional TV format.
  • 1:1: Square format for social media.
  • 9:16: Vertical videos for mobile devices.

Adjusting Aspect Ratio with FFmpeg

Basic Command to Set Aspect Ratio

Use the setdar filter (Display Aspect Ratio) to adjust the aspect ratio:

ffmpeg -i input.mp4 -vf "setdar=aspect_ratio" output.mp4
  • Replace aspect_ratio with the desired ratio, e.g., 16/9 or 4/3.

Example: Set to 16:9 Aspect Ratio

ffmpeg -i input.mp4 -vf "setdar=16/9" output.mp4

Example: Set to 4:3 Aspect Ratio

ffmpeg -i input.mp4 -vf "setdar=4/3" output.mp4

Example: Square Aspect Ratio

For social media posts:

ffmpeg -i input.mp4 -vf "setdar=1/1" output.mp4

Preserve Quality When Changing Aspect Ratio

To avoid resizing distortion, add padding instead of stretching the video:

ffmpeg -i input.mp4 -vf "scale=1280:720, pad=1280:720:(ow-iw)/2:(oh-ih)/2" output.mp4
  • Replace 1280:720 with your target resolution.

Preview Aspect Ratio Changes

Use FFplay to preview changes before applying them:

ffplay -i input.mp4 -vf "setdar=16/9"

Best Practices for Aspect Ratio Adjustment

  1. Understand Platform Requirements: Know the aspect ratio guidelines for your target platform.
  2. Use the Correct Filter: Choose setdar for display aspect ratio or setsar for sample aspect ratio.
  3. Preview Changes: Always test with FFplay to ensure the output meets expectations.
  4. Backup Originals: Keep a copy of your original video files.

Conclusion

Adjusting the aspect ratio with FFmpeg is essential for creating professional-looking videos tailored to specific platforms or fixing display issues. With FFmpeg's powerful filters and efficient processing, you can quickly modify your videos' aspect ratio without compromising quality. Start mastering aspect ratio adjustments with FFmpeg today!