Thursday, March 31, 2011

youtube-rip

how to convert youtube videos into mp3's
dependencies: ffmpeg, lame, youtube-dl, nautilus
works great in gnome or in console tweak it for your own purposes

#!/bin/bash
ICON=/usr/share/icons/YouTube_icon.png
function notify {
  notify-send -i $ICON "Youtube ripping" "$1";
  echo -e "\033[1m$1\033[0m";
}
function fail {
  notify-send -i $ICON "Error while ripping" "$1";
  echo -e "\033[1m$1\033[0m";
  exit 1;
}
FOLDER=~/Music/YouTube

if ! which lame &>/dev/null; then
fail "lame not installed";
fi
if ! which ffmpeg &>/dev/null; then
fail "ffmpeg not installed";
fi
if ! which youtube-dl &>/dev/null; then
fail "youtube-dl not installed";
fi

if [ ! -d $FOLDER ]; then
  mkdir -p $FOLDER;
fi;

if [ $# -lt 1 ]; then
  nautilus --browser $FOLDER &
  exit 0;
fi
if [ $# -gt 1 ];then
  FOLDER=$2
fi
TMP=`mktemp`
TITLE=`youtube-dl -e $1`
notify "Getting $TITLE"
if [ $? -ne 0 ];then fail $1; fi;
#notify "Found $TITLE"
#notify "Saving video temporarily in $TMP"
youtube-dl -o $TMP $1
if [ $? -ne 0 ];then rm -f $TMP;fail "Error while getting ($TITLE)"; fi;

TMPWAV=`mktemp`
ffmpeg -y -i "$TMP" -v 0 -vn -f wav $TMPWAV 2>/dev/null
if [ $? -ne 0 ];then rm -f $TMP $TMPWAV;fail "Extracting audio from ($TITLE) failed"; fi;
lame -h --vbr-new -V 4 $TMPWAV "$FOLDER/$TITLE.mp3"
if [ $? -ne 0 ];then 
  rm -f $TMP $TMPWAV;
  fail "Encoding of ($TITLE) failed";
else 
  notify "Ripped $TITLE for your pleasure at $FOLDER/$TITLE.mp3" 
fi;
rm -f $TMP $TMPWAV

No comments:

Tip me if you like what you're reading