#!/bin/bash
#
# Author: Kalevi Kolttonen <kalevi@kolttonen.fi>
# Date: November 22nd, 2010
# Purpose: Theoretical demonstration of how
# to create an MP3 file from a YouTube video
#
# No guarantees, not fit for any particular purpose,
# use at your own risk etc. This is freeware.


set -u

YOUTUBE_DL=/usr/local/bin/youtube-dl
MPLAYER=/usr/bin/mplayer
LAME=/usr/bin/lame

if [ $# -ne 2 ]; then
	printf "%s usage: %s [youtube url] [mp3 filename]\n" $(basename $0) $(basename $0)
	exit 1
fi

VIDEO=$$
URL=$1
AUDIO=$2

$YOUTUBE_DL -o $VIDEO.flv $URL &&
$MPLAYER -vo null -ao pcm:file=$AUDIO.wav $VIDEO.flv &&
rm $VIDEO.flv &&
$LAME -h -b 192 $AUDIO.wav $AUDIO.mp3 &&
rm $AUDIO.wav &&
printf "successfully wrote MP3 file %s.mp3\n" $AUDIO ||
exit 2

