Trizen

Youtube windows $ENV{'PATH'} fix

Aug 14th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 38.56 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Copyright (C) 2010-2011 Trizen <echo dHJpemVueEBnbWFpbC5jb20K | base64 -d>.
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. #-------------------------------------------------------
  19. #  (C) 2010-2011 by Trizen
  20. #  Created on: 02 Jun 2010
  21. #  Latest edit on: 14 August 2011
  22. #  Website: http://trizen.go.ro
  23. #  Email: echo dHJpemVueEBnbWFpbC5jb20K | base64 -d
  24. #-------------------------------------------------------
  25. #
  26. # [?] What is this script for?
  27. #  - This script is useful to search and watch YouTube videos with MPlayer...
  28. #  - Have fun!
  29. #
  30. # [!] The most important changes are written in the changelog!
  31. #
  32. # [CHANGELOG]
  33. # - Added support for previous page and support to list youtube usernames from a file          - NEW (v2.5.2)
  34. # - Added few options to control cache of MPlayer and lower cache for lower video resolutions  - NEW (v2.5.1)
  35. # - Added colours for text (--colours) or $colours = 1, 360p support (-3), playlist support    - NEW (v2.5.0)
  36. # - Added support for today and all time Youtube tops (usage: -t, --tops, -a, --all-time)      - (v2.4.*)
  37. # - Re-added the support for the next page / Added support for download (-d, --download)       - (v2.4.*)
  38. # - Added support for Youtube CCaptions. (Depends on: 'gcap' - http://gcap.googlecode.com)     - (v2.4.*)
  39. # - First version with Windows support. Require SMPlayer to play videos. See MPlayer Line      - (v2.4.*)
  40. # - Code has been changed in a proportion of ~60% and optimized for speed // --480 became -4   - (v2.4.*)
  41. # - Added mega-powers of omnibox to the STDIN :)                                               - (v2.3.*)
  42. # - Re-added the option to list and play youtube videos from a user profile. Usage: -u [user]  - (v2.3.*)
  43. # - Added a new option to play only the audio track of a videoclip. Usage: [words] -n          - (v2.3.*)
  44. # - Added option for fullscreen (-f, --fullscreen). Usage: youtube-viewer [words] -f           - (v2.3.*)
  45. # - Added one new option '-c'. It shows available categories and will let you to choose one.   - (v2.3.*)
  46. # - Added one new option '-m'. It shows 3 pages of youtube video results. Usage: [words] -m    - (v2.3.*)
  47. # - For "-A" option has been added 3 pages of youtube video results (50 clips)                 - (v2.3.*)
  48. # - Added "-prefer-ipv4" to the mplayer line (videoclips starts in no time now).               - (v2.3.*)
  49. # - Search and play videos at 480p, 720p. Ex: [words] --480, [words] -A --480                  - (v2.3.*)
  50. # - Added support to play a video at 480p even if it's resolution is higher. Ex: [url] --480   - (v2.2.*)
  51. # - Added a nice feature which prints some informations about the current playing video        - (v2.2.*)
  52. # - Added support to play videos by your order. Example: after search results, insert: 3 5 2 1 - (v2.1.*)
  53. # - Added support for next pages of video results (press <ENTER> after search results)         - (v2.1.*)
  54. # - Added support to continue playing searched videos, usage: "youtube-viewer [words] -A"      - (v2.1.*)
  55. # - Added support to print counted videos and support to insert a number instead of video code - (v2.1.*)
  56. # - Added support to search YouTube Videos in script (ex: youtube-viewer avatar trailer 2009)  - (v2.0.*)
  57. # - Added support for script to choose automat quality if it is lower than 1080p               - (v2.0.*)
  58. # - Added support to choose the quality only between 720p and 1080p (if it is available)       - (v2.0.*)
  59. # - Added support for YouTube video codes (ex: youtube-viewer WVTWCPoUt8w)                     - (v1.0.*)
  60. # - Added support for 720p and 1080p YouTube Videos...                                         - (v1.0.*)
  61.  
  62. # Special thanks to:
  63. # - Army (for bugs reports and for his great ideas)
  64. # - dhn (for adding youtube-viewer in freshports.org)
  65.  
  66. use Cwd qw(getcwd);
  67. use URI::Escape qw(uri_escape);
  68. use HTML::Entities qw(decode_entities);
  69.  
  70. my $appname              = 'youtube-viewer';
  71. my $version              = '2.5.2';
  72. my $colours              = 0;
  73. my $cache                = 30000;
  74. my $cache_min            = 5;
  75. my $results              = 20;
  76. my $user_lower_cache     = 0;
  77. my $picks_mode           = 0;
  78. my $start_index          = 1;
  79. my $order_by             = 'relevance';
  80. my $time_p               = 'all_time';
  81. my $users_file           = $ENV{'HOME'} . '/.config/youtube_usernames.txt';
  82. my $mplayer_settings     = "-prefer-ipv4 -cache $cache -cache-min $cache_min";
  83. my $mplayer_srt_settings = '-unicode -utf8';
  84. my $bred                 = '';
  85. my $bgreen               = '';
  86. my $reset                = '';
  87. my $bblue                = '';
  88. my $cblack               = '';
  89. my $bpurle               = '';
  90. my $byellow              = '';
  91. my $cwd                  = getcwd();
  92.  
  93. my $tmp_dir = $ENV{'TMPDIR'} || $ENV{'TEMP'} || $ENV{'TMP'} || '/tmp';
  94.  
  95. if ( $^O =~ /(?:win|dos)/i ) {
  96.     my $MPlayer_exe = $ENV{'ProgramFiles'} . '\\SMPlayer\\mplayer\\mplayer.exe';
  97.     unless ( -e $MPlayer_exe ) {
  98.         warn "\n\n!!! Please install SMPlayer to stream Youtube videos.\n\n";
  99.     }
  100.     $MPlayerLine = qq["$MPlayer_exe" $mplayer_settings];
  101. }
  102. else {
  103.     $bred        = "\e[1;31m";
  104.     $bgreen      = "\e[1;32m";
  105.     $reset       = "\e[0m";
  106.     $MPlayerLine = "mplayer $mplayer_settings";
  107. }
  108.  
  109. my $gcap;
  110. if ( $ENV{'PATH'} ) {
  111.     foreach my $path ( @INC, win32_or_unix_paths() ) {
  112.         if ( -e "$path/gcap" ) {
  113.             $gcap = "$path/gcap";
  114.             last;
  115.         }
  116.     }
  117. }
  118. else {
  119.     $gcap = '/usr/bin/gcap';
  120. }
  121.  
  122. sub win32_or_unix_paths{
  123.     if($^O =~ /(?:win|dos)/i){
  124.         return split(/;/,$ENV{'PATH'});
  125.     }else{
  126.         return split(/:/,$ENV{'PATH'});
  127.     }
  128. }
  129.  
  130. sub UserAgent {
  131.     require LWP::UserAgent;
  132.     $lwp = 'LWP::UserAgent'->new;
  133.     $lwp->agent('Mozilla/5.0 (X11; U; Linux i686; en-US) Chrome/10.0.648.45');
  134.     $lwp->env_proxy;
  135.     $lwp->timeout(15);
  136.     $lwp->show_progress(1) if $debug;
  137.     $lwp_is_set = 1;
  138. }
  139. foreach my $arg (@ARGV) {
  140.     if ( $arg =~ /^-/o ) {
  141.         $stdin_argvs .= " $arg ";
  142.     }
  143.     else {
  144.         $SEARCH .= "$arg ";
  145.     }
  146.     next unless $arg =~ /^-/o;
  147.     if ( $arg =~ /^-+cache=([0-9]+)$/o ) {
  148.         my $cache = $1;
  149.         $MPlayerLine =~ s/-cache\s+[0-9]+/-cache $cache/;
  150.         $user_cache = 1;
  151.     }
  152.     elsif ( $arg =~ /^-+(?:l|lower.?cache)$/o ) {
  153.         $user_lower_cache = 1;
  154.     }
  155.     elsif ( $arg =~ /^-+cache.?min=([0-9]+)$/o ) {
  156.         my $cache_min = $1;
  157.         $MPlayerLine =~ s/-cache-min\s+[0-9]+/-cache-min $cache_min/;
  158.         $user_cache_min = 1;
  159.     }
  160.     elsif ( $arg =~ /^-+(?:n|no.?video)$/o ) {
  161.         $MPlayerLine =~ s/mplayer /mplayer -novideo /o;
  162.         $MPlayerLine =~ s/mplayer\.exe"/mplayer.exe" -novideo/o;
  163.         $stdin_argvs .= ' --480';
  164.     }
  165.     elsif ( $arg =~ /^-+time=(.+)/o ) {
  166.         $time_p = $1;
  167.     }
  168.     elsif ( $arg =~ /^-+(?:T|tty|M|mplayer)=(.+)/o ) {
  169.         $MPlayerLine .= " $1";
  170.     }
  171.     elsif ( $arg =~ /^-+(?:o|order.?by)=(.+)/o ) {
  172.         $order_by = $1;
  173.     }
  174.     elsif ( $arg =~ /^-+(?:sub|lang)=([\w]+)$/o ) {
  175.         $default_sub = lc $1;
  176.     }
  177.     elsif ( $arg =~ /^-+(?:colou?rs?|C|cl)$/o ) {
  178.         $colours = 1;
  179.     }
  180.     elsif ( $arg =~ /^-+(?:d|download)$/o ) {
  181.         $download_video = 1;
  182.     }
  183.     elsif ( $arg =~ /^-+(?:a|all.?time)$/o ) {
  184.         $all_time = 1;
  185.     }
  186.     elsif ( $arg eq '--debug' ) {
  187.         $debug = 1;
  188.     }
  189.     elsif ( $arg =~ /^-+(?:A|all)$/o ) {
  190.         $playback = 1;
  191.         $stdin_argvs .= ' --1080';
  192.     }
  193.     elsif ( $arg =~ /^-+(?:m|more)$/o ) {
  194.         $results = 50;
  195.     }
  196.     elsif ( $arg =~ /^-+(?:fs?|fullscreen)$/o ) {
  197.         $MPlayerLine =~ s/mplayer /mplayer -fs /o;
  198.         $MPlayerLine =~ s/mplayer.exe"/mplayer.exe" -fs/o;
  199.     }
  200. }
  201. if ( $colours and not $^O =~ /(?:win|dos)/io ) {
  202.     $cblack  = "\e[40m";
  203.     $byellow = "\e[1;33m";
  204.     $bpurle  = "\e[1;35m";
  205.     $bblue   = "\e[1;34m";
  206. }
  207. $stdin_argvs ||= '';
  208. $default_sub ||= 'en';
  209. $SEARCH      ||= '';
  210. my $i = 0;
  211. foreach my $arg (@ARGV) {
  212.     $i += 1;
  213.     next unless $arg =~ /^-/o;
  214.     if ( $arg =~ /^-+(?:h|help|\?|usage)$/io ) {
  215.         &help;
  216.     }
  217.     if ( $arg =~ /^-+(?:v|version)$/io ) {
  218.         &version;
  219.     }
  220.     if ( $arg =~ /^-+(?:pl?|playlists?)$/o ) {
  221.         &search_playlists;
  222.     }
  223.     if ( $arg =~ /^-+(?:c|categories)$/o ) {
  224.         &categories_area;
  225.     }
  226.     if ( $arg =~ /^-+(?:a|all[_-]?time|t|tops)$/o ) {
  227.         &youtube_tops;
  228.     }
  229.     if ( $arg =~ /^-+(?:u|username)$/o ) {
  230.         if ( $ARGV[$i] ) {
  231.             unless ( $ARGV[$i] =~ /^-/o ) {
  232.                 $user = $ARGV[$i];
  233.             }
  234.         }
  235.         unless ($user) {
  236.             $i -= 2;
  237.             if ( $ARGV[$i] ) {
  238.                 unless ( $ARGV[$i] =~ /^-/o ) {
  239.                     $user = $ARGV[$i];
  240.                 }
  241.             }
  242.         }
  243.         if ($user) {
  244.             &videos_from_username($user);
  245.         }
  246.     }
  247.     if ( $arg =~ /^-+(?:U|user(?:s|names))=?(.*)/o ) {
  248.         if ( defined $1 and -r $1 ) {
  249.             $users_file = $1;
  250.         }
  251.         &list_user_names;
  252.     }
  253. }
  254. foreach $_ (@ARGV) {
  255.     if ( not $_ =~ /^-/o or length $_ == 11 ) {
  256.         $non_argv = 1;
  257.         last;
  258.     }
  259. }
  260. print "\n";
  261. &insert_url unless $non_argv;
  262. foreach my $code (@ARGV) {
  263.     if ( $code =~ /^([\w-]{11})$/o ) {
  264.         $code = $1;
  265.         if ( $code =~ /[0-9]+|[A-Z]+|-/o ) {
  266.             $dont_exit = 1;
  267.             &get_youtube($code);
  268.         }
  269.     }
  270.     elsif ( $code =~ /view_play_list\?p=([A-Z0-9]+)/o ) {
  271.         $playlist = 1;
  272.         &list_playlist($1);
  273.     }
  274.     elsif ( $code =~ m[(?:v|embed)[=/]+([\w-]{11})]o ) {
  275.         $code      = $1;
  276.         $dont_exit = 1;
  277.         if ( length $code == 11 ) {
  278.             &get_youtube($code);
  279.         }
  280.         else {
  281.             next;
  282.         }
  283.     }
  284.     elsif ( $code =~ m[^http://]o and not $code =~ /youtube\.com/o ) {
  285.         &code_from_content($code);
  286.     }
  287.     else {
  288.         next;
  289.     }
  290. }
  291.  
  292. sub help {
  293.     my $appname = uc $appname;
  294.     print "\n\t" . '=' x 25 . " $appname " . '=' x 25 . qq[
  295. \t\t\t\t\t\t  by Trizen (trizen.go.ro)
  296. \n Usage: youtube-viewer [<url> | <video_id>] [-(4|7|1)]
  297.                        [-u <username>] [-categories] [-(a|t)]
  298.                        [<keywords>] [-fs] [-novideo] [-(m|p)]
  299. \nBase Options: youtube-viewer [...]
  300.    <url>                : play an YouTube video by URL
  301.    <code>               : play an YouTube video by code
  302.    <keywords>           : search and list YouTube videos
  303.    <playlist_url>       : list a playlist of Youtube videos
  304. \nYoutube options:
  305.    -t, --tops           : shows today Youtube tops
  306.    -a, --all-time       : shows all time Youtube tops
  307.    -c, --categories     : shows available YouTube categories
  308.    -m, --more           : shows more video results (50 clips)
  309.    -p, --playlist       : searches for Youtube playlists
  310.    -u <username>        : lists videos uploaded by a specific user
  311.    -order-by=<ORDER>    : order entries by: published, viewCount or rating
  312.    -time=<TIME>         : valid values are: today, this_week and this_month
  313.    -2, -3, -4, -7, -1   : plays videos at 240p, 360p, 480p, 720p and 1080p
  314. \nMPlayer options:
  315.    -f, --fullscreen     : plays all videos in fullscreen mode
  316.    -n, --novideo        : plays only the audio track of video(s)
  317.    -l, --lower-cache    : set a lower cache for MPlayer (for slow connections)
  318.    -mplayer='SETTINGS'  : add some extra options to the MPlayer line
  319.    -sub=<LANG>          : subtitle language (default: en) (depends on gcap)
  320.    -cache=<VALUE>       : set cache for MPlayer (default: $cache)
  321.    -cache-min=<VALUE>   : set cache-min for MPlayer (default: $cache_min)
  322. \nOther options:
  323.    -d, --download       : downloads youtube video(s) with LWP
  324.    -A, --all            : plays all video results in order
  325.    -C, --colors         : use colors for text output
  326.    -U, --users=file.txt : list youtube usernames from file
  327.    -v, --version        : prints version and exits
  328.    -h, --help           : prints help and exits
  329. \nTips and tricks:
  330.   1. After search results, press <ENTER> for the next page
  331.   2. After search results, insert 'back' for the previous page
  332.   3. After search results, insert: <number> (--download, -d, -1, -4, -7)
  333.   4. After search results, insert more numbers to play videos in your order
  334.   5. Play all audio tracks of video results by adding "-A -n" after keywords.
  335.   6. Play all video results in fullscreen mode at 720p: "<keywords> -A -f -7"
  336.   7. Play all videos from a user at 480p: "<username> -u -4" and insert "all"
  337. \n];
  338.     exit;
  339. }
  340.  
  341. sub version {
  342.     print "Youtube Viewer $version\n";
  343.     exit;
  344. }
  345.  
  346. sub code_from_content {
  347.     &UserAgent unless $lwp_is_set;
  348.     $connect = $lwp->get( $_[0] ) if $_[0];
  349.     if ( $connect->content =~ m[youtube\.com/(?:v|embed)/([\w-]{11})]o ) {
  350.         &get_youtube($1);
  351.     }
  352. }
  353.  
  354. sub insert_url {
  355.     print "$bred=>>$reset$bgreen "
  356.       . "Insert an YouTube URL or search something...\n$reset> ";
  357.     chomp( $youtube = <STDIN> );
  358.     if ( $youtube =~ m[(?:v|embed)[=/]+([\w-]{11})]o ) {
  359.         &get_youtube($1);
  360.     }
  361.     elsif ( $youtube =~ /view_play_list\?p=([A-Z0-9]+)/o ) {
  362.         $playlist = 1;
  363.         &list_playlist($1);
  364.     }
  365.     elsif ( $youtube =~ m[^http://]o and not $youtube =~ /youtube\.com/o ) {
  366.         &code_from_content($youtube);
  367.     }
  368.     elsif ($youtube) {
  369.         $stdin_argvs .= ' ' . $youtube;
  370.         $SEARCH = $youtube;
  371.         $number = 0;
  372.         print "\n";
  373.         &search;
  374.     }
  375.     else {
  376.         warn "\n$bred(x_x) Unable to continue...$reset\n\n";
  377.         exit;
  378.     }
  379. }
  380.  
  381. sub list_user_names {
  382.     if ( -r $users_file ) {
  383.         sysopen FILE, $users_file, 0;
  384.         my $n = 0;
  385.         my %usernames_table;
  386.         while ( defined( $_ = <FILE> ) ) {
  387.             $n += 1;
  388.             print do {
  389.                 ' ' if $n < 10;
  390.             }, "$bred$n$reset - ", "$bgreen$_$reset";
  391.             $usernames_table{$n} = $_;
  392.         }
  393.         print "\n$bred=>>$reset ${bgreen}Pick an username number$reset\n> ";
  394.         chomp( my $pick = <STDIN> );
  395.         exit if $pick =~ /^(?:q|quit|exit)$/io;
  396.         if ( defined $usernames_table{$pick} ) {
  397.             chomp( my $user = $usernames_table{$pick} );
  398.             &videos_from_username($user);
  399.         }
  400.         else {
  401.             &list_user_names;
  402.         }
  403.     }
  404. }
  405.  
  406. sub videos_from_username {
  407.     $YT_API_URL = "http://gdata.youtube.com/feeds/api/users/$_[0]/uploads";
  408.     &YoutubeAPI;
  409. }
  410.  
  411. sub search_playlists {
  412.     $playlists = 1;
  413.     &UserAgent unless $lwp_is_set;
  414.     print "\n";
  415.     $new_index = $new_index ? $new_index : 1;
  416.     my $url = $_[0]
  417.       || "http://gdata.youtube.com/feeds/api/playlists/snippets?q=$SEARCH"
  418.       . "&start-index=1&max-results=$results&v=2";
  419.     my (@playlists) = split( m?<title>?, $lwp->get($url)->content, 0 );
  420.     my $n = -1;
  421.     my ( @list_of_playlists, $title, $summary, $playlist_id, $countHint,
  422.         $author );
  423.  
  424.     foreach $_ (@playlists) {
  425.         if (m[^([^<]*)</title>]o) {
  426.             $title = $1;
  427.             if ( $n == -1 ) {
  428.                 $n += 1;
  429.                 next;
  430.             }
  431.             $title = decode_entities($title);
  432.         }
  433.         if (m[<yt:playlistId>([^<]+)</yt:playlistId>]o) {
  434.             $n += 1;
  435.             $playlist_id = $1;
  436.         }
  437.         else {
  438.             next;
  439.         }
  440.         if (m[<summary>([^<]*)</summary>]o) {
  441.             $summary = $1;
  442.             $summary =~ s/\s+/ /go;
  443.             $summary = $1 if $summary =~ /^(.{10})/o;
  444.             $summary = decode_entities($summary);
  445.         }
  446.         else {
  447.             $summary = '';
  448.         }
  449.         if (m[<yt:countHint>([0-9]+)</yt:countHint>]o) {
  450.             $countHint = $1;
  451.         }
  452.         else {
  453.             $countHint = 0;
  454.         }
  455.         if (m[<name>([^<]+)</name>]o) {
  456.             $author = $1;
  457.         }
  458.         else {
  459.             $author = 'Unknown';
  460.         }
  461.         push @list_of_playlists,
  462.           [ $n, $title, $summary, $playlist_id, $countHint, $author ];
  463.     }
  464.     foreach $_ (@list_of_playlists) {
  465.         my $number    = $$_[0];
  466.         my $title     = $$_[1];
  467.         my $summary   = $$_[2];
  468.         my $countHint = $$_[4];
  469.         my $author    = $$_[5];
  470.         print do {
  471.             ' ' if $number < 10;
  472.           }, $cblack
  473.           . $bred
  474.           . $number
  475.           . $reset
  476.           . $cblack . ' - '
  477.           . $byellow
  478.           . $title . ' '
  479.           . $summary
  480.           . $reset
  481.           . $cblack . ' ('
  482.           . $bpurle . 'by '
  483.           . $author
  484.           . $reset
  485.           . $cblack . ') ('
  486.           . $bblue
  487.           . $countHint
  488.           . $reset
  489.           . $cblack . ')'
  490.           . $reset . "\n";
  491.     }
  492.     print "\n$bred=>>$reset ${bgreen}Pick one playlist$reset\n> ";
  493.     chomp( $pick = <STDIN> );
  494.     if ( not $pick =~ /^\s*[0-9]+\s*$/o and $pick =~ /^\s*$/o
  495.         or $pick eq 'next' )
  496.     {
  497.         if ( $url =~ /start-index=([0-9]+)/o ) {
  498.             $new_index = $1 + $results;
  499.             $url =~ s/start-index=[0-9]+/start-index=$new_index/;
  500.             undef @list_of_playlists;
  501.             &search_playlists($url);
  502.         }
  503.     }
  504.     elsif ( $pick eq 'back' and $new_index > 1 ) {
  505.         if ( $url =~ /start-index=([0-9]+)/o ) {
  506.             $new_index = $1 - $results;
  507.             $url =~ s/start-index=[0-9]+/start-index=$new_index/;
  508.             undef @list_of_playlists;
  509.             &search_playlists($url);
  510.         }
  511.     }
  512.     elsif ( $pick =~ /^[^0-9]+/o and not $pick =~ /^\s+[0-9]+/o ) {
  513.         $SEARCH = $pick;
  514.         undef @list_of_playlists;
  515.         &search_playlists;
  516.     }
  517.     elsif ( $pick =~ /^\s*([0-9]+)/o ) {
  518.         $pick = $1;
  519.     }
  520.     &list_playlist( $list_of_playlists[ $pick - 1 ][3] );
  521. }
  522.  
  523. sub list_playlist {
  524.     if ( $_[0] and $_[0] =~ /^([A-Z0-9]+)$/o ) {
  525.         $YT_API_URL = "http://gdata.youtube.com/feeds/api/playlists/$1";
  526.         &YoutubeAPI;
  527.     }
  528. }
  529.  
  530. sub youtube_tops {
  531.     $n = 0;
  532.     my $today = $all_time ? 0 : 1;
  533.     $youtube_top_url = 'http://gdata.youtube.com/feeds/api/standardfeeds';
  534.     foreach my $line (
  535.         'top_rated',      'top_favorites',
  536.         'most_viewed',    'most_popular',
  537.         'most_recent',    'most_discussed',
  538.         'most_responded', 'recently_featured'
  539.       )
  540.     {
  541.         $n += 1;
  542.         my $top_name = ucfirst $line;
  543.         $top_name =~ s/_/ /;
  544.         print "$bred$n$reset - $top_name\n";
  545.         push @tops, "$n - $youtube_top_url/$line";
  546.     }
  547.     print "\n$bred=>>$bgreen Pick one of$reset\n> ";
  548.     chomp( my $pick = quotemeta <STDIN> );
  549.     foreach $_ (@tops) {
  550.         if ( $_ =~ /^$pick - (.+)/ ) {
  551.             $YT_API_URL = $1;
  552.             if ($today) {
  553.                 unless ( $YT_API_URL =~ /recent/o ) {
  554.                     $YT_API_URL .= '?time=today';
  555.                 }
  556.             }
  557.             &YoutubeAPI;
  558.         }
  559.     }
  560. }
  561.  
  562. sub categories_area {
  563.     &UserAgent unless $lwp_is_set;
  564.     $connect =
  565.       $lwp->get('http://gdata.youtube.com/schemas/2007/categories.cat');
  566.     @cates = split( /category term=/o, $connect->content, 0 );
  567.     $n = 0;
  568.     foreach my $cat (@cates) {
  569.         if ( $cat =~ /^'([^']+)' label='([^']+)'/o
  570.             and not $cat =~ /deprecated/o )
  571.         {
  572.             $n += 1;
  573.             my $cat_name  = $1;
  574.             my $cat_label = $2;
  575.             $cat_label =~ s/&amp;/&/;
  576.             if ( $n < 10 ) {
  577.                 print ' ';
  578.             }
  579.             print "$bred$n$reset - $cat_label\n";
  580.             push @categories, "$n - $cat_name";
  581.         }
  582.     }
  583.     print "\n$bgreen=>> Insert a category number\n>$reset ";
  584.     chomp( $pickcat = quotemeta <STDIN> );
  585.     foreach my $cat (@categories) {
  586.         if ( $cat =~ /^$pickcat - (.+)/ ) {
  587.             $YT_API_URL = 'http://gdata.youtube.com/feeds/api/standardfeeds/'
  588.               . "recently_featured_$1";
  589.             &YoutubeAPI;
  590.         }
  591.     }
  592. }
  593.  
  594. sub MPlayer {
  595.     print "** STREAMING: $streaming\n\n" if $debug;
  596.     if ( $download_video or $tmp_download_video ) {
  597.         $title =~ s[/][|]g;
  598.         if ( not -e "$title.mp4" ) {
  599.             if ( -e '/usr/bin/wget' ) {
  600.                 my $title = quotemeta $title;
  601.                 system "wget -nc '${streaming}' -O $title.mp4";
  602.             }
  603.             else {
  604.                 print "** Saving to: '$title.mp4'\n";
  605.                 $lwp->show_progress(1);
  606.                 $lwp->mirror( $streaming, "$title.mp4" );
  607.                 $lwp->show_progress(0);
  608.             }
  609.         }
  610.         else {
  611.             warn "** '$title.mp4' already exists...\n";
  612.         }
  613.         $tmp_download_video = 0;
  614.     }
  615.     else {
  616.         print "** MPlayer Line: $MPlayerLine\n\n" if $debug;
  617.         `$MPlayerLine "$streaming"`;
  618.         chdir $cwd;
  619.     }
  620.     print "\n";
  621.     if ($picks_mode) {
  622.         &foreach_pick;
  623.     }
  624.     if ( $playlist or $video_results and not $dont_exit ) {
  625.         &print_results;
  626.     }
  627.     exit unless $dont_exit;
  628. }
  629.  
  630. sub YoutubeAPI {
  631.     undef @Videos;
  632.     if ( not $YT_API_URL =~ /\?/o ) {
  633.         $YT_API_URL .= '?start-index=1';
  634.     }
  635.     else {
  636.         $YT_API_URL .= '&start-index=1';
  637.     }
  638.     $YT_API_URL .= "&max-results=$results";
  639.     &parse_content($YT_API_URL);
  640.     print "\n";
  641.     &print_results;
  642. }
  643. &search unless $dont_exit or $playlists;
  644.  
  645. sub search {
  646.     $SEARCH =~ s/^\s+|\s+$//go;
  647.     $SEARCH = uri_escape($SEARCH);
  648.     $ys     = "http://gdata.youtube.com/feeds/api/videos?q=$SEARCH"
  649.       . "&max-results=$results&time=$time_p&orderby=$order_by&start-index=1";
  650.     &parse_content($ys);
  651.     &print_results;
  652. }
  653.  
  654. sub parse_content {
  655.     &UserAgent unless $lwp_is_set;
  656.     undef @Videos;
  657.     my (@content) = split( /<entry>/o, $lwp->get( $_[0] )->content, 0 );
  658.     my ( $author, $code, $title, $category, $published, $duration, $views,
  659.         $description );
  660.     foreach $_ (@content) {
  661.         print "$_\n\n" if $debug;
  662.         next unless $_ =~ /^<id>/o;
  663.         if (m[v=([\w-]+)&amp;feature=youtube_gdata'/>]o) {
  664.            $code = $1;
  665.        }
  666.        else {
  667.            next;
  668.        }
  669.        if (m[<name>([^<]+)</name>]o) {
  670.            $author = $1;
  671.        }
  672.        else {
  673.            $author = 'Unknown';
  674.        }
  675.        if (m[>([^<]+)</title>]o) {
  676.            $title = $1;
  677.        }
  678.        else {
  679.            $title = 'Unknown';
  680.        }
  681.        if (/<media:category label=['"]([^']+)['"]/o) {
  682.             $category = $1;
  683.             $category =~ s/&amp;/&/o;
  684.         }
  685.         else {
  686.             $category = 'Unknown';
  687.         }
  688.         if (/<published>([0-9\-]+)/o) {
  689.             $published = $1;
  690.         }
  691.         else {
  692.             $published = 'Unknown';
  693.         }
  694.         if (/duration[^']+'([0-9]+)'/o) {
  695.             $duration = $1;
  696.         }
  697.         else {
  698.             $duration = 0;
  699.         }
  700.         if (/viewCount='([0-9]+)'/o) {
  701.             $views = $1;
  702.         }
  703.         else {
  704.             $views = 0;
  705.         }
  706.         if (/<media:description type='plain'>([^<]+)/o) {
  707.             $description = $1;
  708.         }
  709.         else {
  710.             $description = 'No description available...';
  711.         }
  712.         push @Videos,
  713.           [
  714.             $author,    $code,     $title, $category,
  715.             $published, $duration, $views, $description
  716.           ];
  717.     }
  718. }
  719.  
  720. sub print_results {
  721.     exit unless scalar @Videos;
  722.     my $number = 0;
  723.     undef @codes;
  724.     foreach $_ (@Videos) {
  725.         $number += 1;
  726.         my $author      = $$_[0];
  727.         my $code        = $$_[1];
  728.         my $title       = decode_entities( $$_[2] );
  729.         my $category    = $$_[3];
  730.         my $published   = $$_[4];
  731.         my $time        = format_time( $$_[5] );
  732.         my $views       = $$_[6];
  733.         my $description = $$_[7];
  734.         push @codes,
  735.           [
  736.             $number, $title, $published, $category,
  737.             $time,   $code,  $views,     $description
  738.           ];
  739.         print "$bred";
  740.  
  741.         if ( $number < 10 ) {
  742.             unless ($video_results) {
  743.                 $video_results = 1;
  744.             }
  745.             print "$cblack ";
  746.         }
  747.         print $cblack
  748.           . $bred
  749.           . $number
  750.           . $reset
  751.           . $cblack . ' - '
  752.           . $byellow
  753.           . $title
  754.           . $reset
  755.           . $cblack . ' ('
  756.           . $bpurle . 'by '
  757.           . $author
  758.           . $reset
  759.           . $cblack . ') ('
  760.           . $bblue
  761.           . $time
  762.           . $reset
  763.           . $cblack . ')'
  764.           . $reset . "\n";
  765.     }
  766.     if ($playback) {
  767.         for ( my $i = 1 ; $i <= $number ; $i += 1 ) {
  768.             push @picks, $i;
  769.         }
  770.         $playback_mode = 1;
  771.         &picks_area;
  772.     }
  773.     print "\n$bred=>>$reset$bgreen Insert a number or "
  774.       . "search something else\n$reset> ";
  775.     chomp( my $youtube = <STDIN> );
  776.     $PickBackup = $youtube;
  777.     print "\n";
  778.     if ( $youtube =~ /^(?:q|quit|exit)$/o ) {
  779.         exit;
  780.     }
  781.     elsif ( $PickBackup =~ m[^http://]o and not $PickBackup =~ /youtube\.com/o )
  782.     {
  783.         &code_from_content($PickBackup);
  784.     }
  785.     elsif ( $PickBackup =~ /([0-9]+) -([\w\s-]+)/o ) {
  786.         $youtube     = $1;
  787.         $stdin_argvs = " -$2";
  788.         @options     = split( m? ?, "-$2", 0 );
  789.         foreach $_ (@options) {
  790.             if ( $_ =~ /^-+(?:d|download)$/o ) {
  791.                 $tmp_download_video = 1;
  792.                 last;
  793.             }
  794.         }
  795.     }
  796.     if ( $youtube =~ /^\s*([0-9]+)\s*$/o ) {
  797.         if ( $youtube > $number or not $youtube ) {
  798.             print "\n";
  799.             &print_results;
  800.         }
  801.         else {
  802.             &foreach_code($1);
  803.         }
  804.     }
  805.     elsif ( $youtube =~ /^\s*$/o or $youtube eq 'next' ) {
  806.         &next_page;
  807.     }
  808.     elsif ( $youtube eq 'back' and $start_index > 1 ) {
  809.         &previous_page;
  810.     }
  811.     elsif ( $youtube eq 'all' ) {
  812.         $stdin_argvs .= ' --1080 --play-all';
  813.         undef @picks;
  814.         for ( my $i = 1 ; $i <= $number ; $i += 1 ) {
  815.             push @picks, $i;
  816.         }
  817.         &picks_area;
  818.     }
  819.     elsif ( $youtube =~ /view_play_list\?p=([A-Z0-9]+)/o ) {
  820.         &list_playlist($1);
  821.     }
  822.     elsif ( $youtube =~ m[(?:v|embed)[=/]+([\w-]{11})]o ) {
  823.         &get_youtube($1);
  824.     }
  825.     elsif ( $youtube =~ /[0-9]+[,\s]+[0-9]+/o ) {
  826.         @picks = split( m?[^0-9]?o, $youtube, 0 );
  827.         &picks_area;
  828.     }
  829.     else {
  830.         $SEARCH = $youtube;
  831.         &search;
  832.     }
  833. }
  834.  
  835. sub next_page {
  836.     if ($ys) {
  837.         if ( $ys =~ /start-index=([0-9]+)/o ) {
  838.             $start_index = $1 + $results;
  839.             $ys =~ s/index=$1/index=$start_index/;
  840.             &parse_content($ys);
  841.             &print_results;
  842.         }
  843.     }
  844.     elsif ($YT_API_URL) {
  845.         undef @Videos;
  846.         $ys = '';
  847.         if ( $YT_API_URL =~ /start-index=([0-9]+)/o ) {
  848.             $start_index = $1 + $results;
  849.             $YT_API_URL =~ s/index=$1/index=$start_index/;
  850.             &parse_content($YT_API_URL);
  851.             &print_results;
  852.         }
  853.     }
  854. }
  855.  
  856. sub previous_page {
  857.     if ($ys) {
  858.         if ( $ys =~ /start-index=([0-9]+)/o ) {
  859.             $start_index = $1 - $results;
  860.             $ys =~ s/index=$1/index=$start_index/;
  861.             &parse_content($ys);
  862.             &print_results;
  863.         }
  864.     }
  865.     elsif ($YT_API_URL) {
  866.         undef @Videos;
  867.         $ys = '';
  868.         if ( $YT_API_URL =~ /start-index=([0-9]+)/o ) {
  869.             $start_index = $1 - $results;
  870.             $YT_API_URL =~ s/index=$1/index=$start_index/;
  871.             &parse_content($YT_API_URL);
  872.             &print_results;
  873.         }
  874.     }
  875. }
  876.  
  877. sub picks_area {
  878.     $NrOfPicks = scalar @picks;
  879.     $no        = 0;
  880.     $lastpick  = '';
  881.     &foreach_pick;
  882. }
  883.  
  884. sub foreach_pick {
  885.     for ( $number = $no ; $number <= $NrOfPicks ; ++$number ) {
  886.         $no   = $number;
  887.         $pick = $picks[$number];
  888.         if ($pick) {
  889.             next if $lastpick eq $pick;
  890.         }
  891.         if ( not $playback_mode ) {
  892.             &print_results if $number eq $NrOfPicks;
  893.         }
  894.         else {
  895.             &next_page if $number eq $NrOfPicks;
  896.         }
  897.         $picks_mode = 1;
  898.         $lastpick   = $pick;
  899.         &foreach_code($pick);
  900.     }
  901. }
  902.  
  903. sub foreach_code {
  904.     foreach $_ ( $codes[ $_[0] - 1 ] ) {
  905.         my $title       = $$_[1];
  906.         my $published   = $$_[2];
  907.         my $category    = $$_[3];
  908.         my $time        = $$_[4];
  909.         my $code        = $$_[5];
  910.         my $views       = $$_[6];
  911.         my $description = decode_entities( $$_[7] );
  912.         &get_youtube( $code, $title, $time, $views, $category, $published,
  913.             $description );
  914.     }
  915. }
  916.  
  917. sub select_resolution {
  918.     $streaming =~ s/^[\w]*%[\w]*http:/http:/o;
  919.     if ( not $streaming =~ /itag=34/o and $streaming =~ /itag=([0-9]+)/o ) {
  920.         my $itag = $1;
  921.         $streaming =~ s/.*http(.+)itag=$itag([^%]*).+/http$1itag=$itag$2/;
  922.         $streaming =~ s/%.*//o;
  923.         unless ( $stdin_argvs =~ / -+f/o ) {
  924.             $MPlayerLine =~ s/-fs //o;
  925.         }
  926.         &lower_cache;
  927.         &description;
  928.         &MPlayer;
  929.     }
  930.     elsif ( $stdin_argvs =~ / -+2/o ) {
  931.         &lower_quality(5);
  932.         &description;
  933.         &MPlayer;
  934.     }
  935.     elsif ( $stdin_argvs =~ / -+3/o ) {
  936.         &lower_quality(34);
  937.         &description;
  938.         &MPlayer;
  939.     }
  940.     elsif ( $stdin_argvs =~ / -+4/o ) {
  941.         &lower_quality;
  942.         &description;
  943.         &MPlayer;
  944.     }
  945.     elsif ( $stdin_argvs =~ / -+7/o ) {
  946.         if ( $streaming =~ /itag=22/o ) {
  947.             $streaming =~ s/.*http(.+)itag=22([^%]*).+/http$1itag=22$2/;
  948.             &fullscreen_check;
  949.             &description;
  950.             &MPlayer;
  951.         }
  952.         else {
  953.             &lower_quality;
  954.             &description;
  955.             &MPlayer;
  956.         }
  957.     }
  958.     elsif ( $stdin_argvs =~ / -+1/o ) {
  959.         if ( $streaming =~ /itag=37/o ) {
  960.             $streaming =~ s/.*http(.+)itag=37([^%]*).+/http$1itag=37$2/;
  961.             &fullscreen_check;
  962.             &description;
  963.             &MPlayer;
  964.         }
  965.         elsif ( $streaming =~ /itag=22/o ) {
  966.             $streaming =~ s/.*http(.+)itag=22([^%]*).+/http$1itag=22$2/;
  967.             &fullscreen_check;
  968.             &description;
  969.             &MPlayer;
  970.         }
  971.         else {
  972.             &lower_quality;
  973.             &description;
  974.             &MPlayer;
  975.         }
  976.     }
  977.     else {
  978.         &description;
  979.         &check_resolution;
  980.     }
  981. }
  982.  
  983. sub format_time {
  984.     my $sec = shift @_ || return (0);
  985.     my $time = $sec / 3600 % 24 . ':' . $sec / 60 % 60 . ':' . $sec % 60;
  986.     $time =~ s/^0://o;
  987.     unless ( $time =~ /:/o ) {
  988.         $time = "0:$time";
  989.     }
  990.     if ( $time =~ /(.):(.):(.+)/o ) {
  991.         $time = "$1:0$2:$3";
  992.     }
  993.     if ( $time =~ /^(.+):(.)$/o ) {
  994.         $time = "$1:0$2";
  995.     }
  996.     return $time;
  997. }
  998.  
  999. sub description {
  1000.     my ( $date, $rating );
  1001.     if ( $connect->content =~ /rating average='([0-9.]+)'/o ) {
  1002.         $rating = $1;
  1003.         $rating =~ s/^([0-9.]{4}).*/$1/;
  1004.     }
  1005.     elsif ( $connect->content =~ /avg_rating=([^&]+)/o ) {
  1006.         $rating = $1;
  1007.         $rating =~ s/^([0-9.]{4}).*/$1/;
  1008.     }
  1009.     unless ($title) {
  1010.         my $feed_url = 'http://gdata.youtube.com/feeds/api/videos/' . $code;
  1011.         my $content  = $lwp->get($feed_url)->content;
  1012.         if ( $content =~ m[media:title type='plain'>([^<]+)</media:title>]o ) {
  1013.             $title = decode_entities($1);
  1014.         }
  1015.         if ( $content =~ /viewCount='([0-9]+)'/o ) {
  1016.             $views = $1;
  1017.         }
  1018.         if ( $content =~ /<published>([0-9\-]+)/o ) {
  1019.             $date = $1;
  1020.             $date =~ s/-/./go;
  1021.             $date =~ s/(.+)\.(.+)\.(.+)/$3.$2.$1/;
  1022.         }
  1023.         if ( $content =~ m[<media:description type='plain'>([^<]+)</media]o ) {
  1024.             $description = decode_entities($1);
  1025.         }
  1026.         if ( $content =~ /duration[^0-9]+([0-9]+)/o ) {
  1027.             $duration = format_time($1);
  1028.         }
  1029.         if ( $content =~ /category label='([^']+)'/o ) {
  1030.             $category = $1;
  1031.             $category =~ s/&amp;/&/o;
  1032.         }
  1033.     }
  1034.     if ( not $date and $published ) {
  1035.         $date = $published;
  1036.         $date =~ s/-/./go;
  1037.         $date =~ s/(.+)\.(.+)\.(.+)/$3.$2.$1/;
  1038.     }
  1039.     if ($views) {
  1040.         $views = reverse($views);
  1041.         $views =~ s/([0-9]{3})/$1./g;
  1042.         $views = reverse($views);
  1043.         $views =~ s/^\.//o;
  1044.     }
  1045.     if ($description) {
  1046.         print "$bred=>> " . $bgreen . $title . $reset . "\n" . '-' x 80;
  1047.         print "\n" . $description . "\n" . '-' x 80 . "\n";
  1048.     }
  1049.     else {
  1050.         print "\n";
  1051.     }
  1052.     print "$bred=>> ${bgreen}View & Download$reset\n" . '-' x 80;
  1053.     my $get = $1 if $streaming =~ m[((http://)[^%]+)]o;
  1054.     print "\n* URL ";
  1055.     print STDOUT $url;
  1056.     print "\n* GET $get\n" . '-' x 80 . "\n";
  1057.     my $count = length $title;
  1058.     if ( $count <= 40 ) {
  1059.         print "$bred\t\t=>> ";
  1060.     }
  1061.     elsif ( $count >= 40 and $count <= 55 ) {
  1062.         print "$bred\t=>> ";
  1063.     }
  1064.     elsif ( $count >= 56 ) {
  1065.         print "$bred  =>> ";
  1066.     }
  1067.     print "$bgreen$title$reset$bred <<=\n$reset";
  1068.     my $author = $1 if $connect->content =~ /&author=([^&]+)/o;
  1069.     print "\n** Author    : $author\n" if $author;
  1070.     print "** Category  : $category\n" if $category;
  1071.     print "** Duration  : $duration\n" if $duration;
  1072.     print "** Rating    : $rating\n"   if $rating;
  1073.     print "** Views     : $views\n"    if $views;
  1074.     print "** Published : $date\n"     if $date;
  1075.     print '-' x 80 . "\n\n";
  1076. }
  1077.  
  1078. sub get_youtube {
  1079.     ( $code, $title, $duration, $views, $category, $published, $description ) =
  1080.       @_;
  1081.     my $youtube = "http://www.youtube.com/get_video_info?&video_id=$code"
  1082.       . '&el=detailpage&ps=default&eurl=&gl=US&hl=en';
  1083.     &UserAgent unless $lwp_is_set;
  1084.     $connect = $lwp->get($youtube);
  1085.     $url     = "http://www.youtube.com/watch?v=$code";
  1086.     $MPlayerLine =~ s/ $mplayer_srt_settings -sub.*//;
  1087.     if (    -e $gcap
  1088.         and not $MPlayerLine  =~ / -novideo/o
  1089.         and $connect->content =~ /&has_cc=True&/o
  1090.         and not $download_video
  1091.         and not $tmp_download_video )
  1092.     {
  1093.         chdir $tmp_dir;
  1094.         unless ( -e "${code}_$default_sub.srt" ) {
  1095.             system "gcap $url";
  1096.             print "\n";
  1097.         }
  1098.         my @srt_files;
  1099.         opendir( DIR, $tmp_dir );
  1100.         /\.srt\z/o and push( @srt_files, $_ ) while defined( $_ = readdir DIR );
  1101.         close DIR;
  1102.         my $srt_file;
  1103.         foreach my $line (@srt_files) {
  1104.             if ( $line =~ /${code}_$default_sub\.srt/ ) {
  1105.                 $srt_file = "${code}_$default_sub.srt";
  1106.                 $MPlayerLine .= " $mplayer_srt_settings -sub $srt_file";
  1107.             }
  1108.         }
  1109.         unless ($srt_file) {
  1110.             foreach $line (@srt_files) {
  1111.                 if ( $line =~ /$code([\w]*)\.srt/ ) {
  1112.                     $srt_file = "$code$1.srt";
  1113.                     $MPlayerLine .= " $mplayer_srt_settings -sub $srt_file";
  1114.                 }
  1115.             }
  1116.         }
  1117.     }
  1118.     if ( $connect->content =~ /url_encoded_fmt_stream_map=([^&]+)&/o ) {
  1119.         $streaming = $1;
  1120.         $streaming =~ s/%253A/:/gio;
  1121.         $streaming =~ s[%252F][/]gio;
  1122.         $streaming =~ s/%2526/&/go;
  1123.         $streaming =~ s/%253D/=/gio;
  1124.         $streaming =~ s/%253F/?/gio;
  1125.         $streaming =~ s/%25252C/,/gio;
  1126.         &select_resolution;
  1127.     }
  1128.     else {
  1129.         warn "\n$bred(x_x) Something went wrong...$reset\n\n";
  1130.         warn "$bred(x_x) Unable to stream: $reset$url\n\n";
  1131.         if ( $video_results and not $dont_exit ) {
  1132.             unless ( $stdin_argvs =~ / -+(?:all|A|play-all)/o or $NrOfPicks ) {
  1133.                 sleep 1;
  1134.                 &print_results;
  1135.             }
  1136.         }
  1137.         else {
  1138.             unless ( $video_results or $dont_exit ) {
  1139.                 exit;
  1140.             }
  1141.         }
  1142.     }
  1143. }
  1144.  
  1145. sub check_resolution {
  1146.     if ( $streaming =~ /itag=37/o and not $stdin_argvs =~ / -+(?:all|A)/o ) {
  1147.         &pick;
  1148.     }
  1149.     elsif ( $stdin_argvs =~ / -+(?:all|A)/o ) {
  1150.         if ( $streaming =~ /itag=37/o ) {
  1151.             $streaming =~ s/.*http(.+)itag=37([^%]*).+/http$1itag=37$2/;
  1152.             &default_cache;
  1153.             &fullscreen_check;
  1154.             &description;
  1155.             &MPlayer;
  1156.         }
  1157.     }
  1158.     elsif ( $streaming =~ /itag=22/o ) {
  1159.         $streaming =~ s/.*http(.+)itag=22([^%]*).+/http$1itag=22$2/;
  1160.         &default_cache;
  1161.         &fullscreen_check;
  1162.         &MPlayer;
  1163.     }
  1164.     else {
  1165.         &lower_quality;
  1166.         &MPlayer;
  1167.     }
  1168. }
  1169.  
  1170. sub fullscreen_check {
  1171.     $MPlayerLine =~ s/mplayer -p/mplayer -fs -p/o;
  1172.     $MPlayerLine =~ s/mplayer.exe" -p/mplayer.exe" -fs -p/o;
  1173. }
  1174.  
  1175. sub pick {
  1176.     print "$bred=>>$reset $bgreen Please choose "
  1177.       . 'the quality of video (default: 1)';
  1178.     print "\n$reset";
  1179.     print "\n  $bred 1$reset - 1280x720  (720p)\n"
  1180.       . "  $bred 2$reset - 1920x1080 (1080p)\n  ";
  1181.     print '=' x 23 . "\n";
  1182.     print "\n$bgreen=>> Pick format:$reset ";
  1183.     chomp( my $pick = <STDIN> );
  1184.     print '-' x 80 . "\n\n";
  1185.     if ( $pick eq 2 ) {
  1186.         $streaming =~ s/.*http(.+)itag=37([^%]*).+/http$1itag=37$2/;
  1187.         &default_cache;
  1188.         &fullscreen_check;
  1189.         &MPlayer;
  1190.     }
  1191.     else {
  1192.         $streaming =~ s/.*http(.+)itag=22([^%]*).+/http$1itag=22$2/;
  1193.         &default_cache;
  1194.         &fullscreen_check;
  1195.         &MPlayer;
  1196.     }
  1197. }
  1198.  
  1199. sub lower_quality {
  1200.     if ( $_[0] and $_[0] =~ /^[0-9]+$/o ) {
  1201.         if ( $streaming =~ /itag=$_[0]/ ) {
  1202.             $streaming =~ s/.*http(.+)itag=$_[0]([^%]*).*/http$1itag=$_[0]$2/;
  1203.             &lower_cache;
  1204.         }
  1205.     }
  1206.     else {
  1207.         foreach $_ ( 35, 34 ) {
  1208.             if ( $streaming =~ /itag=$_/ ) {
  1209.                 $streaming =~ s/.*http(.+)itag=$_([^%]*).*/http$1itag=$_$2/;
  1210.                 &lower_cache;
  1211.                 last;
  1212.             }
  1213.         }
  1214.     }
  1215.     unless ( $stdin_argvs =~ / -+f/o ) {
  1216.         $MPlayerLine =~ s/-fs //o;
  1217.     }
  1218. }
  1219.  
  1220. sub lower_cache {
  1221.     $MPlayerLine =~ s/-cache\s+[0-9]+/-cache 1000/o      unless $user_cache;
  1222.     $MPlayerLine =~ s/-cache-min\s+[0-9]+/-cache-min 3/o unless $user_cache_min;
  1223. }
  1224.  
  1225. sub default_cache {
  1226.     if ( not $user_lower_cache ) {
  1227.         $MPlayerLine =~ s/-cache\s+[0-9]+/-cache $cache/ unless $user_cache;
  1228.         $MPlayerLine =~ s/-cache-min\s+[0-9]+/-cache-min $cache_min/
  1229.           unless $user_cache_min;
  1230.     }
  1231.     else {
  1232.         &lower_cache;
  1233.     }
  1234. }
Add Comment
Please, Sign In to add comment