Skip to content

Instantly share code, notes, and snippets.

@koo5
Created January 22, 2012 11:26
Show Gist options
  • Save koo5/1656635 to your computer and use it in GitHub Desktop.
Save koo5/1656635 to your computer and use it in GitHub Desktop.
my version of mpdtube.pl
#!/usr/bin/perl -w
#
# mpdtube.pl
# version 0.2
# author Johan Ask
#
# Script to download youtube clips, convert to mp3 and add to an mpd playlist.
#
# This script can be run both on the commandline and as a CGI script.
# commandline usage: mpdtube.pl youtube-code title
# http usage: mpdtube.pl?code=xxxxx&title=Name%20of%20song
# youtube code means the value XXX in http://www.youtube.com/watch?v=XXX
#
# Dependencies:
# - perl
# - cpan perl modules: MP3::Tag, Audio::MPD
# - youtube-dl, a python script
# - ffmpeg
#
# TODO:
# - add commandline options and something similar for cgi
# - find out why the tagging won't work
# - start converting to mp3 directly from the stream
# - be able to force redownload
# - any other ideas?
#
use strict;
use Audio::MPD;
use MP3::Tag;
use CGI;
# global variables
my $DIR = "/media/hudba/mpdtube";
my $MPD_HOST = "localhost";
my $MPD_PORT = 6600;
my $VERSION = 0.2;
my $VERBOSE = 1;
my $CGI;
# input variables
my $yt_code;
my $yt_title;
my $query = new CGI;
# check input
if($#ARGV == 1)
{
$yt_code = $ARGV[0];
$yt_title = $ARGV[1];
$CGI = 0;
}
elsif($query->param("code") && $query->param("title"))
{
$yt_code = $query->param("code");
$yt_title = $query->param("title");
$CGI = 1;
}
else
{
output("Wrong input\n");
exit 1;
}
if($CGI)
{
print $query->header();
}
if($yt_code =~ m/[^a-z0-9_]/i)
{
output("Youtube code contains invalid characters\n");
exit 1;
}
# download
my $tmp_file_name = youtube_dl($yt_code, $DIR);
# convert
if(-e "$DIR/$tmp_file_name.flv")
{
flv2mp3("$DIR/$tmp_file_name.flv", "$DIR/$tmp_file_name.mp3");
unlink "$DIR/$tmp_file_name.flv";
add_id3("$DIR/$tmp_file_name.mp3",$yt_title);
}
# and add
mpd_queue("$tmp_file_name.mp3", $DIR);
exit 0;
# only subroutines below
sub youtube_dl
{
my $code = shift;
my $dir = shift;
my $file = "youtube-$code";
my $url = "http://www.youtube.com/watch?v=$code";
if(-e "$dir/$file.mp3")
{
output("MP3-file already exists, skip download\n");
return $file;
}
output("Downloading video file\n");
#exec("youtube-dl -q -c --output=$dir/$file.flv $url") unless fork;
system("youtube-dl -q -c --output=$dir/$file.flv $url");
return $file;
}
sub flv2mp3
{
my $in = shift;
my $out = shift;
output("Converting flv to mp3\n");
system("ffmpeg -i $in -acodec libmp3lame -ac 2 -ab 128k -vn -y $out");
}
sub mpd_queue
{
my $song = shift;
my $dir = shift;
my $mpd = Audio::MPD->new("host" => $MPD_HOST, "port" => $MPD_PORT);
my $stat=`mpc update mpdtube`;
output($stat);
while($stat=~/^Updating DB/m)
{
output(".");
sleep 3;
$stat=`mpc`;
}
output("\n");
output("Adding song to queue: $dir/$song\n");
$mpd->playlist->add("mpdtube/$song");
$mpd->play;
}
sub add_id3
{
my $file = shift;
my $title = shift;
output("Creating id3-tag\n");
my $mp3 = MP3::Tag->new($file);
my $tag = $mp3->new_tag("ID3v1");
$tag->title($title);
$tag->artist("Youtube");
$tag->album("Youtube");
$tag->write_tag();
}
sub output
{
my $str = shift;
if($CGI)
{
$str =~ s/\n/<br\/>/g;
# open(outoputo, '>>/var/lib/mpd/mpdtube/status');
print $str;
}
else
{
if($VERBOSE)
{
print $str;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment