My improved youtube2mp3 converter script

A while ago I found this article http://jeffreyv.hubpages.com/hub/Youtube-to-MP3-on-Ubuntu-Linux it did exactly what it wanted at the time but me been lazy like I am decided it was time to upgrade it a bit mainly so I just need to supply it with the location URL and it names the file itself. cue investigation into youtube-dl (which is awesome) after finding that it can return the video’s title I fairly quickly got the rest sorted so here it is:

 bash | 
 
 copy code |
?

01
#!/bin/bash
02
if [ "$2" = "" ]
03
then
04
  name=$(youtube-dl -s --get-title "$1")
05
  name2=$(echo ${name} | sed -e s/"'"/""/g  -e 's/ /_/g')
06
  echo "No output file defined saving mp3 as:"
07
  echo  "/var/www/"${name2}".mp3"
08
  Output="/var/www/"${name2}".mp3"
09
else
10
  echo "Output file detected saving mp3 as:"
11
  echo "${2}"
12
  Output="${2}"
13
fi
14
x=~/.youtube-dl-$RANDOM-$RANDOM.flv
15
youtube-dl --output=$x --format=18 "$1"
16
 
17
ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "${Output}"
18
rm $x

Leave a Reply

Your email address will not be published. Required fields are marked *