SVN: toys/rsget.pl/Video/YouTube

sparky sparky at pld-linux.org
Sun Apr 18 15:05:33 CEST 2010


Author: sparky
Date: Sun Apr 18 15:05:33 2010
New Revision: 11335

Modified:
   toys/rsget.pl/Video/YouTube
Log:
- implemented robust video format detection. getter is now able
  to download videos which don't have format 18


Modified: toys/rsget.pl/Video/YouTube
==============================================================================
--- toys/rsget.pl/Video/YouTube	(original)
+++ toys/rsget.pl/Video/YouTube	Sun Apr 18 15:05:33 2010
@@ -25,9 +25,38 @@
 		[ 18, "mp4", "LQ" ],
 	);
 
+	sub best_format
+	{
+		/"fmt_map"\s*:\s*"(.*?)"/ or m{&fmt_map=(\S+?)&}
+			or return undef;
+		my %fmts = map { m{(\d+)/}; $1 => $_ } split ",", uri_unescape( $1 );
+
+		foreach my $f ( @fmt ) {
+			return $f
+				if exists $fmts{ $f->[0] };
+		}
+
+		return undef;
+	}
 start:
 	GET( $-{_uri} );
 
+	ERROR( "file cannot be accessed" )
+		if /<div id="" class="yt-alert-content">/;
+
+	# try to find best format
+	$-{fmt} = best_format();
+
+	# format 18 is almost always hidden,
+	# if the best format we found is 5 we check for 18 manually
+	GOTO stage_got_format
+		if $-{fmt};
+
+	GET( "$-{_uri}&fmt=18" );
+
+	$-{fmt} = best_format();
+
+stage_got_format:
 	! m{"video_id": "(.*?)"} or m{'VIDEO_ID': '(.*?)'};
 	my $id = $1;
 
@@ -35,24 +64,17 @@
 	my $name = de_ml( de_ml( $1 ) );
 	$name =~ s{/}{_}g;
 
-	! /"fmt_map"\s*:\s*"(.*?)"/ or m{&fmt_map=(\S+?)&};
-	my %fmts = map { m{(\d+)/}; $1 => $_ } split ",", uri_unescape( $1 );
-
-	my $fmt = $fmt[ $#fmt ];
-	foreach my $f ( @fmt ) {
-		if ( exists $fmts{ $f->[0] } ) {
-			$fmt = $f;
-			last;
-		}
-	}
-
 	! /"t"\s*:\s*"(.*?)"/ or m{&t=(\S+?)&};
 	my $t = $1;
 
+	my $fmt = $-{fmt};
+	my $fmtstr = $fmt ? "&fmt=$fmt->[0]" : "";
+	$fmt //= [ 5, "flv", "vLQ" ];
+
 	my $fname = $name . "." . $fmt->[1];
 	INFO( name => $fname, fmt => $fmt->[0], quality => $fmt->[2] );
 
-	DOWNLOAD( "http://youtube.com/get_video?video_id=$id&t=$t&fmt=$fmt->[0]",
+	DOWNLOAD( "http://youtube.com/get_video?video_id=$id&t=$t$fmtstr",
 		fname => $fname );
 
 # vim: filetype=perl:ts=4:sw=4


More information about the pld-cvs-commit mailing list