Scaling video sizes via ffmpeg.

To reduce the resolution of an MP4 video using FFmpeg, follow these steps and choose the method that best fits your needs:

1. Basic Resolution Reduction (e.g., to 640×480)

   ffmpeg -i input.mp4 -vf "scale=640:480" output.mp4
  • Note: This may distort the aspect ratio if not proportional to the original.

2. Maintain Aspect Ratio (Auto-Height)

Scale to a specific width while calculating height to preserve aspect ratio:

   ffmpeg -i input.mp4 -vf "scale=640:-2" output.mp4
  • -2 ensures the height is divisible by 2 (required for most codecs).

3. Scale by Percentage (e.g., 50% smaller)

   ffmpeg -i input.mp4 -vf "scale=iw*0.5:ih*0.5" output.mp4
  • iw/ih: Input width/height. Multipliers (0.5 = half size) can be adjusted.

4. Target Maximum Dimensions (Fit within 640×480 box)

Scales down only if needed, preserving aspect ratio:

   ffmpeg -i input.mp4 -vf "scale='min(640,iw)':'min(480,ih)':force_original_aspect_ratio=decrease" output.mp4

5. High-Quality Downscale with Lanczos Filter

   ffmpeg -i input.mp4 -vf "scale=1280:720:flags=lanczos" -c:v libx264 -crf 23 -preset slow -c:a copy output.mp4
  • flags=lanczos: Uses the Lanczos algorithm for sharper downscaling.
  • -crf 23: Controls quality (lower = better, 18–28 is typical).
  • -preset slow: Better compression (slower encoding).
  • -c:a copy: Copies audio without re-encoding (remove to re-encode audio).

6. Force Specific Codec & Optimize for Web

   ffmpeg -i input.mp4 -vf "scale=1280:720" -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k -movflags +faststart output.mp4
  • -c:v libx264: Uses H.264 video codec.
  • -c:a aac: Re-encodes audio to AAC.
  • `-movflags +faststart**: Allows video to start playing faster online.

Key Notes:

  • Aspect Ratio: Always use scale=WIDTH:-2 (auto-height) or force_original_aspect_ratio to avoid distortion.
  • Quality: Use -crf (lower = better) and -preset (slower = better compression).
  • Hardware Acceleration: Add flags like -hwaccel cuda (NVIDIA) or -c:v h264_videotoolbox (macOS) for faster encoding.
  • Check Resolution: Use ffprobe input.mp4 to see original dimensions.

Example (Most Common Use Case):

Scale to 720p, preserve aspect ratio, high-quality settings:

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *