Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Youtube Downloader - Perl Code Bank


Youtube Downloader
Search and download youtube videos with 'wget'.
                #!/usr/bin/perl
#
# Copyright (C) 2010-2011 Trizen <echo dHJpemVueEBnbWFpbC5jb20K | base64 -d>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#-------------------------------------------------------
#  Coded by Trizen
#  Created on: 01 Jun 2010
#  Latest edit on: 22 May 2011
#  Website: http://trizen.go.ro
#  Email: echo dHJpemVueEBnbWFpbC5jb20K | base64 -d
#-------------------------------------------------------
#

use URI::Escape;
use HTML::Entities;

my $appname = 'Youtube Downloader';
my $version = '1.8.5';

sub help {
    print "
\t----------------- $appname $version -----------------
\t
Usage: youtube-downloader || <url> || <code>
\nBase Options: youtube-downloader [...]
   <url>                : download video(s) by URL(s)
   <code>               : download video(s) by code(s)
   <keywords>           : search and download YouTube video(s)
   -v, --version        : prints version and exits
   -h, --help           : prints help and exits
\nResolution options:
 --240, -2  : search and download videos always at 240p resolution
 --360, -3  : search and download videos always at 360p resolution
 --480, -4  : search and download videos always at 480p resolution
 --720, -7  : search and download videos always at 720p resolution
 --1080 -1  : search and download videos always at 1080p resolution
 --best     : choose the best quality available (same as --1080)
\nOther options:
   -t, --tops           : shows today Youtube tops
   -a, --all-time       : shows all time Youtube tops
   -c, --categories     : shows available YouTube categories
   -A, --all            : download all video results in order
   -u <username>        : lists videos uploaded by a specific user
   -m, --more           : insert more youtube URLs via STDIN
   --file=FILENAME      : download videos from a list of Youtube URLs
\nTips and Tricks:
  1. After search results, press <ENTER> for the next page
  2. After search results, insert more numbers to download videos in your order
\n";
    exit;
}

sub version {
    print "$appname $version\n";
    exit;
}
$os = $^O;
if ( not $os =~ /(?:win|dos)/i ) {
    $bred   = "\e[1;31m";
    $bgreen = "\e[1;32m";
    $reset  = "\e[0m";
}
else {
    $bred   = '';
    $bgreen = '';
    $reset  = '';
}
my $pick;
foreach $arg (@ARGV) {
    if ( $arg =~ /^-/ ) {
        $SearchBackup .= " $arg ";
    }
    else {
        $Search .= "$arg ";
    }
}
foreach $_ (@ARGV) {
    if ( $_ =~ /^[-]+(?:colou?rs|co|cl)$/ ) {
        $colours = 1;
        next;
    }
    if ( $_ =~ /^-+(?:https)$/ ) {
        $https = 1;
        next;
    }
    if ( $_ =~ /^[-]+(?:A|all)$/ ) {
        $playback = 1;
        next;
    }
    if ( $_ =~ /^[-]+(?:a|all.?time)$/ ) {
        $all_time = 1;
        next;
    }
    if ( $_ =~ /^-+debug$/ ) {
        $debug = 1;
        next;
    }
    if ( $_ =~ /^[-]+(?:2|240p?)$/ ) {
        $res_240p = 1;
    }
    elsif ( $_ =~ /^-+(?:3|360p?)$/ ) {
        $res_360p = 1;
    }
    elsif ( $_ =~ /^-+(?:4|480p?)$/ ) {
        $res_480p = 1;
    }
    elsif ( $_ =~ /^-+(?:7|720p?)$/ ) {
        $res_720p = 1;
    }
    elsif ( $_ =~ /^-+(?:1|1080p?|best)$/ ) {
        $res_1080p = 1;
    }
}
if ( $colours and not $os =~ /(?:win|dos)/i ) {
    $cblack  = "\e[40m";
    $byellow = "\e[1;33m";
    $bpurle  = "\e[1;35m";
    $bblue   = "\e[1;34m";
}
else {
    $bblue   = '';
    $cblack  = '';
    $bpurle  = '';
    $byellow = '';
}
foreach $_ (@ARGV) {
    ++$i;
    next unless $_ =~ /^-/;
    if ( $_ =~ /^[-]+(?:h|help|\?|usage)$/ ) {
        &help;
    }
    elsif ( $_ =~ /^-+(?:v|version)$/ ) {
        &version;
    }
    elsif ( $_ =~ /^[-]+(?:m|more)$/ ) {
        &more_urls;
    }
    elsif ( $_ =~ /^[-]+(?:a|all[_-]?time|t|tops)$/ ) {
        &youtube_tops;
    }
    elsif ( $_ =~ /^[-]+(?:u|username)$/ ) {
        if ( $ARGV[$i] ) {
            unless ( $ARGV[$i] =~ /^-/ ) {
                $user = $ARGV[$i];
            }
        }
        unless ($user) {
            $i -= 2;
            if ( $ARGV[$i] ) {
                unless ( $ARGV[$i] =~ /^-/ ) {
                    $user = $ARGV[$i];
                }
            }
        }
        if ($user) {
            &videos_from_username($user);
        }
    }
    elsif ( $_ =~ /^[-]+(?:c|categories)$/ ) {
        &category_area;
    }
    elsif ( $_ =~ /^[-]+(?:file|list)=(.+)/ ) {
        $file_name = $1;
        &urls_from_list;
    }
}

