2013-01-16 208 views
15

我通常使用keepvid.com或Firefox下載助手插件來下載YouTube視頻。是否可以使用Linux中提供的wget命令下載它們?用wget下載YouTube視頻

另外,我有一個VPS服務器。我想在我的VPS服務器(cPanel)上下載視頻。

如果有可能,如果有可能如何?

謝謝。

回答

18
#!/usr/bin/perl 

use strict; 
use warnings; 
## Two arguments 
## $1 YouTube URL from the browser 
## $2 Prefix to the file name of the video (optional) 
# 

## 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 
my ($download) = $html =~ /"url_encoded_fmt_stream_map"([\s\S]+?)\,/ig; 

## Clean up the URL by translating Unicode and removing unwanted strings 
$download =~ s/\:\ \"//; 
$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/sig=/signature=/g; 
$download =~ s/\\u0026/\&/g; 
$download =~ s/(type=[^&]+)//g; 
$download =~ s/(fallback_host=[^&]+)//g; 
$download =~ s/(quality=[^&]+)//g; 

## Collect the URL and signature since the HTML page randomizes the order 
my ($signature) = $download =~ /(signature=[^&]+)/; 
my ($youtubeurl) = $download =~ /(http.+)/; 
$youtubeurl =~ s/&signature.+$//; 

## Combine the URL and signature in order to use in Wget 
$download = "$youtubeurl\&$signature"; 

## A bit more cleanup 
$download =~ s/&+/&/g; 
$download =~ s/&itag=\d+&signature=/&signature=/g; 

## Print the file name of the video collected from the web page title for us to see on the CLI 
print "\n Download: $prefix$title.webm\n\n"; 

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

#### EOF ##### 

這是我一直在使用的Perl腳本。我不確定它從哪裏來......雖然效果很好。

+0

感謝你的分享。這樣可行。 –

+0

好,它的工作... – Amini

+0

評論,因爲我今天在三個不同的視頻上嘗試了這個腳本,併爲他們所有人產生了一個空文件。 YouTube的問題可能會發生一些變化。我還沒有做過故障排除,但我想添加一個警告。 – LiberalArtist

19

我會推薦youtube-dl作爲YouTube和其他視頻網站的控制檯下載程序。

+3

晚會有點遲,但剛發現這可以通過Homebrew安裝。很簡單,謝謝! – JAL

+0

$ sudo pip安裝youtube-dl –