22858 total geeks with 3297 solutions
Recent challengers:
best bread maker
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
sefo
anilg, new comments are deleted automaticall y because of some abuse recently
anilg
this is plain wierd. I submitted comments twice to article 950, and they dont seem to be there. Something wrong with the comment code?
CodeX
shout-boxes in general are old + the staff thing happened to everyone after an issue 2 months ago
anilg
/me is no longer staff :(
anilg
Also, osix's shoutbox predated twitter. Heh.

Donate
Donate and help us fund new challenges
Donate!
Due Date: Sep 30
September Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Washington Supremes
deliver death
sentence to betting
site
Google faces
antitrust
investigation in
Texas
It"s alive! Duke
Nukem Forever
breaks out of
vapour trail
Ubuntu "Maverick
Meerkat" erects own
App Store
Doctor Who goes to
the Proms
Unity ? iPhone code
swap approved by
Jobs (for now)
Nigerian man gets
12 years for $1.3m
419 scam
Oz school in
homosexual
kookaburra rumpus
All the week"s
Reg Hardware
reviews
Gordon Brown joins
World Wide Web
Foundation
Slashdot
Software (and
Appropriate Input
Device) For a
Toddler?
Brazil Considering
Legalizing File
Sharing
Game Publishers
Using Stealth P2P
Clients
Winnie-the-Pooh
Parodied In
Wookie-the-Chew
2010 May Be the
First Year YouTube
Turns a Profit
VISA Pulls Plug On
ePassporte, Porn
Webmasters
New and Old
Experiments Combine
To Help the Search
For Life On Mars
NVIDIA Announces
New Line of
Fermi-Based Mobile
Chips
Where Does Dell Go
After Losing 3Par?
Anti-Google Video
Runs In Times
Square
Article viewer

Batch-download YouTube videos to MP3s with ID3 Tags



Written by:ateam
Published by:SAJChurchey
Published on:2009-10-26 16:50:33
Topic:Perl
Search OSI about Perl.More articles by ateam.
 viewed 2748 times send this article printer friendly

Digg this!
    Rate this article :
I am a university student double-majoring in Italian and CompSci. I am very involved with helping the Italian department get up to speed with the 21st century in terms of digital media.

One recent project involved getting 120 YouTube videos into MP3 format for the students and the professor of an Italian Music class. A little research brought me to "youtube-dl." From this and two other tools, I put together a script to take care of this task for me automatically!

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";
}

Did you like this article? There are hundreds more.

Comments:
ffpimp
2009-11-06 21:33:13
You already have to go to Youtube to get the URLs that you're going to download (unless you're just scraping). If you're already doing that, wouldn't it make sense to just download the youtube videos with a firefox plugin?
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
bb
List the number of files in a directory and each subdirectory on Fri 29th Sep 11am
I needed to get the number of files in a directory and recurse through all subdirectories doing the same. I nedded it so i could compare two sets of directory trees for something I was working on. My Irish chum Enstyne came up with this Perl solution w

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Perl: The basics by sefo

Basic knowledge on often used Perl features


     
Your Ad Here
 
Copyright Open Source Institute, 2006