sub UserAgent {
    require LWP::UserAgent;
    $lwp = 'LWP::UserAgent'->new;
    $lwp->agent('Mozilla/5.0 (X11; U; Linux i686; en-US) Chrome/10.0.648.45');
    $lwp->env_proxy;
    $lwp->timeout(10);
    $lwp_is_set = 1;
}

sub urls_from_content {
    foreach $line (@content) {
        if ( $line =~ m[youtube\.com.*(?:v|embed)[=/]*([\w-]{11})] ) {
            $parallel_downloads = 1;
            &GetYouTube($1);
        }
    }
}

sub urls_from_list {
    die $! unless open FILE, $file_name;
    while ( defined( $_ = <FILE> ) ) {
        print "$_\n";
        if ( $_ =~ m[(?:v|embed)?[=/]?([\w-]{11})] ) {
            $parallel_downloads = 1;
            &GetYouTube($1);
        }
        elsif ( $_ =~ /^([\w-]{11})$/ ) {
            $parallel_downloads = 1;
            &GetYouTube($1);
        }
    }
    close FILE;
}

sub more_urls {
    $more_urls = 1;
    print "\n$bred=>>$bgreen Insert few Youtube URLs:$reset\n";
    until ( $stdin eq "\n" ) {
        print '> ';
        $stdin = <STDIN>;
        $urls .= $stdin;
    }
    foreach $_ ( split( /\n/, $urls, 0 ) ) {
        if ( $_ =~ m[(?:v|embed)?[=/]?([\w-]{11})] ) {
            $parallel_downloads = 1;
            &GetYouTube($1);
        }
    }
}

