A Bash script to batch extract audio from YouTube videos

I’m work­ing on a pod­cast­ing project that requires me to extract the audio from mul­ti­ple YouTube videos. So, I put togeth­er a lit­tle script that allows me to batch extract the audio from a com­ma-sep­a­rat­ed list of YouTube urls.

In case some­one else might find this func­tion­al­i­ty use­ful, I’ve put togeth­er the instruc­tions for cre­at­ing and exe­cut­ing the script here. It requires that you’re a bit com­fort­able with the com­mand-line, and assumes you’re on a UNIX-based sys­tem (such as Lin­ux or OS X).

The script requires a com­mand-line pro­gram called youtube-dl. You can down­load it here. (And make sure to fol­low the instal­la­tion instruc­tions in order to make it exe­cutable.)

Once that is down­loaded, cre­ate a text file and name it ytmp3.sh:

for i in $(echo $1 | sed “s/,/ /g”)doyoutube-dl — extract-audio — audio-format=mp3 -t $idone

Once you’ve cre­at­ed the script, make it exe­cutable with the fol­low­ing:

sudo chmod a+rx ytmp3.sh

Then, you can move it to /usr/local/bin so that it’s exe­cutable from any­where:

mv ytmp3.sh /usr/local/bin

Now you can exe­cute run the script by pass­ing to it a com­ma-sep­a­rat­ed list (no spaces), eg.:

ytmp3.sh https://www.youtube.com/watch?v=yGhm7NfDaz8,https://www.youtube.com/watch?v=UJl-rMSULrk,https://www.youtube.com/watch?v=nPU7rnYpSxc

It will cycle through each video, extract the audio, and con­vert it to an mp3 for you.

It’s a sim­ple script, but I’m new to Bash-script­ing, so I was quite impressed with myself. I hope some­one can find it use­ful!

Leave a Reply