This script parses a textfile containg YouTube video ID numbers. It
downloads the FLV file (the YouTube video) for each ID in the list.
Usage:
youtube-grabber.pl <textfile>
Finding a YouTube video's ID is simple. Let's take the following YouTube
URL as an example:
http://www.youtube.com/watch?v=OCjs3Se0XTc
http://www.youtube.com/watch?v= [ OCjs3Se0XTc ]
The string of numbers and letters that follows "?v=" is this video's ID.
So, this video's ID is "OCjs3Se0XTc."
It's important to note that, often, a YouTube URL will contain extra
parameters after the ID. For example:
http://www.youtube.com/watch?v=BrEYMP2rb3c&feature=featured&fmt=18
http://www.youtube.com/watch?v= [ BrEYMP2rb3c ] &feature=featured&fmt=18
The is always the string following "?v=". In this and all other multi-
parameter YouTube URLs, the ID ends before the first ampersand (&). So,
this video's ID is "BrEYMP2rb3c."
Once you have a video's ID, you're ready to create your textfile. Each
line of the file will contain a video ID, artist name and song title.
Below is an example textfile:
W-V2Cm8CLJ4|Tony D'Aloia|Napoli
tfsisahydZw|Antonella|Arrivera' L'Estate
Am-HUOiRWXo|Antonella|La Mia Poesia
The video ID, artist name and song title are separated by the pipe
character (|), which can be entered using the "Shift" + "\" key
combination.
The next step is taken care of by the script! Each FLV file is converted
to an MP3 and tagged using the artist name and song title from your
textfile.
Requirements:
youtube-dl
http://www.backdrifts.net/files/texts/perl/youtube-dl
http://bitbucket.org/rg3/youtube-dl/wiki/Home
apt-get install youtube-dl
flv2mp3
http://www.backdrifts.net/files/texts/perl/flv2mp3
id3tool
http://nekohako.xware.cx/id3tool/
apt-get install id3tool
Example textfile:
http://www.backdrifts.net/files/texts/perl/videolist
TODO:
-Support conversion to higher bitrate MP3 (requires retrieving HD FLV file).
-Support downloading entire YouTube "Playlists."
# Use strict!
use strict;
# Textfile parameter.
my $textfile = @ARGV[0];
# The "vids" array stores each line of the textfile.
my @vids;
# The "track" array that each line of the textfile is split into.
my @track;
# The variables used for storing information on each YouTube video.
my $id, my $artist, my $title, my $url, my $file, my $i;
# Find paths to required binaries.
chop(my $id3tool = `whereis id3tool | awk '{ print \$2}'`);
chop(my $youtube-dl = `whereis youtube-dl | awk '{ print \$2}'`);
chop(my $flv2mp3 = `whereis flv2mp3 | awk '{ print \$2}'`);
# Print welcome message.
print "youtube-grabber 0.05\n";
print "Written by Derek Pascarella\n\n";
# If one of the required binaries doesn't exist, print an error.
if($id3tool eq "" || $youtube-dl eq "" || $flv2mp3 eq "")
{
print "Error!\n";
print "Your system is missing required binaries.\n\n";
print "id3tool: ";
if($id3tool ne "")
{
print "found\n";
}
else
{
print "missing\n";
}
print "youtube-dl: ";
if($youtube-dl ne "")
{
print "found\n";
}
else
{
print "missing\n";
}
print "flv2mp3: ";
if($flv2mp3 ne "")
{
print "found\n";
}
else
{
print "missing\n";
}
}
# If no textfile was specified, print an error.
elsif($textfile eq "")
{
print "Error!\n";
print "Usage: youtube-grabber.pl <textfile>\n";
}
# If a textfile was specified and exists, proceed.
elsif($textfile ne "" && -e $textfile)
{
# Open textfile containing YouTube video information.
open(TEXTFILE, "$textfile");
chop(@vids = <TEXTFILE>);
close(TEXTFILE);
# Print number of videos found in textfile.
print $#vids + 1 . " video ID(s) found in \"$textfile\"\n\n";
# Parse through each line of the textfile.
for($i = 0; $i <= $#vids; $i ++)
{
# Split current line by the pipe character (|).
@track = split(/\|/, @vids[$i]);
# Parse out the video ID, artist and song title.
$id = @track[0];
$artist = @track[1];
$title = @track[2];
# Create "url" and "file" variables.
$url = "http://www.youtube.com/watch?v=" . $id;
$file = $id . ".flv";
# Print the artist and song title for the YouTube video
# currently being worked on.
print $i + 1 . ". $artist - $title\n";
# Download FLV file from YouTube.
print "\nyoutube-dl \"$url\"\n";
system "$youtube-dl \"$url\"";
# Convert the FLV file to an MP3.
print "\nflv2mp3 $file \"$artist - $title.mp3\"\n";
system "$flv2mp3 $file \"$artist - $title.mp3\"";
# Tag the MP3 using the artist and song title information
# from the textfile.
print "\nid3tool -r \"$artist\" -t \"$title\" \"$artist - $title.mp3\"\n";
system "id3tool -r \"$artist\" -t \"$title\" \"$artist - $title.mp3\"";
# Remove the FLV file.
print "\nCleaning up...\n";
system "rm $file";
# Sleep 2 seconds before the next download begins, reducing
# timeouts.
print "\nSleeping 2 seconds before beginning next download...\n\n";
system "sleep 2";
}
print "Process complete!\n\n";
print "Enjoy :)\n";
}
# If the textfile doesn't exist, print an error.
else
{
print "Error!\n";
print "Your textfile \"$textfile\" does not exist!\n";
}
|