sub code_from_content {
    unless ($lwp_is_set) {
        &UserAgent;
    }
    $connect = $lwp->get( $_[0] ) if $_[0];
    if ( $connect->content =~ m[youtube\.com/(?:v|embed)/([\w-]{11})] ) {
        &GetYouTube($1);
    }
}
foreach $code (@ARGV) {
    if ( $code =~ /^([\w-]{11})$/ ) {
        $code = $1;
        if ( $code =~ /[\d]+|[A-Z]+|-/ ) {
            $dont_exit = 1;
            &GetYouTube($code);
        }
    }
    elsif ( $code =~ /view_play_list\?p=([A-Z0-9]+)/ ) {
        $playlist = 1;
        &list_playlist($1);
    }
    elsif ( $code =~ m[(?:v|embed)[=/]+([\w-]{11})] ) {
        $code      = $1;
        $dont_exit = 1;
        if ( length $code == 11 ) {
            &GetYouTube($code);
        }
        else {
            next;
        }
    }
    elsif ( $code =~ m[^https?://] and not $code =~ /youtube\.com/ ) {
        &code_from_content($code);
    }
    else {
        next;
    }
}
foreach $_ (@ARGV) {
    if ( not $_ =~ /^-/ or length $_ == 11 ) {
        $non_argv = 1;
        last;
    }
}
if ($more_urls) {
    my $quit = 0;
    until ( $quit =~ /^(?:quit|exit)$/ ) {
        chomp( $quit = <STDIN> );
    }
    exit;
}
unless ( $non_argv or $more_urls ) {
    &InsertURL;
}

sub InsertURL {
    print "\n$bred=>>$reset$bgreen "
      . "Insert an YouTube URL or search something...\n$reset> ";
    chomp( $youtube = <STDIN> );
    if ( $youtube =~ m[(?:v|embed)[=/]+([\w-]{11})] ) {
        &GetYouTube($1);
    }
    elsif ( $youtube =~ /view_play_list\?p=([A-Z0-9]+)/ ) {
        $playlist = 1;
        &list_playlist($1);
    }
    elsif ( $youtube =~ m[^https?://] and not $youtube =~ /youtube\.com/ ) {
        &code_from_content($youtube);
    }
    elsif ($youtube) {
        $SearchBackup .= ' ' . $youtube;
        $Search = $youtube;
        $number = 0;
        &search;
    }
    else {
        warn "\n$bred(x_x) Unable to continue...$reset\n\n";
        exit;
    }
}

sub make_https {
    my $https_url = shift @_;
    $https_url =~ s[^http://][https://];
    return $https_url;
}

sub list_playlist {
    if ( $_[0] and $_[0] =~ /^([A-Z0-9]+)$/ ) {
        $yt_api_url = "http://gdata.youtube.com/feeds/api/playlists/$1";
        $yt_api_url = make_https($yt_api_url) if $https;
        &YoutubeAPI;
    }
}

sub youtube_tops {
    print "\n";
    $n = 0;
    unless ($all_time) {
        $today = 1;
    }
    $youtube_top_url = 'http://gdata.youtube.com/feeds/api/standardfeeds';
    $youtube_top_url = make_https($youtube_top_url) if $https;
    foreach my $line (
        'top_rated',      'top_favorites',
        'most_viewed',    'most_popular',
        'most_recent',    'most_discussed',
        'most_responded', 'recently_featured'
      ) {
        ++$n;
        my $top_name = ucfirst $line;
        $top_name =~ s/_/ /;
        print "$bred$n$reset - $top_name\n";
        push @tops, "$n - $youtube_top_url/$line";
    }
    print "\n$bred=>>$bgreen Pick one of$reset\n> ";
    chomp( my $pick = quotemeta <STDIN> );
    foreach $_ (@tops) {
        if ( $_ =~ /^$pick - (.+)/ ) {
            $yt_api_url = $1;
            if ($today) {
                unless ( $yt_api_url =~ /recent/ ) {
                    $yt_api_url .= '?time=today';
                }
            }
            &YoutubeAPI;
        }
    }
}

sub videos_from_username {
    $yt_api_url = "http://gdata.youtube.com/feeds/api/users/$_[0]/uploads";
    $yt_api_url = make_https($yt_api_url) if $https;
    &YoutubeAPI;
}

sub category_area {
    unless ($lwp_is_set) {
        &UserAgent;
    }
    $categories_url = 'http://gdata.youtube.com/schemas/2007/categories.cat';
    $categories_url = make_https($categories_url) if $https;
    $connect        = $lwp->get($categories_url);
    @cates          = split( /category term=/, $connect->content, 0 );
    $n              = 0;
    print "\n";
    foreach my $cat (@cates) {

        if ( $cat =~ /^'([^']+)' label='([^']+)'/ and not $cat =~ /deprecated/ )
        {
            ++$n;
            my $cat_name  = $1;
            my $cat_label = $2;
            $cat_label =~ s/&amp;/&/;
            if ( $n < 10 ) {
                print ' ';
            }
            print "$bred$n$reset - $cat_label\n";
            push @categories, "$n - $cat_name";
        }
    }
    print "\n$bgreen=>> Insert a category number\n>$reset ";
    chomp( $pickcat = quotemeta <STDIN> );
    foreach my $cat (@categories) {
        if ( $cat =~ /^$pickcat - (.+)/ ) {
            $yt_api_url = 'http://gdata.youtube.com/feeds/api/standardfeeds/'
              . "recently_featured_$1";
            $yt_api_url = make_https($yt_api_url) if $https;
            &YoutubeAPI;
        }
    }
}

sub YoutubeAPI {
    $#Videos = -1;
    if ($MoreResults) {
        $results = 50;
    }
    else {
        $results = 20;
    }
    $start_index = 1;
    if ( not $yt_api_url =~ /\?/ ) {
        $yt_api_url .= "?start-index=$start_index&max-results=$results";
    }
    else {
        $yt_api_url .= "&start-index=$start_index&max-results=$results";
    }
    unless ($lwp_is_set) {
        &UserAgent;
    }
    &Parse($yt_api_url);
    &PrintResults;
}
unless ($dont_exit) {
    &search;
}

sub search {
    $PageNumber = 1;
    $FirstPage  = 1;
    $Search =~ s/^\s+|\s+$//g;
    $Search = uri_escape($Search);
    if ($MoreResults) {
        $results = 50;
    }
    else {
        $results = 20;
    }
    $ys = "http://gdata.youtube.com/feeds/api/videos?q=$Search"
      . "&max-results=$results";
    $ys = make_https($ys) if $https;
    $start_index = 1;
    $ys .= "&start-index=$start_index";
    unless ($lwp_is_set) {
        &UserAgent;
    }
    &Parse($ys);
    &PrintResults;
}

sub Parse {
    $content = $lwp->get( $_[0] )->content;
    $#Videos = -1;
    @content = split( /<entry>/, $content, 0 );
    foreach $_ (@content) {
        if ($debug) {
            print "$_\n\n";
        }
        study $_;
        next unless $_ =~ /^<id>/;
        if ( $_ =~ m[<name>([^<]+)</name>] ) {
            $video_line = "<author=$1>";
        }
        if ( $_ =~ m[v=([\w-]+)&amp;feature=youtube_gdata'/>] ) {
            $video_line .= "<v=$1>";
        }
        if ( $_ =~ m[>([^<]+)</title>] ) {
            $video_line .= "<title=$1>";
        }
        if ( $_ =~ /<media:category label=['"]([^']+)['"]/ ) {
            $category_label = $1;
            $category_label =~ s/&amp;/&/;
            $video_line .= "<category=$category_label>";
        }
        else {
            $video_line .= '<category=None>';
        }
        if ( $_ =~ /<published>([\d-]+)/ ) {
            $video_line .= "<published=$1>";
        }
        else {
            $video_line .= '<published=0>';
        }
        if ( $_ =~ /duration[^']+'(\d+)'/ ) {
            $video_line .= "<time=$1>";
        }
        else {
            $video_line .= '<time=0>';
        }
        if ( $_ =~ /viewCount='(\d+)'/ ) {
            $video_line .= "<views=$1>";
        }
        else {
            $video_line .= '<views=0>';
        }
        if ( $_ =~ /<media:description type='plain'>([^<]+)/ ) {
            $video_line .= "<description=$1>";
        }
        else {
            $video_line .= '<description=No description available...>';
        }
        if ($debug) {
            print "$video_line\n\n";
        }
        push @Videos, $video_line;
    }
}

sub PrintResults {
    $#codes = -1;
    my $number = 0;
    print "\n";
    foreach $line (@Videos) {
        if (
            $line =~ /^<author=([^>]+)><v=([^>]+)><title=([^>]+)>
<category=([^>]+)><published=([^>]+)><time=
([^>]*)><views=([^>]*)><description=([^>]+)>$/x
          ) {
            ++$number;
            my $author      = $1;
            my $code        = $2;
            my $title       = decode_entities($3);
            my $category    = $4;
            my $published   = $5;
            my $time        = format_time($6);
            my $views       = $7;
            my $description = $8;
            push @codes,
                "$number>title=$title>published=$published>"
              . "category=$category>time=$time>code="
              . "$code>views=$views>desc=$description>";
            print "$bred";

            if ( $number < 10 ) {
                unless ($video_results) {
                    $video_results = 1;
                }
                print "$cblack ";
            }
            print "$cblack$bred$number$reset$cblack - $byellow$title$reset"
              . "$cblack (${bpurle}by $author$reset$cblack) "
              . "($bblue$time$reset$cblack)$reset\n";
        }
    }
    if ($playback) {
        for ( $i = 1 ; $i <= $number ; ++$i ) {
            push @picks2, "$i ";
        }
        $youtube = join( '', @picks2 );
        @picks = split( ' ', $youtube, 0 );
        &PicksArea;
    }
    print "\n$bred=>>$reset$bgreen Insert a number or "
      . "search something else\n$reset> ";
    chomp( my $youtube = <STDIN> );
    $PickBackup = $youtube;
    if ( $youtube =~ /^(?:q|quit|exit)$/ ) {
        exit;
    }
    elsif ( $PickBackup =~ m[^https?://] and not $PickBackup =~ /youtube\.com/ )
    {
        &code_from_content($PickBackup);
    }
    if ( $youtube =~ /^\s*([\d]+)\s*$/ ) {
        if ( $youtube > $number or not $youtube ) {
            print "\n";
            &PrintResults;
        }
        else {
            &ForeachCode($1);
        }
    }
    elsif ( $youtube =~ /^\s*$/ ) {
        $PageNumber = $FirstPage;
        ++$PageNumber;
        if ($ys) {
            if ( $ys =~ /index=([\d]+)/ ) {
                $old_index   = $1;
                $start_index = $old_index + $results;
                $ys =~ s/index=$old_index/index=$start_index/;
                &Parse($ys);
                &PrintResults;
            }
        }
        elsif ($yt_api_url) {
            $#Videos = -1;
            $ys      = '';
            if ( $yt_api_url =~ /index=([\d]+)/ ) {
                $old_index   = $1;
                $start_index = $old_index + $results;
                $yt_api_url =~ s/index=$old_index/index=$start_index/;
                &Parse($yt_api_url);
                &PrintResults;
            }
        }
    }
    elsif ( $youtube eq 'all' ) {
        $SearchBackup .= ' --1080 --play-all';
        $#picks = -1;
        for ( $i = 1 ; $i <= $number ; ++$i ) {
            push @picks, "$i";
        }
        &PicksArea;
    }
    elsif ( $youtube =~ /view_play_list\?p=([A-Z0-9]+)/ ) {
        &list_playlist($1);
    }
    elsif ( $youtube =~ m[(?:v|embed)[=/]+([\w-]{11})] ) {
        &GetYouTube($1);
    }
    elsif ( $youtube =~ /\d+[,\s]+\d+/ ) {
        @picks = split( m?\D?, $youtube, 0 );
        &PicksArea;
    }
    elsif ($youtube) {
        $Search = $youtube;
        &search;
    }
}

sub PicksArea {
    $NrOfPicks = $#picks + 1 if $#picks;
    $no        = 0;
    $lastpick  = '';
    &ForeachPick;
}

sub ForeachPick {
    for ( $number = $no ; $number <= $NrOfPicks ; ++$number ) {
        $no   = $number;
        $pick = $picks[$number];
        if ($pick) {
            next if $lastpick eq $pick;
        }
        &PrintResults if $number eq $NrOfPicks;
        $Picks    = 1;
        $lastpick = $pick;
        &ForeachCode($pick);
    }
}

sub ForeachCode {
    foreach $line (@codes) {
        if ($debug) {
            print "** FOREACHCODE: $line\n\n";
        }
        if (
            $line =~ /^$_[0]>title=([^>]+)>published=([^>]+)>
category=([^>]+)>time=([^>]+)>code=([\w-]{11})>
views=([\d]*)>desc=([^>]+)>$/x
          ) {
            my $description = decode_entities($7);
            &GetYouTube( $5, $1, $4, $6, $3, $2, $description );
        }
    }
}

sub format_time {
    my $sec = shift @_ || return (0);
    my $time = $sec / 3600 % 24 . ':' . $sec / 60 % 60 . ':' . $sec % 60;
    $time =~ s/^0://;
    unless ( $time =~ /:/ ) {
        $time = "0:$time";
    }
    if ( $time =~ /(.):(.):(.+)/ ) {
        $time = "$1:0$2:$3";
    }
    if ( $time =~ /^(.+):(.)$/ ) {
        $time = "$1:0$2";
    }
    return $time;
}

sub Description {
    study $connect->content;
    my ( $date, $rating );
    if ( $connect->content =~ /rating average='([\d\.]+)'/ ) {
        $rating = $1;
        $rating =~ s/^([\d\.]{4}).*/$1/;
    }
    elsif ( $connect->content =~ /avg_rating=([^&]+)/ ) {
        $rating = $1;
        $rating =~ s/^([\d\.]{4}).*/$1/;
    }
    unless ($title) {
        my $feed_url = 'http://gdata.youtube.com/feeds/api/videos/' . $code;
        $feed_url = make_https($feed_url) if $https;
        my $content = $lwp->get($feed_url)->content;
        study $content;
        if ( $content =~ m[media:title type='plain'>([^<]+)</media:title>] ) {
            $title = decode_entities($1);
        }
        if ( $content =~ /viewCount='([\d]+)'/ ) {
            $views = $1;
        }
        if ( $content =~ /<published>([\d-]+)/ ) {
            $date = $1;
            $date =~ s/-/./g;
            $date =~ s/(.+)\.(.+)\.(.+)/$3.$2.$1/;
        }
        if ( $content =~ m[<media:description type='plain'>([^<]+)</media] ) {
            $description = decode_entities($1);
        }
        if ( $content =~ m[<author><name>([^<]+)</name>] ) {
            $author = $1;
        }
        if ( $content =~ /duration[^\d]+(\d+)/ ) {
            $duration = format_time($1);
        }
        if ( $content =~ /category label='([^']+)'/ ) {
            $category = $1;
            $category =~ s/&amp;/&/;
        }
    }
    if ( not $date and $published ) {
        $date = $published;
        $date =~ s/-/./g;
        $date =~ s/(.+)\.(.+)\.(.+)/$3.$2.$1/;
    }
    if ($views) {
        $views = reverse($views);
        $views =~ s/([\d]{3})/$1./g;
        $views = reverse($views);
        $views =~ s/^\.//;
    }
    if ($description) {
        print "\n$bred=>> " . $bgreen . $title . $reset . "\n" . '-' x 80;
        print "\n" . $description . "\n" . '-' x 80 . "\n";
    }
    else {
        print "\n";
    }
    print "* URL $url\n" . '-' x 80 . "\n";
    my $count = length $title;
    if ( $count <= 40 ) {
        print "$bred\t\t=>> ";
    }
    elsif ( $count >= 40 and $count <= 55 ) {
        print "$bred\t=>> ";
    }
    elsif ( $count >= 56 ) {
        print "$bred  =>> ";
    }
    print "$bgreen$title$reset$bred <<=\n$reset";
    $author = $1 if $connect->content =~ /&author=([^&]+)/;
    print "\n** Author    : $author\n" if $author;
    print "** Category  : $category\n" if $category;
    print "** Duration  : $duration\n" if $duration;
    print "** Rating    : $rating\n"   if $rating;
    print "** Views     : $views\n"    if $views;
    print "** Published : $date\n"     if $date;
    print '-' x 80 . "\n\n";
}

sub GetYouTube {
    ( $code, $title, $duration, $views, $category, $published, $description ) =
      @_;
    $#_ = -1;
    unless ($lwp_is_set) {
        &UserAgent;
    }
    $url = 'http://www.youtube.com/watch?v=' . $code;
    my $youtube =
        'http://www.youtube.com/get_video_info?&video_id='
      . $code
      . '&el=detailpage&ps=default&eurl=&gl=US&hl=en';
    $youtube = make_https($youtube) if $https;
    $connect = $lwp->get($youtube);
    if ( $connect->content =~ /fmt_url_map=([^&]+)&/ ) {
        $download = $1;
        $download =~ s/%3A/:/gi;
        $download =~ s[%2F][/]gi;
        $download =~ s/%26/&/g;
        $download =~ s/%3D/=/gi;
        $download =~ s/%3F/?/gi;
        $download =~ s/%252C/,/gi;
    }
    else {
        warn "$bred
\nURL: http://www.youtube.com/watch?v=$code
\n(x_x) Something went wrong...
\n$reset";
        sleep 1;
    }
    &Description;
    if ($res_240p) {
        &flv_240p;
    }
    elsif ($res_360p) {
        &mp4_360p;
    }
    elsif ($res_480p) {
        &mp4_480p;
    }
    elsif ($res_720p) {
        &mp4_720p;
    }
    elsif ($res_1080p) {
        &mp4_1080p;
    }
    else {
        print '=' x 23;
        print "
   | ${bred}1$reset - FLV (240p)
   | ${bred}2$reset - FLV (360p)
   | ${bred}3$reset - MP4 (360p)";
        if ( $download =~ /itag=35/ ) {
            print "\n   | ${bred}4$reset - FLV (480p)";
        }
        if ( $download =~ /itag=22/ ) {
            print "\n   | ${bred}5$reset - MP4 (720p)";
        }
        if ( $download =~ /itag=37/ ) {
            print "\n   | ${bred}6$reset - MP4 (1080p)";
        }
        print "\n" . '=' x 23 . "\n\n";
        print "$bred=>>$reset Pick format: ";
        chomp( $pick = <STDIN> );
        $pick = 6 unless $pick =~ /\d/;
        print "\n";
        if ( $pick == 1 ) {
            &flv_240p;
        }
        elsif ( $pick == 2 ) {
            &flv_360p;
        }
        elsif ( $pick == 3 ) {
            &mp4_360p;
        }
        elsif ( $pick == 4 ) {
            &mp4_480p;
        }
        elsif ( $pick == 5 ) {
            &mp4_720p;
        }
        elsif ( $pick == 6 ) {
            &mp4_1080p;
        }
    }
}

sub flv_240p {
    if ( $download =~ /itag=5/ ) {
        $download =~ s/(.*)http(.+)itag=5([^%]*)(.*)/http$2itag=5$3/;
        $format = 'flv';
        &wget;
    }
}

sub flv_360p {
    if ( $download =~ /itag=34/ ) {
        $download =~ s/(.*)http(.+)itag=34([^%]*)(.+)/http$2itag=34$3/;
        $format = 'mp4';
        &wget;
    }
    elsif ( $download =~ /itag=5/ ) {
        &flv_240p;
    }
}

sub mp4_360p {
    if ( $download =~ /itag=18/ ) {
        $download =~ s/(.*)http(.+)itag=18([^%]*)(.+)/http$2itag=18$3/;
        $format = 'mp4';
        &wget;
    }
    elsif ( $download =~ /itag=34/ ) {
        &flv_360p;
    }
    elsif ( $download =~ /itag=5/ ) {
        &flv_240p;
    }
}

sub mp4_480p {
    if ( $download =~ /itag=35/ ) {
        $download =~ s/(.*)http(.+)itag=35([^%]*)(.+)/http$2itag=35$3/;
        $format = 'flv';
        &wget;
    }
    elsif ( $download =~ /itag=18/ ) {
        &mp4_360p;
    }
    elsif ( $download =~ /itag=34/ ) {
        &flv_360p;
    }
    elsif ( $download =~ /itag=5/ ) {
        &flv_240p;
    }
}

sub mp4_720p {
    if ( $download =~ /itag=22/ ) {
        $download =~ s/(.*)http(.+)itag=22([^%]*)(.+)/http$2itag=22$3/;
        $format = 'mp4';
        &wget;
    }
    elsif ( $download =~ /itag=35/ ) {
        &mp4_480p;
    }
    elsif ( $download =~ /itag=18/ ) {
        &mp4_360p;
    }
    elsif ( $download =~ /itag=34/ ) {
        &flv_360p;
    }
    elsif ( $download =~ /itag=5/ ) {
        &flv_240p;
    }
}

sub mp4_1080p {
    if ( $download =~ /itag=37/ ) {
        $download =~ s/(.*)http(.+)itag=37([^%]*)(.+)/http$2itag=37$3/;
        $format = 'mp4';
        &wget;
    }
    elsif ( $download =~ /itag=22/ ) {
        &mp4_720p;
    }
    elsif ( $download =~ /itag=35/ ) {
        &mp4_480p;
    }
    elsif ( $download =~ /itag=18/ ) {
        &mp4_360p;
    }
    elsif ( $download =~ /itag=34/ ) {
        &flv_360p;
    }
    elsif ( $download =~ /itag=5/ ) {
        &flv_240p;
    }
}

sub wget {
    $title =~ s[/][|]g;
    if ( not -e "$title.$format" ) {
        warn "** Downloading '$title.${format}'\n";
    }
    else {
        warn "** '$title.${format}' already exists\n";
        sleep 1;
    }
    if ($parallel_downloads) {
        unless ($threads_required) {
            require threads;
            $threads_required = 1;
        }
        'threads'->new(
            sub {
                unless ( -e "$title.$format" ) {
                    $lwp->mirror( "$download", "$title.$format" );
                }
            }
        );
    }
    else {
        my $tmp_download_url = make_https($download) if $https;
        my $tmp_headers = $lwp->head($tmp_download_url)->headers if $https;
        if ($https) {
            if ( $tmp_headers->content_length ) {
                print "** HTTPS is possible\n";
                $download = $tmp_download_url;
            }
            elsif ($https) {
                print "** HTTPS is not possible\n";
            }
        }
        unless ( -e "$title.$format" ) {
            if ( -x '/usr/bin/wget' ) {
                $title = quotemeta $title;
                my $wget_line = "wget '${download}' -O $title.$format";
                $wget_line .= ' --no-check-certificate' if $https;
                system $wget_line;
            }
            else {
                my $result         = $lwp->head($download);
                my $remote_headers = $result->headers;
                my $video_size     = $remote_headers->content_length / 1048576;
                print "** Video size: $1 MB\n" if $video_size =~ /^(.{5})/;
                $lwp->show_progress(1);
                $lwp->mirror( "$download", "$title.$format" );
            }
        }
    }
    $lwp->show_progress(0);
    if ($Picks) {
        &ForeachPick;
    }
    if ( $playlist or $video_results and not $dont_exit ) {
        &PrintResults;
    }
    unless ( $dont_exit or $parallel_downloads ) {
        exit;
    }
}

            
Comments
Sorry but there are no comments to display