为什么这个perl脚本不能使用wget保存youtube视频,正常工作?

时间:2012-09-14 04:10:02

标签: perl bash ubuntu wget

第一个参数应该是视频的网址。任何人都可以向我指出为什么这不起作用?

我启动了其中一个ubuntu 12.04的ec2实例,它已经安装了perl和wget http://cloud-images.ubuntu.com/precise/current/

此脚本源自此处: https://calomel.org/youtube_wget.html

#!/usr/bin/perl

use strict;
use warnings;

## collect the URL from the command line argument
my $url = $ARGV[0] or die "\nError: You need to specify a YouTube URL\n\n";

## declare the user defined file name prefix 
my $prefix = defined($ARGV[1]) ? $ARGV[1] : "";

## download the html code from the youtube page
my $html = `wget -Ncq -e "convert-links=off" --keep-session-cookies --save-cookies /dev/null --no-check-certificate "$url" -O-`  or die  "\nThere was a problem downloading the HTML file.\n\n";

## collect the title of the page to use as the file name
my ($title) = $html =~ m/<title>(.+)<\/title>/si;
$title =~ s/[^\w\d]+/_/g;
$title =~ s/_Youtube_//ig;
$title =~ s/^_//ig;
$title = lc ($title);

## collect the URL of the video and translate to the proper URL
my ($download) = $html =~ /url_encoded_fmt_stream_map([\s\S]+?)fallback_host/ig;
$download =~ s/\\u0026/\&/g;
$download =~ s/^\=url%3D//g;
$download =~ s/%25/%/g;
$download =~ s/%3A/:/g;
$download =~ s/%2F/\//g;
$download =~ s/%3F/\?/g;
$download =~ s/%3D/\=/g;
$download =~ s/%252C/%2C/g;
$download =~ s/%26/\?/g;
$download =~ s/%253A/\:/g;

## print the file name of the video
print "\n     Download:   $prefix$title.webm\n\n";

## Download the file.
  my $error_code=system("wget -Ncq -e \"convert-links=off\" --load-cookies /dev/null --tries=50 --timeout=45 --no-check-certificate $download -O $prefix$title.webm &")>>8;

修改 运行perl脚本后,视频无法下载(大小为0字节)

$ ./youtube_get_videos.pl http://www.youtube.com/watch?v=ejkm5uGoxs4

     Download:   radscorpion_youtube.webm

$ ls -lh
-rw-rw-r-- 1 ubuntu ubuntu    0 Sep 14 04:40 radscorpion_youtube.webm

2 个答案:

答案 0 :(得分:1)

我怀疑你的&命令中有“donald-duck”,&符号system。您不是在等待程序完成。我不知道这可能需要多长时间,但它不会是瞬间的。您(可能)从system获取的错误代码0将由shell返回(&将调用shell),而不是wget

答案 1 :(得分:1)

让我为你指出:WWW::YouTube::Download。安装此模块后,只需使用捆绑的youtube-download实用程序:

stas@Stanislaws-MacBook-Pro:~$ youtube-download -o "{title}.{suffix}" http://www.youtube.com/watch?v=ejkm5uGoxs4 
--> Working on ejkm5uGoxs4
Downloading `RADSCORPION!.mp4`
26320989/26320989 (100.00%)
Download successful!
stas@Stanislaws-MacBook-Pro:~$ file RADSCORPION\!.mp4 
RADSCORPION!.mp4: ISO Media, MPEG v4 system, version 2
stas@Stanislaws-MacBook-Pro:~$

像魅力一样工作:)