mecrazycoder has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, Recently I downloaded below code from one site. Actually this code is used for downloading youtube videos. But the video which is getting downloaded is always corrupted. What may be the problem. Please help me whats wrong with this code.Thanks in advance
use LWP; use URI::Escape; my $url; foreach $_ (@ARGV) { $_ =~ s[^.*[=/]?([\w\-]{11}).*][$1]; my $ua = LWP::UserAgent->new; #$ua->proxy(['http', 'ftp'], 'http://192.16.3.254:8080/'); $c = uri_unescape($t = $ua->get("http://www.youtube.com/get_video_info +?&video_id=$_&el=detailpage&ps=default&eurl=&gl=US&hl=en")->content); $t =~ /&title=([^&]+)&/ ? $t = uri_unescape($1) : ''; $t =~ s[\+|/][ ]g ? $t = quotemeta $t : ''; $c =~ s/.*fmt_url_map=[\d\|]+([^,]+).*/$1/; open(FILE, '>Test.mp4'); my $resp = $ua->get( $c ) or die; print FILE $resp->content; close(FILE); }

Replies are listed 'Best First'.
Re: Whats wrong with this code
by moritz (Cardinal) on Jul 22, 2011 at 13:31 UTC
    You should check the HTTP response code (at least $resp->is_success), and binmode your file handle.

    It might also be worth looking into the created file with a text editor, maybe you get some html (for example error document) instead of an actual movie.

      Let me check and let you know. Thanks
Re: Whats wrong with this code
by Anonymous Monk on Jul 22, 2011 at 13:42 UTC
Re: Whats wrong with this code
by cdarke (Prior) on Jul 22, 2011 at 13:37 UTC
    I don't know this format, but this:
    $t =~ s[\+|/][ ]g
    might have downloaded incorrectly if you did a copy/paste. The whitespace inside [ ]could have been a tab.
      I did the above changes and tried but no use. Still I am getting corrupted video.

        I did the above changes and tried but no use.

        Show the changed code :) your probably forgot to binmode ... you could simply use mirror( $url, $filename ) or get-flash-videos ...