Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Automate YouTube Video and MP3 Rips with Bash Shell Scripting

15 Responses »

I love listening to music while I code. Listening to music keeps me from getting too frustrated about code that isn't working (yes, it happens) or frustrations brought on by hosting providers, clients, etc. What I'll do is listen to Pandora, hear a song I like, look up the song on YouTube, and save the YouTube link in a text file for later listening. Having to go to YouTube every time sucks so I set out to take YouTube videos linked in a text file and download them to my computer in MP3 format.

The Text File

http://www.youtube.com/watch?v=iYtDqHupwrg
http://www.youtube.com/watch?v=xZ23pVTPfSw
http://www.youtube.com/watch?v=Rvw1ctGWfSs
http://www.youtube.com/watch?v=IXPOHCsgWFw
http://www.youtube.com/watch?v=QF73Ypncwss
http://www.youtube.com/watch?v=8nBmIetx_t8
http://www.youtube.com/watch?v=sw_x8XtngGM

One link per line.

The Bash Script

#!/bin/sh
IFS=$'\n'
for line in `< songs.txt`; do
	newfile=`youtube-dl.py -o %\(title\)s.%\(ext\)s $line | grep '^\[download\]' | cut -d ' ' -f 3-`
	ffmpeg -b 192k -i $newfile ${newfile/flv/mp3}
	rm $newfile
done

My script reads the file by line, downloads the YouTube video at the link provided by the text document, uses ffmpeg to rip the song into an MP3, and deleted the video. Note that you'll need YouTube-DL and FFMPEG to run this script; this script is essentially a shortcut to achieve what was outlined in my post titled YouTube, FFMPEG, and MP3 Conversion.

Discussion

  1. October 1, 2009 @ 11:33 am

    Youtube videos have awful audio quality though. Compared to 320kbps MP3 or FLAC anyway.

  2. October 1, 2009 @ 11:37 am

    Agreed, but they have a lot of stuff you wont find in other places (like live stuff).

  3. October 1, 2009 @ 12:33 pm

    is not that illegal??

  4. October 1, 2009 @ 12:41 pm

    I think you’ll look forward to the release of Spotify (http://www.spotify.com) in the US then ;) Although, that might take a while…

  5. urchin
    October 1, 2009 @ 1:09 pm

    I wrote a script in PHP that would download videos from crunchyroll.com. It would download the video via an fget in a loop and then update a json-file on the server and a periodic ajax request would read the json-file and display download progress to the user. It was cool while it lasted.

  6. October 1, 2009 @ 1:21 pm

    Spotify is great!

  7. October 1, 2009 @ 3:25 pm

    nice one david, great bashing!

  8. October 1, 2009 @ 4:40 pm

    Ever heard of http://www.songza.fm ?

  9. t0os
    October 1, 2009 @ 5:16 pm

    it seems snippet for textmate doesn’t work correct..

  10. October 2, 2009 @ 7:01 am

    yip, you’re gonna love spotify.

  11. October 7, 2009 @ 2:38 pm

    Hi, I had problems with the bash script that was always returning “Bad substitution”
    After googling around i solved the problem changing the leading
    #!/bin/sh
    in
    #!/bin/bash

  12. October 31, 2009 @ 11:16 am

    nice bashing. ;)

  13. richard hoogstad
    November 6, 2009 @ 7:35 pm

    Thanks for sharing this works great just had to make some minor changes to make it work under Linux.

    Just one note, all the mp3s that got converted had a bitrate of 64kBit/s which sounded horrible.

    For a better quality I changed:
    “ffmpeg -b 192k -i $newfile ${newfile/flv/mp3}”

    to:
    “ffmpeg -i $newfile -ab 128k ${newfile/flv/mp3}”

    the “-b” argument is for videos where “-ab” is for audio hope that was usefull.

  14. February 12, 2010 @ 12:20 am

    great.. entering KICK before the url also works ;)
    (so you get kickyoutube..) easy way to quickly get any format

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!