O'Reilly logo

Linux Multimedia Hacks by Kyle Rankin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #76. Cut Commercials

Use avidemux to quickly and easily cut commercials out of your recorded TV shows.

So you have set up MythTV [Hack #77] , retrieved shows from your TiVo, or otherwise have some sort of television show in digital form on your computer. One nice feature of a VCR, TiVo, or other video recorder is the ability to quickly skip commercials in TV shows you have recorded. If you have recorded a show in digital format, you can certainly just fast-forward each time a commercial appears, but that requires extra work each time you watch the show. Also, with some time-based recording methods you end up getting a bit of the previous show and perhaps a bit of the next show in your recording in case your clock differs from the TV station's clock. If you plan to archive the show and watch it multiple times, you probably want to crop out all those commercials and bits from other shows. This is especially true if you plan on converting the video to VCD, SVCD, or DVD format, as discussed in [ Hacks #69 and #70 ].

Now, before you get visions of sitting behind a huge sound board or in front of three monitors full of advanced video-editing software, let me say that cropping out commercials is not very difficult or time-consuming if you use the proper tools. After all, if it takes more work to crop the commercials than to watch them, what's the point? With the avidemux video-editing software, and particularly with its "Scan for black frames" feature, you can locate and remove commercials from your videos quickly and without much effort.

The first step is to download and install the avidemux software. This step will vary based on your distribution, but visit the avidemux download page at http://fixounet.free.fr/avidemux/download.html and find the precompiled package for your distribution. If there is no precompiled version, then download the .tar.gz source from the site and follow the installation documentation at http://fixounet.free.fr/avidemux/doc/en/install.xml.html to install the software. There are very few definite required libraries for avidemux, it mostly requires GTK+ and glib2.x, libmad, libxml2, and nasm—all software that should be readily available on any major Linux distribution. Apart from that, other libraries are optional and only serve to increase avidemux's support for various video and audio formats. Use [Hack #53] to find and install various extra codecs and libraries for your computer.

Tip

avidemux also has been ported to OSX and Windows so you can crop commercials from those environments as well. Just download those binaries from the same download page.

Once avidemux is installed, launch it from a menu or type avidemux from a console. The default interface has a lot of different options but for your purposes you only need to use a few of them. First click File → Open and choose the video you wish to edit from the dialog box. A large number of video formats are supported, including DivX, Xvid, MPEG1 and MPEG2, Nuppelvideo, DV, and a number of other formats (a full list is available at http://fixounet.free.fr/avidemux/doc/en/input.xml.html). avidemux shows you the first frame in the main section of the window, and if you want you can even start playback from this interface.

The key to removing commercials in broadcast content is identifying black frames. Broadcasters usually use one or more black frames to separate commercials from the actual show or from each other, so if you can identify these frames you can use them as guide points for where to cut.

The simplest way to remove commercials from a program is chronologically. The following method also takes into account the fact that a commercial break might contain multiple sets of black frames in between individual commercials. Since your show probably doesn't have any black frames, it's easiest to move to different sections of the show, then move forward or backward to find black frames that signal new commercials.

The first step in the commercial removal process is to remove any commercials or other content from the beginning of the video so your video starts with the beginning of the show. This step is a bit easier than removing commercials from the middle of the video, so it's a good place to start. If your recording already starts at the beginning of the show you can skip this particular step. With the video you wish to edit open but paused, drag the bar along the bottom with your mouse to skip ahead until you find the beginning of your show. Now click Play → Search Previous Black Frame (or click the corresponding button on the bottom toolbar) and avidemux will scan backwards through the video for a black frame. Once it finds the black frame, click Edit → Set Marker B or press ]. This sets the end marker for where to cut. Now move the slider to the very beginning of the video and click Edit → Set Marker A or press [. This sets the beginning marker for where to cut. Now hit the Delete key and avidemux will delete all the frames in between the two markers, leaving you at the beginning of your show.

Now you are ready to remove any commercials that are within the show. Skip ahead a few frames in the show so you are definitely on a non-black frame, then click Play → Search Next Black Frame. avidemux will skip through the program and find the next black frame. Set the opening marker (Edit → Set Marker A) and then drag the slider to skip ahead through the commercials. While you could just skip to the next black frame, often there are black frames between commercials so this is a timesaver. Skip through the commercials with the slider until you see your show again. Now click Play → Search Previous Black Frame to find the end of the commercials, then set the closing marker (Edit → Set Marker B), and press Delete to remove all of the frames between the markers.

Repeat the previous step until you reach the end of the show. If there is commercial content past the end of the credits, just seek to the black frame, set an opening marker, then skip to the end of the video, set the closing marker, and delete. After your video is all edited, click File → Save → Save Video to save your changes. Once you get some practice, you'll find you can remove commercials from your shows rather quickly.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required