Save iTunes Music Store videos

Apple’s iTunes Music Store (iTMS) includes a large selection of music videos, updated regularly. When you find a video you want to watch, just click on it and you’ll see a “watch the video” page, allowing you to choose either the Small or Large version of the video. Make your selection, and (after the download reaches a certain point) the video starts playing within iTunes.

But what if you want to watch the video again later, without waiting for a potentially large file to re-download? The iTMS doesn’t make it simple to save these videos to your hard drive; QuickTime Pro’s contextual menus won’t appear with a control-click, and the cached data isn’t usable as a movie file. The difficulty in saving these videos is hard to understand—after all, they’re simply commercials for the music itself, enticing you to purchase the full, higher-quality version of the song they’re advertising. So why make it so difficult to save a commercial?

There are, however, options available if you’d like to save these videos. You can try checking MacUpdate, VersionTracker, or your favorite software update site for iTMS video saving programs—try a search on iTMS video , for instance. There are usually a few such programs available, but they tend to break with each iTunes update. At the moment, there’s even a website that will take a copied iTMS music video URL (via a control-click on the Small or Large buttons) and output a direct download URL. This too, though, may either stop working some day, or vanish from the net. So the other option is to get your hands a bit dirty in the Terminal, and install a program that will help you manually save the iTMS videos yourself. This is the solution I’ll cover in today’s hint.

Install a new Unix application The first thing we’ll need to do is install a new Unix program called tcpflow. This program captures data that is transmitted over TCP connections—it basically saves the data streams that go over your internet connection for later analysis. There are many ways to install this program; if you have either Fink or DarwinPorts installed, tcpflow is available as an installable program. If you don’t know what Fink and DarwinPorts are, don’t worry about it—there’s another simple way to get the program. Marc Liyanage has made a compiled version of tcpflow available on his Mac OS X Packages page.

Visit Marc’s page, and download the proper version of tcpflow for your system—if you’re using 10.3 or newer, including 10.4, grab the 10.3 tar.gz archive. Once it’s downloaded, expand it and then double-click the resulting tcpflow.pkg file you’ll see in your downloads folder. This is a standard OS X installation package; follow the instructions and enter your password when prompted. When the installer finishes, you’ll have a copy of tcpflow installed in your /usr/local/bin folder. Note that Fink and/or DarwinPorts may install tcpflow into a separate location.

Modify your $PATH statement Before we can put it to use, though, we need to make sure that Unix knows about the directory—we do that by adding the directory to something called the $PATH variable. The $PATH variable tells Unix where to look for programs and files. If you used Fink or DarwinPorts to install tcpflow, you’ve already got an updated $PATH variable, so you can skip this section. In your Terminal, type these commands:

	$ cd ~
	$ pico .bash_profile
	

Depending on what you’ve done in Unix before, this file may not exist, or it may have existing data in it. In either case, you just want to add this line to the file:

PATH="$PATH:/usr/local/bin"

Use the arrow keys to move down to an empty row, copy the above text, paste it into the editor, then hit Control-O followed by Return to save the file, and hit Control-X to quit pico. Close the current Terminal window and open a new one (this will re-read your .bash_profile file, activating your new $PATH setting. You can check it by typing echo $PATH at the Terminal prompt; make sure the output includes the /usr/local/bin directory. Also launch iTunes (if it’s not running), and switch to the music video area of the store.

Get the video Find the video you wish to watch from the list, and click once on it. You should now be at the Small or Large version option screen. If the video you selected only has one size, use the iTMS’ Back button to go back to the selection page. Now switch to the Terminal, and type the following command, then press Return:

sudo tcpflow -c -i en0 port 80 | grep -oE '\<GET *.*\.mov.*HTTP/1.1 *\>|Host: *.*'

Note: You’ll probably need to change en0 to en1 if you’re using an AirPort connection instead of an Ethernet connection. If you’re on a modem, try ppp0. You may have to look at your specific interface configuration output (type ifconfig -a in the Terminal) to find other options if none of those seem to work.

The above is one long line; you can simply copy and paste it into the Terminal. Explaining everything it’s doing is well beyond the scope of this hint (as well as far beyond my Unix skill level!), but here’s a brief summary. tcpflow is launched with some options (output to the console (screen) and which interface and port to watch) and root privileges via sudo. Any output from tcpflow is sent to grep, a search command, to look for lines containing GET and .mov or Host:. These are the lines that contain the actual URL for the music video; using grep simply trims what would otherwise be a huge amount of output to just those lines we’re interested in seeing.

Enter your password when prompted, and tcpflow is now sitting there, waiting for some activity to record. Now switch back to iTunes, and click on the version of the video that you’d like to save—probably the large one, unless you’re on a very slow connection and would rather save the smaller versions. As soon as you see the QuickTime window appear with its progress bar, switch back to the Terminal. tcpflow is still running, of course, but you should now see some output from the program. Assuming you see some output, hit Control-C to stop tcpflow. When you do so, you should see something like this on your Terminal screen, below the Password: line:

	tcpflow[4649]: listening on en0
	Host: metrics.apple.com
	Host: metrics.apple.com
	GET /Features/y2005/m05/d11/h17/dj.asfqgwhk.mov HTTP/1.1
	Host: a1988.phobos.apple.com
	^Ctcpflow[4649]: terminating
	

There may be additional lines as well, and the data you see won’t match exactly what’s shown above. However, you’re only interested in parts of two lines—the one that starts with GET /Features and the one that starts with Host:. Using these two lines, you can build a download URL for the video. All you need to do is to put http:// in front, then take the host name listed after the Host: entry, add the /Features… bit from the other line (only through .mov, and you’ll have the final URL:

http://a1988.phobos.apple.com/Features/y2005/m05/d11/h17/dj.asfqgwhk.mov

The easiest way to assemble this URL is actually in Safari itself. In a new blank window or tab, type http:// in the URL bar, then switch to the Terminal and select and copy the relevant host information. Switch back to Safari, click in the URL bar (make sure the cursor is positioned after the double-slash), paste that text, switch back to Terminal, copy the /Features bit, through and including the .mov extension, switch back to Safari, and paste again. You now have a complete URL in Safari’s URL bar. Select it all via Command-A, then copy it, switch to the downloads window, and hit Paste.

If everything worked right, your music video should now be downloading to your specified Downloads folder.

Subscribe to the Best of Macworld Newsletter

Comments