20096 total geeks with 3178 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
Domuk
No, not an issue with the PHP - I was responding to "AJAX not being cross site is annoying"
MaxMouse
Really? i thought that would only be important if the user had some kind of control over where the XML came from, if you hard code it (As in a PHP file) wouldn't that eliminate XSS attacks?
Domuk
Yes, but very, very necessary. AJAX requests run in the context of the browser, there'd be no security if it was cross-domain .
MaxMouse
AJAX not being cross site is annoying, all other scripts can be used in that way, having to resort to PHP to patch it is a shame.
SAJChurchey
thx MaxMouse

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


News Feeds
The Register
MySpace makes peace
with Indies
Nvidia previews
next-gen Fermi GPUs
Potty-mouths
charged for Comcast
hijack
Microsoft
Silverlight - now
with hidden Windows
bias
Apple cult leader
emails outside
world
Sony demos monster
3D TV
Wrecking CRU:
hackers cause
massive climate
data breach
Skinny Acer
notebook delivers
six-day battery
life
VTOL gyro-copter
flying car mates
with killer robot
Oracle begs EC for
more time
Slashdot
iPhone Owners
Demand To See Apple
Source Code
Proton Beams Sent
Around the LHC
Microsoft"s Lack of
Nightly Builds For
IE
Some Claim Android
App Store Worse
Than iPhone"s
Climatic Research
Unit Hacked, Files
Leaked
Aging Nuclear
Stockpile Good For
Decades To Come
Netbooks Have
Higher Failure Rate
Than Laptops
Xbox Live Class
Action Being
Investigated
Patent Issued For
Podcasting
Linus Torvalds For
Nobel Peace Prize?
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 387 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

Related Links:
Mandriva Linux Security Update Advisory - perl (MDKSA-2005:079)
..
Debian Security Advisory - perl (DSA 696-1)
..
Randal Schwartzs Perls of Wisdom
r3lody (Raymond Lodato) writes Anyone who has been working on the *nix platform has had a brush with Perl, the scripting language whose acronym (depending on who you ask) could mean Practical Extraction and Report Language, or Pathologically Eclectic..
Ubuntu Security Notice - perl vulnerability (USN-94-1)
..
Firefox Plugin Annodex For Searching Audio, Video
loser in front of a computer writes ZDNet Australia reports that Australias CSIRO research organisation has developed a Firefox plugin named Annodex that allows browsing through time-continuous media such as audio and video in the same way that HTML ..
Trustix Secure Linux Security Advisory - bind clamav cpio cups mod_python perl postgresql python squ
..
Mandrakelinux Security Update Advisory - perl (MDKSA-2005:031)
..
Ubuntu Security Notice - perl vulnerabilities (USN-72-1)
..
OpenPKG Security Advisory - perl (OpenPKG-SA-2005.001)
..
Debian Security Advisory - perl (DSA 620-1)
..
Perl Relational Operators
Perl If Elsif Else
Perl 107
Perl 106
Perl 105
Perl 104
Perl 103
Perl 102
Perl 101 (Repeat)
Perl 101


     
Your Ad Here
 
Copyright Open Source Institute, 2006