To remove baked-in black bars (letterbox or pillarbox) from a video using ffmpeg, you can use the crop filter. The crop filter allows you to specify the dimensions and position of the area you want to keep, effectively removing the black bars. Here's how you can do it:
Steps to Remove Black Bars
-
Analyze the Video: First, determine the area of the video that contains the actual content (i.e., the dimensions and position of the non-black area). You can do this manually by inspecting the video in a media player or by using ffmpeg's cropdetect filter to automatically detect the black bars.
-
Use cropdetect (Optional): If you're unsure of the exact crop dimensions, you can use ffmpeg to detect the black bars automatically:
ffmpeg -i input.mp4 -vf "cropdetect" -f null -
- This command analyzes the video and outputs crop values in the console, such as
crop=1280:720:0:0.
- Look for the
crop values in the output, which are in the format crop=width:height:x:y.
width and height are the dimensions of the content area.
x and y are the top-left coordinates of the content area.
-
Apply the Crop Filter: Once you have the crop values, use the crop filter to remove the black bars. For example, if the crop values are crop=1280:720:0:0, run the following command:
ffmpeg -i input.mp4 -vf "crop=1280:720:0:0" -c:v libx264 -c:a copy output.mp4
-vf "crop=width:height:x:y": Specifies the crop filter with the detected or manually determined values.
-c:v libx264: Re-encodes the video using the H.264 codec (you can use other codecs if needed).
-c:a copy: Copies the audio stream without re-encoding to save time and preserve quality.
output.mp4: The name of the output file.
-
Manual Cropping (If Needed): If cropdetect doesn't give accurate results or you want to manually specify the crop, you can determine the values by:
-
Preserve Quality: To minimize quality loss, you can adjust the video bitrate or use a higher quality preset. For example:
ffmpeg -i input.mp4 -vf "crop=1280:720:0:0" -c:v libx264 -preset slow -crf 18 -c:a copy output.mp4
-preset slow: Uses a slower encoding preset for better quality.
-crf 18: Sets the Constant Rate Factor (lower values mean better quality, with 18 being visually lossless).
Notes
Example with Scaling (Optional)
If you want to crop the black bars and then scale the video to a specific resolution, you can chain the crop and scale filters. For example:
ffmpeg -i input.mp4 -vf "crop=1280:720:0:0,scale=1920:1080" -c:v libx264 -c:a copy output.mp4
This crops the video to 1280x720 and then scales it to 1920x1080.
By following these steps, you can effectively remove baked-in black bars from your video using ffmpeg.