Saturday, April 02, 2011

Download HQ or HD video from Youtube and convet to mp3

Well the title says it all. Copy the Youtube URL, and go to www.keepvid.com where you paste it into the search field. Click the "high definition" mp4 file link to download (sometimes it's just "high quality") but it's going to be enough for the treadmill or car stereo.

When you've saved enough videos to one directory, use the following script to convert them to mp3. You will need faad and lame (or twolame)

#!/bin/bash
#hacked by adrian (at) javaguru [dot] org
if [ -f /usr/bin/faad ]
        then
if [ -z $1 ]
        then
                echo "Usage: mp3-to-mp3 / all"
                exit 0
fi
if [ $1 != "all" ]
        then
if [ -f $1 ]
        then
                echo Processing $1
                faad $1
                #filename change
                FNAME_LENGTH=${#1}
                FEXT_INDEX=$((FNAME_LENGTH-4))
                echo Filename length: $FNAME_LENGTH
                WAVFILE=${1:0:$FEXT_INDEX}.wav
                echo $WAVFILE created. Processing...

                # Running lame encoder
                echo Running lame mp3 encoder with 256kbps and HQ settings
                lame -h -b 256 $WAVFILE

                # process filenames
                MP3FILE=$WAVFILE.mp3
                NEWMP3FILE=${MP3FILE:0:$FEXT_INDEX}.mp3
                mv $MP3FILE $NEWMP3FILE
                echo Deleting intermediate WAV file
                rm -v $WAVFILE
                echo $NEWMP3FILE created. Enjoy!
        fi
else
        if [ $1 == "all" ]
                then
                # removing keepvid.com tag
                find . -name "*keepvid*" -exec rename -v 's/\_\[www.keepvid.com\]//'$
                find . -name "*.mp4" -exec mp4-to-mp3 {} \;
        fi

fi
else
        echo /usr/bin/faad missing! Exiting
fi