Hack 51. Crop Video During Playback
Get rid of those annoying black bars at the top and bottom of movies using special features from MPlayer.
For the longest time, I was living in a 4:3 aspect ratio world and I didn't even know it. When I watched a VHS movie on the television, I didn't know that parts of the image had been cropped or what "pan and scan" was. As I got older and DVDs came out, I noticed that they often had widescreen and fullscreen options, and I finally started to understand that movies generally have a widescreen aspect ratio (such as 5:3 or 16:9) that, when displayed correctly on a TV, results in black bars along the top and bottom.
Of course, when playing back a movie on the computer, most video players adjust their size so that all you see is the movie without any black bars (until you fullscreen the image at least). However, if you watch a VCD or SVCD (videos formatted so that they can be played off of a CD in many modern DVD players) on the computer, the black bars are part of the image and are there whether you watch at fullscreen or not. My laptop's display has a widescreen aspect ratio (5:3) so it's perfect for watching DVDsespecially on a plane tripbut when I play back VCD or SVCD files fullscreen, I get large black bars and an even smaller video (Figure 3-1). My widescreen display goes to waste. I found that with a few simple command-line tweaks to mplayer, however, I could crop those black bars from the top and the bottom so the video would completely fill my screen. I could even have MPlayer figure out the optimal crop settings for me. In this hack I discuss how to leverage MPlayer to automatically crop those annoying black bars from a movie.
Figure 3-1. Video with black bands
The first step to automatically crop your video is to run mplayer with the -vf cropdetect argument. This tells MPlayer to scan the video as it is playing for those black bars and to output optimal crop dimensions for you to use to crop out the black bars:
$ mplayer -vf cropdetect movie.mpg … crop area: X: 0..479 Y: 80..496 (-vf crop=480:416:0:80)2.5% 3 0 46% crop area: X: 0..479 Y: 80..496 (-vf crop=480:416:0:80)2.5% 3 0 46% crop area: X: 0..479 Y: 80..496 (-vf crop=480:416:0:80)2.5% 3 0 46% crop area: X: 0..479 Y: 80..496 (-vf crop=480:416:0:80)2.5% 3 0 45%
When you first start MPlayer with the option, you will notice a lot of output to your script as MPlayer tells you the width and height of the section to crop, as well as the dimensions of the image to keep. Let the video play back for a few seconds, particularly if the opening screen is black, so MPlayer can accurately find the sections to crop. Once the crop dimensions are consistently the same for a few seconds, close MPlayer.
The mplayer output tells you the exact argument you need to crop the black bars. In this example the argument is -vf crop=480:416:0:80. Now run mplayer again with this:
$ mplayer -vf crop=480:416:0:80 movie.mpg
Now get the full benefit of your widescreen displays, no matter what video format you happen to be watching (see Figure 3-2).
Figure 3-2. Video with black bands cropped