summaryrefslogtreecommitdiffstats
path: root/youtube-viewer/youtube-viewer
blob: 09c93fd23f079081c16d80792deb70608db543af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#!/usr/bin/perl

# Last edit on January 12 2011, 10:25 PM

$appname = 'youtube-viewer';
$version = 'v2.4.4';

=start
-------------------------------------------------------
  (C) 2010-2011 by Trizen
  WebSite: http://trizen.go.ro
  Email: echo dHJpemVueEBnbWFpbC5jb20K | base64 -d
-------------------------------------------------------

[?] What is this script for?          
 - This script is useful if do you hate the Flash Player and love YouTube.
 - Using this script you can search and play YouTube Videos with MPlayer...
 - Have fun!

[!] Most important changes are written in the changelog!

 [CHANGELOG]
 - First version with Windows support. Require SMPlayer to play videos. See MPlayer Line      - NEW (v2.4.*)
 - Code has been changed in a proportion of ~60% and optimized for speed // --480 became -4   - NEW (v2.4.*)
 - Added mega-powers of omnibox to the STDIN :)                                               - NEW (v2.3.*)
 - Re-added the option to list and play youtube videos from a user profile. Usage: -u [user]  - NEW (v2.3.*)
 - Added a new option to play all video clips from a category. Usage: '-c 9' and insert 'all' - NEW (v2.3.*)
 - Category area is more friendly... New options: -c [n] where 'n' is the number of category  - NEW (v2.3.*)
 - Added a new option to play only the audio track of a videoclip. Usage: [words] -n          - NEW (v2.3.*)
 - Added option for fullscreen (-f, --fullscreen). Usage: youtube-viewer [words] -f           - NEW (v2.3.*)
 - Added one new option '-c'. It shows available categories and will let you to choose one.   - NEW (v2.3.*)
 - Added one new option '-m'. It shows 3 pages of youtube video results. Usage: [words] -m    - NEW (v2.3.*)
 - For "-p" option has been added 3 pages of youtube video results (60 clips)                 - NEW (v2.3.*)
 - Added "-prefer-ipv4" to the mplayer line (videoclips starts in no time now).               - NEW (v2.3.*)
 - Search and play videos at 480p, 720p. Ex: [words] --480, [words] -p --480                  - NEW (v2.3.*)
 - Search has been corrected due to some YouTube changes...                                   - (v2.2.*)
 - Added support to play a video at 480p even if it's resolution is higher. Ex: [url] --480   - (v2.2.*)
 - Added a nice feature which prints some informations about the video which you watching at  - (v2.2.*)
 - Added support to play videos by your order. Example: after search results, insert: 3 5 2 1 - (v2.1.*)
 - Added support for next pages of video results (press ENTER when ask you to insert number)  - (v2.1.*)
 - Bug fixed: "Numbers go crazy after more than 20 video results".                            - (v2.1.*)
 - Added support to continue playing searched videos, usage: "youtube-viewer [words] -p"      - (v2.1.*)
 - Added support to print counted videos and support to insert the number of video to play it - (v2.1.*)
 - Added support to search YouTube Videos in script (ex: youtube-viewer avatar trailer 2009)  - (v2.0.*)
 - Added support for script to choose automat quality if it is lower than 1080p               - (v2.0.*)
 - Added support to choose the quality only between 720p and 1080p (if it is available)       - (v2.0.*)
 - Added support for an YouTube video code (ex: youtube-viewer WVTWCPoUt8w)                   - (v1.0.*)
 - Added support for 720p and 1080p YouTube Videos...                                         - (v1.0.*)
 
 Special thanks to:
 - Army (for bugs reports and for his great ideas)
 - dhn (for adding youtube-viewer in freshports.org)
 
 And also many thanks to:
 - everybody who use this script...
=cut

require LWP::UserAgent;

