26335 total geeks with 3498 solutions
Recent challengers:
 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
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

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


News Feeds
The Register
Uncle Fester, are
you listening?
Huawei?s nattering
about BUYING Nokia
Home Office boffins
slip out
passport-scanning
Android app
Boffins light way
to photonic
computing with 1TB
DVD tech
Welcome our GIANT
TITANIUM INSECT
OVERLORDS
Hi? Vladimir? It"s
Obama. The hackers
ARE BACK. Hello?
Are you still
there?
Intel joins
Alliance for
Wireless Power
LinkedIn DNS
hijacked, site
offline
Embezzler stings
IBM, Microsoft in
Japan
Quantum boffins and
bread-heads form
new VC fund
NASA serves up
Curiosity"s
billion-pixel
panorama
Slashdot
21 Financial Sites
Found To Store
Sensitive Data In
Browser Disk Cache
US and Russia Set
Up Cyber Cold War
Hotline
Amazon Vows To
Fight Government
Requests For Data
2 Men Accused of
Trying To Make
X-Ray Weapon
Monsanto Executive
Wins World Food
Prize
Microsoft Launches
$100k Bug Bounty
Program
The Plight of Star
Wars Droids
Java API and
Microsoft"s
.NET API: a
Comparison
MakerBot Merging
With Stratasys
Microsoft Kills
Xbox One Phone-Home
DRM
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 11862 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?
Anonymous
2011-02-13 02:50:42
Screen repair for laptops

Sony laptop screen repair
Anonymous
2011-03-31 12:58:59
The code which is provided is really very helpful in case of getting the idea with the long term of programming as well as download of videos and mp3...


Anterior cruciate ligament
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