Linux 使用 wget 下载 YouTube 视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14368823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download YouTube videos with wget
提问by Fatih Toprak
I usually use keepvid.com or a Firefox download helper plugin for dowloading YouTube videos. Is it possible to download them with the wget command available in Linux?
我通常使用 keepvid.com 或 Firefox 下载助手插件来下载 YouTube 视频。是否可以使用 Linux 中可用的 wget 命令下载它们?
Also, I have a VPS server. I want to download videos on my VPS server (cPanel).
另外,我有一个 VPS 服务器。我想在我的 VPS 服务器 (cPanel) 上下载视频。
If is possible and if so how?
如果可能,如果可能,如何?
Thanks.
谢谢。
采纳答案by Fatih Toprak
#!/usr/bin/perl
use strict;
use warnings;
## Two arguments
## YouTube URL from the browser
## 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 #####
This is a Perl script that I have been using for a while. I am not sure where it's from though... It works great.
这是我使用了一段时间的 Perl 脚本。我不确定它是从哪里来的……它很好用。
回答by Stas
I would recommend youtube-dlas a console downloader from YouTube and other video sites.
我会推荐youtube-dl作为来自 YouTube 和其他视频网站的控制台下载器。