Automate YouTube Video and MP3 Rips with Bash Shell Scripting

By  on  

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.

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Incredible Demos

  • By
    Smooth Scrolling with MooTools Fx.SmoothScroll

    I get quite a few support requests for my previous MooTools SmoothScroll article and the issue usually boils down to the fact that SmoothScroll has become Fx.SmoothScroll. Here's a simple usage of Fx.SmoothScroll. The HTML The only HTML requirement for Fx.SmoothScroll is that all named...

  • By
    CSS content and attr

    CSS is becoming more and more powerful but in the sense that it allows us to do the little things easily.  There have been larger features added like transitions, animations, and transforms, but one feature that goes under the radar is generated content.  You saw a...

Discussion

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

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

  3. is not that illegal??

  4. 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

    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.

    • TFTP

      Where can I found ur scritp to DL from CR??

  6. Spotify is great!

  7. nice one david, great bashing!

  8. t0os

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

  9. yip, you’re gonna love spotify.

  10. 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

  11. nice bashing. ;)

  12. Richard Hoogstad

    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.

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

  14. There’s a new script for linux to search and watch youtube’s video on linux shell, called YouShell.

    http://code.google.com/p/youshell/

  15. maja

    doing what you described here worked great: http://davidwalsh.name/youtube-ffmpeg-mp3-conversion
    but the bashscript doesn’t work for me. i start youtube-dl without the ‘.py’ (its installed), so i deleted these signs, but if i execute the shell-script nothing happens (and yes i did make it executable). any ideas? :-(

    • Maxwell Chandler

      See the version I posted below

  16. Anum Khan

    I like the way you explain everything but i just wanna know that how to make download page to download videos direct for Funops Videos.

  17. Maxwell Chandler
    #!/bin/sh
    
    while read -r p; do    
    
    x=/tmp/.youtube-dl-$(date +%y.%m.%d_%H.%M.%S)-$RANDOM.flv
    
    youtube-dl --audio-quality 160k --output="$x" --format=18 "$p"
    
    SUBSTRING=$(echo "$p"| cut -c33-50)
    
    ffmpeg -i "$x" -acodec libmp3lame -ac 2 -ab 128k -vn -y "$SUBSTRING.mp3" </dev/null
    
    rm "$x"
    
    done <songs.txt
    
  18. Maxwell Chandler

    The version above is just another way to do things, Awesome post btw.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!