$os = $^O;
if ($os =~ /MSWin/) {
    $MPlayerLine = '"C:\\Program Files\\SMPlayer\\mplayer\\mplayer.exe" -prefer-ipv4 -cache 10000 -cache-min 1';
}
else {
    $MPlayerLine = 'mplayer -prefer-ipv4 -cache 100000 -cache-min 1';
}
$lwp = 'LWP::UserAgent'->new;
$lwp->agent('Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.634.0 Safari/534.16');
unless ($os =~ /MSWin/) {
    $bred = "\e[1;31m";
    $bgreen = "\e[1;32m";
    $reset = "\e[0m";
}
$a = $ARGV[0];
$b = $ARGV[1];
unless ($b) {
    $b = '';
}
unless ($a) {
    $a = '';
}
if ($a =~ /^(-v|--version)$/) {
    $appname =~ s/-/ /;
    $appname =~ s/(.)(.+) (.)/\U$1\E\L$2 \E\U$3\E/g;
    print "$appname $version\n";
    exit;
}
my($Picks, $pickcat, $Search);
$PickBackup = '';
$SearchBackup = '';
$MoreResults = '';
$FirstPage = 1;
$NextPage = 1;
$username = 0;
$pick = '';
if ($a =~ /^(-a|--author)$/) {
    @_ = ('4', '2', ')', ':', '%', '.');
    foreach $_ (@_) {
        $@ = '@';
        $_ = $@ | $_;
        &_;
        sub _ {
            $@ = $] * ($] * 3 + 1);
            print '=' x $@ . "\n";
        }
        print "\t" x $] . "\U$_\n\E";
        &_;
    }
    exit;
}
$Working = "\n[...] Working... Please wait...$reset\n";
$app = $0;
$app =~ s[(.*)/([^/]+)$][$2];
for ($n = 0; $n <= $#ARGV; ++$n) {
    push @args, $ARGV[$n];
}
if ($args[0]) {
    foreach $arg (@args) {
        next if $arg eq '';
        $arg =~ s/--novideo/-n/;
        $arg =~ s/--categories/-c/;
        $arg =~ s/^-([-]?)f(.*)/-f/;
        if ($arg eq '-n') {
            $MPlayerLine =~ s/mplayer /mplayer -novideo /;
            $MPlayerLine =~ s/mplayer.exe"/mplayer.exe" -novideo/;
            $SearchBackup = ' --480';
        }
        if ($arg =~ /^-([-]?)([\d]+)/) {
            $SearchBackup .= " $arg";
        }
        if ($arg eq '-c') {
            $categories = 'TRUE';
        }
        if ($arg eq '-m' or $arg eq '-p') {
            $MoreResults = 1;
        }
        else {
            $MoreResults = 0;
        }
        if ($arg eq '-f') {
            $SearchBackup .= " $arg";
            $MPlayerLine =~ s/mplayer /mplayer -fs /;
            $MPlayerLine =~ s/mplayer.exe"/mplayer.exe" -fs/;
        }
    }
}
if ($a eq '' or $a =~ /^([-]?)-([471]+)|^-f|^-m|^-n/ and $b eq '') {
    &InsertURL;
}
if ($a) {
    $a =~ s[http://.*embed/][]i;
    if ($a =~ m[^http://] and not $a =~ /youtube\.com/) {
        $connect = $lwp->get($a);
        if ($connect->content =~ m[youtube.com/v/([\w\-]{11})]) {
            $youtube = "v=$1";
            &GetYouTube;
        }
    }
}
if ($a =~ /^-g([^\=]*)=([\w]+)/) {
	$NextPage = 0;
    $a = "http://www.youtube.com/music/$2";
    $Search .= ' ';
}
elsif ($a =~ /^-g$/ and $b =~ /([\w]*)/) {
	$NextPage = 0;
    $a = "http://www.youtube.com/music/$b";
    $Search .= ' ';
}
sub InsertURL {
    print "\n$bred=>>$reset$bgreen Insert an YouTube URL or search something...\n$reset> ";
    chomp($youtube = <STDIN>);
    if ($youtube =~ m[^http://] and not $youtube =~ /youtube\.com/) {
        $connect = $lwp->get($youtube);
        if ($connect->content =~ m[youtube.com/v/([\w\-]{11})]) {
            $youtube = "v=$1";
            &GetYouTube;
        }
    }
    elsif (not $youtube =~ m[youtube.com(.+)v([\=/]+)([\w\-]{11})] and $youtube) {
        $SearchBackup .= ' ' . $youtube;
        $Search = $youtube;
        $number = 0;
        &Trizen;
    }
    elsif (not $youtube =~ /youtube.com(.+)([\w\-]{11})/) {
        print $bred;
        print "\n(x_x) Invalid URL...\n\n";
        print $reset;
        exit;
    }
    else {
        print $bgreen . '';
        &GetYouTube;
    }
}
if ($categories) {
    foreach $arg (@args) {
        if ($arg =~ /^([\d]+)$/) {
            $catnr = $1;
        }
    }
    unless ($catnr) {
        $catnr = '';
    }
}
if ($a =~ /youtube.com/ and not $a =~ m[v([=/]+)([\w\-]+)]) {
    $c = 'TRUE';
    $CatURL = $a;
    &CatSearch;
}
else {
    $c = '';
}
if ($a =~ /^-u/ and not $b =~ /(.+)/) {
    die "\nUsage:  $0 -u [username]\n\t$0 [username] -u\n\n";
}
if ($a =~ /^-([-]*)u/ and $b =~ /(.+)/ or $b =~ /^-([-]*)u/) {
    if ($b =~ /^-([-]*)u/) {
        $b = $a;
    }
    $username = 1;
    $NextPage = 0;
    $ytp = "http://gdata.youtube.com/feeds/api/users/$b/uploads";
    $connect = $lwp->get($ytp);
    $cc = $connect->content;
    if ($cc eq 'User not found') {
        die "\n(x_x) $cc...\n\n";
    }
    $cc =~ s/\n//g;
    $cc =~ s/<title type='text'>/\nTITLE=/g;
    @content = split(?\n?, $cc, 0);
    foreach $_ (@content) {
        if ($_ =~ /TITLE\=/) {
            $_ =~ s[TITLE\=([^\<]+)</title>(.+)v\=([\w\-]{11})(.+)duration([^\']*)'([\d\:]+)'(.+)][v=$3+title="$1">+video-time">$6];
            push @Videos, $_;
        }
    }
    print "\n";
    &PrintResults;
}
if ($a =~ /^([-]?)-c/) {
    if ($catnr =~ /(.+)/) {
        print $bgreen . "$Working";
    }
    if ($b =~ /([-]?)-([\d]+)/) {
        $PickBackup = $b;
    }
    $NextPage = 0;
    $connect = $lwp->get('http://www.youtube.com/videos');
    @cates = split(/\n/, $connect->content, 0);
    foreach $_ (@cates) {
        if ($_ =~ m[<a href=\"(.+)\">(.+)</a>([\ ]*)]) {
            push @cats, "$1 -*- $2";
        }
    }
    $nbr = 0;
    foreach $cat (@cats) {
        unless ($cat =~ /\?v=|http|title=\"|style=\"|onclick=\"/) {
            foreach $_ ($number = 0) {
                $number = $nbr + 1;
                $nbr = $number;
                while ($cat =~ /\"/) {
                    $cat =~ s/(.+)"(.+)-\*-/$1 -*-/;
                }
                push @categories, "$number - $cat\n" unless $cat =~ m[/t/|class=]g;
            }
        }
    }
    print "\n";
    foreach $_ (@categories) {
        next if $_ =~ m[/music|press_room]g;
        if ($_ =~ /opt_out/) {
            $caturl = 'http://www.youtube.com/videos';
            $catname = 'Main videos';
            $pick = '1';
        }
        if ($_ =~ /([\d]+) - (.+) -\*- (.+)/ and not $_ =~ /opt_out/) {
            $caturl = $2;
            $catname = $3;
            $pick = $1;
        }
        if ($catnr eq '' and $pick < 10) {
            print "$bred $pick$reset - $catname\n$bred";
        }
        else {
            if ($catnr eq '' and not $caturl =~ /ads/) {
                print "$bred$pick$reset - $catname\n$bred";
            }
        }
    }
    if (not $catnr =~ /(.+)/) {
        print $bgreen;
        print "\n=>> Insert the number of a category\n> ";
        print $reset;
        chomp($pickcat = <STDIN>);
    }
    else {
        $pickcat = $catnr;
    }
    foreach $cat (@categories) {
        if ($cat =~ /^$pickcat - (.+) -\*- (.+)/) {
            $catname = $1;
            $youtube = "http://www.youtube.com$1";
            $catname =~ s/^(.+)\?(.+)/$2/;
            $connect = $lwp->get($youtube);
            @content = split(/\n/, $connect->content, 0);
        }
    }
    &CatSearch;
    sub CatSearch {
        if ($c =~ /(.+)/) {
            $connect = $lwp->get($CatURL);
            @content = split(/\n/, $connect->content, 0);
        }
        foreach $_ (@content) {
            $_ =~ s[\&amp\;feature\=([\w\-]+)|\"\ data\-thumb(.+)\.jpg|src="http://(.+)gif\"|click="" onmousedown="][]g;
            if ($pickcat) {
                unless ($pickcat =~ /16/) {
                    $_ =~ s/\="http([^\"]+)"|yt.analytics([^\;]+)\;|"src//g;
                    $_ =~ s/" alt\="([^\"]+)"([\ ]*)//g;
                }
            }
            else {
                $_ =~ s/" src="(.+)\.jpg//g;
            }
            $_ =~ s/" onload="tn_load\(\)"  data-thumb alt="([^\"]+)\" |" onload="tn_load\(\)src|src=//g;
            push @Videos, $_ if $_ =~ /^</;
        }
        print "\n";
        &PrintResults;
    }
    ;
}
if ($a =~ m[youtube.com(.+)v([\=/]+)([\w\-]{11})]) {
    $youtube = "http://www.youtube.com/watch?v=$3";
    print $bgreen . "$Working";
    &GetYouTube;
}
if ($a =~ /^([\w\-]{11})$/) {
    $youtube = 'http://www.youtube.com/watch?v=' . $a;
    if ($a =~ /([\d]+)/ or $a =~ /([A-Z]+)/) {
        &GetYouTube;
    }
}
if ($a =~ /^-h$/ or $a =~ /^--help$/) {
    $appname =~ s/(.+)/\U$1\E/g;
    $app =~ s/(.*)\\([^\\]+)$/$2/;
    die "\n\t" . '=' x 17 . " $appname " . '=' x 17 . qq[
\t\t\t\t  by Trizen (trizen.go.ro)

 Usage: $app || [url] || [words] || [code]

Base Options: $app [...]
   [url]             : play an YouTube video by URL
   [code]            : play an YouTube video by code
   [words]           : search and play YouTube videos
  
Other options:
   -c, --categories  : shows available YouTube categories
   -n, --novideo     : plays only audio track of the video
   [words] -f        : plays all videos in fullscreen mode
   [words] -p        : plays all video results in order
   [words] -m        : shows 3 pages of video results (60 videos)
   [words] -n        : plays only the audio track of video results
   [words] -4, -7    : plays video results at 480p resolution (or 720p)
   [ytURL] -4, -7    : plays an YouTube video by URL at 480p (or 720p)
   [username] -u     : lists videos from a YouTube user profile
   -h, --help        : outputs this message...
 
Tips and tricks:
  1. Search something and press enter for the next page.
  2. Search and play all video results by adding "-p" after keywords.
  3. After search results, you can insert: 3 5 8 1 to play videos in your order
  4. Play all audio tracks of video results by adding "-p -n" after keywords.
  5. To listen music from YouTube use: "-c -n 10" and insert "all"
  6. Play videos one after one in fullscreen mode: "[words] -p -f"
  7. Play all video results at 480p: "[words] -p -4" (or 720p => "-7")
  8. Play all videos from a user at 480p: "[username] -u -4" and insert "all"
  9. Play a genre of music from YT: "-g [genre]". Example: "-g reggae -n"

];
}
elsif ($a =~ /^-p/) {
    die "Usage: $app [words] $a\n";
}
elsif (not $a =~ /^-([-]?)4|^-([-]?)7|^-g|^-p|^-n|^-([-]?)1|^-f|^-m/ and $a =~ /^-/) {
    die "Unknown option '${a}'\n";
}
$Search = join(' ', @args) . ' ';
&Trizen;
sub Trizen {
    $PageNumber = 1;
    $FirstPage = 1;
    $SearchBackup .= ' ' . $Search;
    $Search =~ s/\ ([-]?)-([\d]+)|\ -p|\ -m|\ ([-]?)-f([^\ ]*)|\ -n//g;
    $ys = "http://www.youtube.com/results?search_query=$Search&aq=f";
    if ($MoreResults eq 1) {
        $ys = "http://www.youtube.com/results?search_query=$Search&aq=f";
        $connect = $lwp->get($ys);
        push @source, $connect->content . "\n";
        for ($number = 2; $number <= 3; ++$number) {
            $ys = "http://www.youtube.com/results?search_query=$Search".
				  "&suggested_categories=10%2C24&page=$number";
            $connect = $lwp->get($ys);
            push @source, $connect->content . "\n";
        }
        $source = join('', @source);
        @content = split(?\n?, $source, 0);
        print "\n";
    }
    else {
        $connect = $lwp->get($ys);
        @content = split(/\n/, $connect->content, 0);
        if ($connect->content =~ m[class="largeText">(.+)<a href="/results\?search_query=(.+)&search=Search(.+)"(.+)</a></span></div>]) {
            $spell = $4;
            $research = "http://www.youtube.com/results?search_query=$2&search=Search";
            $spell =~ s[<i>|</i>|<b>|</b>|>|<][]g;
            print "\n$bgreen=>> Did you mean: ";
            print "$bred$spell$reset (Y/n)\n> ";
            chomp($yn = <STDIN>);
            if ($yn eq '' or $yn =~ /y/i) {
                $connect = $lwp->get($research);
                splice @content;
                @content = split(/\n/, $connect->content, 0);
            }
        }
        elsif ($connect->content =~ m[Results for: <a href="/results\?search_query=(.+)">(.+)</a>]) {
            $research = "http://www.youtube.com/results?search_query=$1&search=Search";
            print "$bgreen\n=>> Did you mean: $bred$2$reset (Y/n)\n> ";
            chomp($yn = <STDIN>);
            if ($yn eq '' or $yn =~ /y/i) {
                $connect = $lwp->get($research);
                splice @content;
                @content = split(/\n/, $connect->content, 0);
            }
        }
        print "\n";
    }
    &Search;
}
sub Search {
    splice @Videos;
    splice @codes;
    foreach $_ (@content) {
        if ($_ =~ m[^<a href="/watch\?v=]) {
            $_ =~ s/\" data-thumb(.+)\.jpg//g;
            push @Videos, $_;
        }
    }
    &PrintResults;
}
sub PrintResults {
    $number = 0;
    foreach $_ (@Videos) {
        chomp($line = $_);
        if ($line =~ /v\=([\w\-]{11})(.*)title="([^\"]+)(.*)time\">([\d\:]*)/) {
            ++$number;
            $code = $1;
            $title = $3;
            $time = $5;
            unless ($time =~ /\:/) {
                $sec = $time;
                $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";
                }
            }
            push @codes, "$number - $code $time";
            $title =~ s/\&amp;\#39;/'/g;
            $title =~ s/\&amp;/&/g;
            $title =~ s/\&quot;/"/g;
            $title =~ s/\&amp;\#([\d]+);|amp;|quot;//g;
            $title =~ s/src class=$|class=$//g;
            print "$bred";
            if ($number < 10) {
                print ' ';
            }
            print "$number$reset - $title ($time)\n$bred";
        }
    }
    if ($SearchBackup) {
        if ($SearchBackup =~ /\ -p/) {
            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($youtube = <STDIN>);
    $PickBackup = $youtube;
    if ($youtube =~ /^(q|quit|exit)$/) {
        print "\n[*] Exiting...\n\n";
        exit;
    }
    if ($PickBackup =~ m[^http://] and not $PickBackup =~ /youtube\.com/) {
        $connect = $lwp->get($PickBackup);
        if ($connect->content =~ m[youtube.com/v/([\w\-]{11})]) {
            $youtube = "v=$1";
            &GetYouTube;
        }
    }
    if ($PickBackup =~ /([\d]+) ([-]?)-([\d]+)/) {
        $youtube = $1;
    }
    print $bgreen . "$Working";
    if ($youtube =~ /^([\d]+)$/) {
        if ($youtube > $number or $youtube eq 0) {
            print "\n";
            &PrintResults;
        }
    }
    if ($youtube =~ /^([\d]+)$/) {
        $PICK = $1;
        &ForeachCode;
    }
    if ($youtube eq '' and $NextPage eq 1) {
        $PageNumber = $FirstPage;
        ++$PageNumber;
        print "\n[!]$bgreen Page:$reset $PageNumber\n";
        $SearchURL = "http://www.youtube.com/results?search_query=$Search".
					 "&suggested_categories=10%2C24&page=$PageNumber";
        $connect = $lwp->get($SearchURL);
        splice @content;
        @content = split(/\n/, $connect->content, 0);
        print "\n";
        $FirstPage = $PageNumber;
        &Search;
    }
    elsif ($NextPage eq 0 and $youtube eq '') {
        print "\n";
        &PrintResults;
    }
    if ($youtube eq 'all') {
        $SearchBackup .= '  --1080';
        splice @picks;
        for ($i = 1; $i <= $number; ++$i) {
            push @picks, "$i";
        }
        &PicksArea;
    }
    if ($youtube =~ m[v([=/]+)([\w\-]{11})]) {
        $youtube = "http://youtube.com/watch?v=$2";
        &GetYouTube;
    }
    if ($youtube =~ /([\d]+) ([\d]+)/) {
        @picks = split(' ', $youtube, 0);
        &PicksArea;
        sub PicksArea {
            $NrOfPicks = @picks;
            $no = 0;
            $lastpick = '';
            $number = 0;
            &ForeachPick;
        }
        
        sub ForeachPick {
            for ($number = $no; $number <= $NrOfPicks; ++$number) {
                $no = $number;
                $pick = $picks[$number];
                next if $lastpick eq $pick;
                &PrintResults if $number eq $NrOfPicks;
                $Picks = 'TRUE';
                $lastpick = $pick;
                &PlayBack;
            }
        }
        ;
    }
    if ($youtube ne 'all' and $youtube =~ /(.+)/ and not $youtube =~ /^(q|quit|exit)$/) {
        $Search = $youtube;
        &Trizen;
    }
}
sub PlayBack {
    foreach $Code (@codes) {
        if ($Picks eq 'TRUE' and not $Code =~ /^$pick - /) {
            next;
        }
        elsif ($Code =~ /^$pick - ([\w\-]{11})(.*)/ and $Picks eq 'TRUE') {
            $VideoCode = $1;
            $duration = $2;
            $youtube = "http://www.youtube.com/watch?v=$1";
        }
        elsif ($Code =~ /(.+)([\w\-]{11})(.*)/ and $Picks ne 'TRUE') {
            $VideoCode = $2;
            $duration = $2;
            $youtube = "http://www.youtube.com/watch?v=$2";
        }
        &GetYouTube;
    }
}
sub VideoCheck {
    if (not $connect->content =~ /itag=34/ and $connect->content =~ /itag=([\d]+)([\W]+)/) {
        $itag = $1;
        $streaming =~ s/(.*)http(.+)itag=$itag([^\%]*)(.+)/http$2itag=$itag$3/;
        unless ($SearchBackup =~ / ([-]?)-f/) {
            $MPlayerLine =~ s/-fs //;
        }
        &Description;
        &MPlayer;
    }
    if ($b =~ /^([-]?)-4/ or $PickBackup =~ / ([-]?)-4/ or $SearchBackup =~ / ([-]?)-4/) {
        $streaming =~ s/(.*)http(.+)itag=34([^\%]*)(.+)/http$2itag=34$3/;
        unless ($SearchBackup =~ / ([-]?)-f/) {
            $MPlayerLine =~ s/-fs //;
        }
        &Description;
        &MPlayer;
    }
    if ($b =~ /^([-]?)-7/ or $PickBackup =~ / ([-]?)-7/ or $SearchBackup =~ / ([-]?)-7/) {
        if ($connect->content =~ /itag=22/) {
            $streaming =~ s/(.*)http(.+)itag=22([^\%]*)(.+)/http$2itag=22$3/;
            $MPlayerLine =~ s/mplayer -p/mplayer -fs -p/;
            $MPlayerLine =~ s/mplayer.exe" -p/mplayer.exe" -fs -p/;
            &Description;
            &MPlayer;
        }
        else {
            unless ($SearchBackup =~ / ([-]?)-f/) {
                $MPlayerLine =~ s/-fs //;
            }
            $streaming =~ s/(.*)http(.+)itag=34([^\%]*)(.+)/http$2itag=34$3/;
            &Description;
            &MPlayer;
        }
    }
    if ($b =~ /^([-]?)-1/ or $PickBackup =~ / ([-]?)-1/ or $SearchBackup =~ / ([-]?)-1/) {
        if ($connect->content =~ /fmt_list=37/) {
            $streaming =~ s/(.*)http(.+)itag=37([^\%]*)(.+)/http$2itag=37$3/;
            $MPlayerLine =~ s/mplayer -p/mplayer -fs -p/;
            $MPlayerLine =~ s/mplayer.exe" -p/mplayer.exe" -fs -p/;
            &Description;
            &MPlayer;
        }
        elsif ($connect->content =~ /itag=22/) {
            $streaming =~ s/(.*)http(.+)itag=22([^\%]*)(.+)/http$2itag=22$3/;
            $MPlayerLine =~ s/mplayer -p/mplayer -fs -p/;
            $MPlayerLine =~ s/mplayer.exe" -p/mplayer.exe" -fs -p/;
            &Description;
            &MPlayer;
        }
        else {
            unless ($SearchBackup =~ / ([-]?)-f/) {
                $MPlayerLine =~ s/-fs //;
            }
            $streaming =~ s/(.*)http(.+)itag=34([^\%]*)(.+)/http$2itag=34$3/;
            &Description;
            &MPlayer;
        }
    }
    &Description;
    &ResolutionSearch;
}
sub Description {
    if ($connect->content =~ /likes\">([\d\.\,]+)(.+)dislikes\">([^\<]+)/) {
        $like = $1;
        $dislike = $3;
    }
    if ($connect->content =~ m[video-date" >([\ ]*)\n([\t\ ]*)(.+)\n([\ ]*)</span>]) {
        $date = $3;
        $date =~ s[<span>(.+)</span>][];
    }
    if ($connect->content =~ /<meta name="title" content="(.+)">/) {
        $title = $1;
        $title =~ s/\&amp;quot;/"/g;
        $title =~ s/\&amp;amp;/&/g;
        $title =~ s/\&amp;\#39;/'/g;
        $title =~ s/\&amp;\#([\d]+);//g;
    }
    print "\n$bred=>> $reset";
    print $bgreen;
    print "$title\n";
    print $reset;
    print '-' x 80;
    if ($connect->content =~ m[description" >(.*)</p>]) {
        $desc = $1;
        $desc =~ s[<em>|</em>][]g;
        $desc =~ s[<br\ />|<br/>][\n]g;
        $desc =~ s[<a\ href\=\"\#\"\ onclick\=\"yt\.www\.watch\.player\.seekTo\((.+)\);return\ false;\">|<a\ href\=\"(.+)\"\ target\=\"\_blank\"\ title\=\"|\"\ rel\=\"nofollow\"\ dir="ltr\"\ class\=\"(.*)\">(.+)</a>|<wbr />|</a>|<wbr>\&shy;|<wbr>][]g;
        $desc =~ s/\&quot;/"/g;
        $desc =~ s/&amp;/&/g;
        $desc =~ s/\&\#39;/'/g;
        $desc =~ s/\&\#([\d]+);//g;
        print "\n$desc\n";
    }
    print '-' x 80 . "\n";
    print "$bred=>> $reset";
    print $bgreen;
    print "View & Download\n";
    print $reset;
    print '-' x 80;
    $get = $streaming;
    $get =~ s/http:([^\%]+)(.*)/http:$1/;
    $get =~ s/(.*)http/http/;
    $get =~ s/\\//g;
    print "\n* URL $url\n";
    print "* GET $get\n";
    print '-' x 80 . "\n";
    $count = $title =~ s/(.)/$1/gs;
    if ($count <= 40) {
        print "$bred\t\t=>> ";
    }
    elsif ($count >= 40 and $count <= 55) {
        print "$bred\t=>> ";
    }
    elsif ($count >= 56) {
        print "$bred  =>> ";
    }
    print $bgreen;
    print "$title";
    print $reset;
    print "$bred <<=\n";
    print $reset;
    if ($connect->content =~ m[href="/user/([\w\-]+)"><strong>]) {
        print "\n* Uploaded by $1\n";
    }
    if ($duration) {
        print "* Duration:$duration\n";
    }
    if ($connect->content =~ m[<strong>([\d\,]+)</strong>]) {
        print "* Views: $1\n";
    }
    if ($date =~ /(.+)/) {
        print "* Date: $date\n";
    }
    if ($like =~ /(.+)/) {
        print "* Likes: $like+; Dislikes: $dislike-\n";
    }
    print '-' x 80 . "\n\n";
}
sub MPlayer {
    $streaming =~ s/%(.+)$//;
    `$MPlayerLine "$streaming"`;
    print "\n";
    if ($Picks) {
        if ($Picks eq 'TRUE') {
            &ForeachPick;
        }
    }
    if ($Search or $a =~ /^([-]?)-c/ or $username eq 1) {
        &PrintResults;
    }
    exit;
}
sub ForeachCode {
    foreach $code (@codes) {
        if ($code =~ /^$PICK - ([\w\-]{11})(.*)/) {
            $code = $1;
            $duration = $2;
            $youtube = 'http://www.youtube.com/watch?v=' . $code;
            $url = $youtube;
        }
    }
    &GetYouTube;
}
sub GetYouTube {
    if ($youtube =~ m[v([=/]+)([\w\-]{11})]) {
        $VideoCode = $2;
        $youtube = 'http://www.youtube.com/watch?v=' . $2;
    }
    $Count = $VideoCode =~ s/(.)/$1/gs;
    if ($Count ne 11) {
        print "\n$bred(x_x) Invalid URL...$reset\n\n";
        exit;
    }
    unless ($youtube =~ /^http:/) {
        $youtube = 'http://' . $youtube;
    }
    $connect = $lwp->get($youtube);
    $url = $youtube;
    if ($connect->content =~ /fmt_url_map=([^\&]+)\&/) {
        $streaming = $1;
        $streaming =~ s/%3A/:/gi;
        $streaming =~ s[%2F][/]gi;
        $streaming =~ s/%26/&/g;
        $streaming =~ s/%3D/=/gi;
        $streaming =~ s/%3F/?/gi;
        $streaming =~ s/%252C/,/gi;
        &VideoCheck;
    }
    else {
        $unable = "$bred(x_x) Unable to play: $reset$url\n";
        print $bred;
        print "\n(x_x) Something went wrong...\n\n";
        print $reset;
        if (not $SearchBackup =~ / -p/ or $NrOfPicks eq '') {
            print "$unable\n";
        }
        if ($Search =~ /(.+)/) {
            unless ($SearchBackup =~ / -p/) {
                sleep 1;
                &PrintResults;
            }
        }
        else {
            exit;
        }
    }
    if ($streaming =~ /(.+)/) {
        &Description;
    }
    else {
        print $unable;
    }
    &ResolutionSearch;
}
sub ResolutionSearch {
    if ($connect->content =~ /fmt_list=37/ and not $SearchBackup =~ / -p/) {
        &pick;
    }
    elsif ($SearchBackup =~ /-p/) {
        if ($connect->content =~ /fmt_list=37/) {
            $streaming =~ s/(.*)http(.+)itag=37([^\%]*)(.+)/http$2itag=37$3/;
            $MPlayerLine =~ s/mplayer -p/mplayer -fs -p/;
            $MPlayerLine =~ s/mplayer.exe" -p/mplayer.exe" -fs -p/;
            &Description;
            &MPlayer;
        }
    }
    if ($connect->content =~ /itag=22/) {
        $streaming =~ s/(.*)http(.+)itag=22([^\%]*)(.+)/http$2itag=22$3/;
        $MPlayerLine =~ s/mplayer -p/mplayer -fs -p/;
        $MPlayerLine =~ s/mplayer.exe" -p/mplayer.exe" -fs -p/;
        &MPlayer;
    }
    else {
        $streaming =~ s/(.*)http(.+)itag=34([^\%]*)(.+)/http$2itag=34$3/;
        unless ($SearchBackup =~ / ([-]?)-f/) {
            $MPlayerLine =~ s/-fs //;
        }
        &MPlayer;
    }
}
sub pick {
    print "$bred=>>$reset $bgreen Please choose quality of video (default: 1)$reset\n\n";
    print "  $bred 1$reset - 1280x720  (720p)\n  $bred 2$reset - 1920x1080 (1080p)\n  ";
    print '=' x 23 . "\n";
    print $bgreen;
    print "\n=>> Pick format: ";
    print $reset;
    chomp($pick = <STDIN>);
    print '-' x 80 . "\n\n";
    if ($pick ne 2) {
        $streaming =~ s/(.*)http(.+)itag=22([^\%]*)(.+)/http$2itag=22$3/;
        $MPlayerLine =~ s/mplayer -p/mplayer -fs -p/;
        $MPlayerLine =~ s/mplayer.exe" -p/mplayer.exe" -fs -p/;
        &MPlayer;
    }
    if ($pick eq 2) {
        $streaming =~ s/(.*)http(.+)itag=37([^\%]*)(.+)/http$2itag=37$3/;
        $MPlayerLine =~ s/mplayer -p/mplayer -fs -p/;
        $MPlayerLine =~ s/mplayer.exe" -p/mplayer.exe" -fs -p/;
        &MPlayer;
    }
}