如何下载YouTube视频?

如何从YouTube下载视频的说明是什么?

注意:服务条款违规

请注意,通过以下任何答案,您将违反YouTube的服务条款(TOS)。特别是第5.B节:

内容按原样提供给您。您只能根据本服务的提供功能和这些服务条款的规定,以信息和个人使用为目的访问内容。除非YouTube在该内容的服务上显示“下载”或类似链接,否则您不得下载任何内容。未经YouTube或内容的相应许可方事先书面同意,您不得复制、翻印、分发、传输、广播、展示、销售、授权或以其他方式利用任何内容进行其他任何目的。YouTube及其许可方保留在服务和内容中未明示授予的所有权利。



14嗯,光是在YouTube上观看视频也违反了服务条款,因为你正在传输内容... - PeterM
4而且通过观看,你也在展示它。 - nehem
28个回答

youtube-dl

从官方网站安装:

sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

这个软件也可以在Ubuntu仓库中找到,但你经常需要用sudo youtube-dl -U命令来保持它的最新。

安装和升级方法(使用pip):/

sudo -H pip install --upgrade youtube-dl

使用方法:

youtube-dl YouTube-(or-other-website)-video-link

所以它将下载视频。
你也可以查看this,其中有非常详细的使用文档。

7如果你没有aptitude,你可以使用apt-get代替。还请注意,在运行sudo aptitude install ...sudo apt-get install ...命令之前,应该先运行sudo aptitude updatesudo apt-get update - Eliah Kagan
sudo: aptitude: command not found - siraj
@siraj 使用apt-get代替。 - Rahul Virpara
值得注意的是,可以从http://rg3.github.com/youtube-dl/下载youtube-dl的新版本。 - paed808
10我建议在执行apt-get命令后立即运行sudo youtube-dl -U - bohdan_trotsenko
1@modosansreves 有点好笑,它在关闭之前建议重新启动 youtube-dl :p - Thomas
3请记住,youtube-dl还可以从许多其他网站下载视频。 - PythoNic
在开始下载之前,请运行以下命令: sudo add-apt-repository ppa:nilarimogard/webupd8 -y ... sudo apt-get update ... sudo apt-get dist-upgrade... - Sorter
这个应用有质量选项吗?可以从YouTube提供的众多预渲染格式中进行选择吗? - ThorSummoner
3@ThorSummoner 你可以使用 --max-quality 或者 --format 选项。示例:youtube-dl http://www.youtube.com/videolink --max-quality 360p 或者 youtube-dl http://www.youtube.com/videolink --format mp4 - Yasser Hussain
它给了我一个错误:File "/usr/bin/youtube-dl", line 1406, in <genexpr> url_map = dict((ud['itag'][0], ud['url'][0] + '&signature=' + ud['sig'][0]) for ud in url_data) KeyError: 'sig' 我使用的是 Elementary OS Luna。 - Stephane
2不要玩弄PPA,只需使用sudo pip install youtube-dl。@modosansreves我强烈不同意,程序不应该干扰由软件包管理器安装的东西。下次官方仓库升级时,dpkg可能会因为修改的文件而出现问题。 - alexia
下载文件的目标是什么? - m2j
@m2j 默认情况下,youtube-dl将在您打开的终端的工作目录中下载文件。 - Yogesh Umesh Vaity
Debian stable上的apt-get安装无法从YouTube下载视频。但是pip安装了一个更近期的youtube-dl版本,可以正常工作。此外,请参阅@Prateek Joshi的答案和@nyuszika7h的评论。 - Paul Rougieux
免责声明:我只是提出可能性。合法与否,由您决定。根据《条款和条件》第5节的官方说明:https://www.youtube.com/static?template=terms除非YouTube在服务中显示“下载”或类似链接,否则您不得下载任何内容。话虽如此,除了使用youtube-dl下载视频外,您还可以使用以下示例中的-x选项仅下载和提取视频的音频部分:youtube-dl -x --audio-format mp3 https://www.youtube.com/watch?v=IDofTheVideo - Hopping Bunny
已安装但无法下载... - Ashwani Panwar

youtube-dl 是一个非常好用的工具,可以下载 YouTube 视频。使用以下命令进行 安装
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

或者使用pip(python3-pip):
sudo -H pip install --upgrade youtube-dl

从Ubuntu的软件仓库中获取(较旧版本)
sudo apt-get install youtube-dl

然后这样使用它:
youtube-dl [VIDEO_LINK]

要了解更多高级选项,请运行

man youtube-dl

或者查看文档

原始答案

对于Firefox用户:

您可以在Firefox中安装Video DownloadHelper插件,并下载任何嵌入的对象,包括YouTube视频。

Enter image description here

Enter image description here


不适用于Firefox 35版本。 - Aquarius_Girl
Youtube不在支持的网站列表中,我已经移除了视频下载助手。 - Paul Rougieux
1@PaulRougieux,截至今天(当前版本47.0),它与我最新的Firefox浏览器兼容。我已经在我的Ubuntu 16.04上测试了下载多个YouTube视频的功能。 - adadion
使用apt-get安装的youtube-dl版本较旧,无法正常工作。根据手动安装说明,按照此处所述使用wget进行安装是有效的。 - Nagabhushan S N

迷你管

Install minitube

Minitube 是一款 YouTube 桌面应用程序。通过它,您可以以全新的方式观看 YouTube 视频:输入一个关键词,Minitube 就会给您提供无尽的视频流。Minitube 的目标不是克隆原始的 YouTube 网页界面,而是要创造一种全新的类似电视的体验。
它不需要 Flash Player。您可以通过这个应用程序观看和下载视频。

Screenshoot1

Screenshoot2


我很好奇为什么没有人提到这个。顺便说一下,能给个Ubuntu或Linux的截图吗? - Oxwivi
6这并不是问题所指的内容。你能从这里下载YouTube视频吗? - justingrif
5这在我的Ubuntu 13.04上不起作用,每次都在10秒后崩溃。 - iammilind
似乎下载功能不再起作用了。 - fkoessler
@Marronsuisse,正如Ubuntu应用商店的一位评论者所说,为了避免在13.04版本上安装时出现崩溃,您需要安装phonon-backend-vlc - camelCaseD
7截至2014年5月,我查看了一下,Minitube似乎是一个已经停止开发的项目。新版本(收费版)是Minitube-Ubuntu,你无法免费获取它。显然,软件中心上的许多评论显示旧版的Minitube会崩溃。 - itsols


这个很棒的Chrome扩展似乎已经被删除了。你知道还有什么方法可以实现这个功能吗?提前谢谢。 - Geppettvs D'Constanzo
抱歉,YouTube不允许使用这种类型的扩展程序。但是我找到了一个解决办法。我会在一分钟内更新我的答案。 - Jeggy
谢谢@Jeggy,你的建议太棒了。我已经成功在我的Ubuntu系统上安装了这个扩展,并且它在运行中非常好。然而,在尝试使用你的方法在基于Windows的系统上安装相同的扩展时,我发现一个重大问题,即扩展程序安装了但无法工作。如果您能提供一些关于这个问题的经验,我将不胜感激。提前致谢。 - Geppettvs D'Constanzo
它在Windows上应该能很好地工作:/ 很抱歉,我目前身边没有任何Windows设备:( 但我会很快尝试在Windows上使用它,如果成功了可能会更新这个信息。 - Jeggy
这个不再起作用了。 - xan
不好意思,我不知道你是否感兴趣,但我已经更新了我的答案,提供了一些替代方案,比如第三方网站和一个非常好的软件链接。 - Jeggy
3很好,4kdownload是不错的。愿原力与你同在! - jherax
我发现https://askubuntu.com/a/390423/592207是最简单的答案,因为你可能已经安装了VLC,而且只需要两个快捷键和复制粘贴就可以了。 当第三方解决方法不起作用时,这对我有效(带有链接访问的视频)。 - zanshin13

我认为最简单的方法从YouTube下载视频,而不需要安装任何插件或软件,就是使用savefrom.net。这里有一个视频演示如何使用ss技巧下载YouTube视频。假设你想要下载下面的视频:

Video to download

在您的地址栏中,在 youtube.com 前面添加 ss 并按下 Enter ↵

How to

现在您将被重定向到savefrom.net。在右侧,点击您想要下载的视频质量。

Download


3该网站仅允许下载200MB以下的文件。 - user47206
Savefrom 让我的一天变得美好 :-) 所有其他的替代方案都被某种方式阻止了,谢谢你的分享。 - asoundmove
@asoundmove 欢迎您。 - efthialex
2实际上,我现在只用了不到2分钟就下载了一个700多MB的H.264视频。 - Vojta Rylko

首先,如果你没有wget,请安装它:
sudo apt-get install wget

使用这个Perl脚本:
#!/usr/bin/perl -T

use strict;
use warnings;

#
##  Calomel.org  ,:,  Download YouTube videos and music using wget
##    Script Name : youtube_wget_video.pl
##    Version     : 0.38
##    Valid from  : March 2014
##    URL Page    : https://calomel.org/youtube_wget.html
##    OS Support  : Linux, Mac OSX, OpenBSD, FreeBSD or any system with perl
#                `:`
## Two arguments
##    $1 YouTube URL from the browser
##    $2 Prefix to the file name of the video (optional)
#

############  options  ##########################################

# Option: what file type do you want to download? The string is used to search
# in the YouTube URL so you can choose mp4, webm, avi or flv. mp4 seems to
# work on the most players like Android, iPod, iPad, iPhones, VLC media player
# and MPlayer.
my $fileType = "mp4";

# Option: what visual resolution or quality do you want to download? List
# multiple values just in case the highest quality video is not available, the
# script will look for the next resolution. You can choose "highres" for 4k,
# "hd1080" for 1080p, "hd720" for 720p, "itag=18" which means standard
# definition 640x380 and "itag=17" which is mobile resolution 144p (176x144).
# The script will always prefer to download the highest resolution video format
# from the list if available.
my $resolution = "hd720,itag=18";

# Option: How many times should the script retry the download if wget fails for
# any reason? Do not make this too high as a reoccurring error will just hit
# YouTube over and over again.
my $retryTimes = 5;

# Option: do you want the resolution of the video in the file name? zero(0) is
# no and one(1) is yes. This option simply puts "_hd1080.mp4" or similar at the
# end of the file name.
my $resolutionFilename = 0;

# Option: turn on DEBUG mode. Use this to reverse engineering this code if you are
# making changes or you are building your own YouTube download script.
my $DEBUG=0;

#################################################################

## Initialize retry loop and resolution variables
$ENV{PATH} = "/bin:/usr/bin:/usr/local/bin";
my $prefix = "";
my $retry = 1;
my $retryCounter = 0;
my $resFile = "unknown";
my $user_url = "";
my $user_prefix = "";

## Collect the URL from the command line argument
chomp($user_url = $ARGV[0]);
my $url = "$1" if ($user_url =~ m/^([a-zA-Z0-9\_\-\&\?\=\:\.\/]+)$/ or die "\nError: Illegal characters in YouTube URL\n\n" );

## Declare the user defined file name prefix if specified
if (defined($ARGV[1])) {
    chomp($user_prefix = $ARGV[1]);
    $prefix = "$1" if ($user_prefix =~ m/^([a-zA-Z0-9\_\-\.\ ]+)$/ or die "\nError: Illegal characters in filename prefix\n\n" );
}

## Retry getting the video if the script fails for any reason
while ( $retry != 0 && $retryCounter < $retryTimes ) {

    ## Download the html code from the YouTube page suppling the page title and the
    ## video URL. The page title will be used for the local video file name and the
    ## URL will be sanitized and passed to wget for the download.
    my $html = `wget -4Ncq -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";

    ## Format the title of the page to use as the file name
    my ($title) = $html =~ m/<title>(.+)<\/title>/si;
    $title =~ s/[^\w\d]+/_/g or die "\nError: we could not find the title of the HTML page. Check the URL.\n\n";
    $title =~ s/_youtube//ig;
    $title =~ s/^_//ig;
    $title = lc ($title);
    $title =~ s/_amp//ig;

    ## Collect the URL of the video from the HTML page
    my ($download) = $html =~ /"url_encoded_fmt_stream_map"(.*)/ig;

    # Print all of the separated strings in the HTML page
    print "\n$download\n\n" if ($DEBUG == 1);

    # This is where we look through the HTMl code and select the file type and
    # video quality.
    my @urls = split(',', $download);
    OUTERLOOP:
    foreach my $val (@urls) {
    #   print "\n$val\n\n";

        if ($val =~ /$fileType/) {
            my @res = split(',', $resolution);
            foreach my $ress (@res) {
                if ($val =~ /$ress/) {
                    print "\n\nGOOD\n\n" if ($DEBUG == 1);
                    print "$val\n" if ($DEBUG == 1);
                    $resFile = $ress;
                    $resFile = "sd640" if ( $ress =~ /itag=18/ );
                    $resFile = "mobil176" if ( $ress =~ /itag=17/ );
                    $download = $val;
                    last OUTERLOOP;
                }
            }
        }
    }

    ## 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;

    ## Clean up the URL
    my ($youtubeurl) = $download =~ /(http?:.+)/;

    ## URL title additon
    my ($titleurl) = $html =~ m/<title>(.+)<\/title>/si;
    $titleurl =~ s/ - YouTube//ig;
    $titleurl =~ s/ /%20/ig;

    ## Combine the YouTube URL and title string
    $download = "$youtubeurl\&title=$titleurl";

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

    ## Combine file variables into the full file name
    my $filename = "unknown";
    if ($resolutionFilename == 1) {
        $filename = "$prefix$title\_$resFile.$fileType";
    }
    else {
        $filename = "$prefix$title.$fileType";
    }

    ## Process check: Are we currently downloading this exact same video? Two of the
    ## same wget processes will overwrite themselves and corrupt the video.
    my $running = `ps auwww | grep [w]get | grep -c "$filename"`;
    print "\nNumber of the same wgets running: $running\n" if ($DEBUG == 1);
    if ($running >= 1)
    {
        print "\nAlready $running process, exiting." if ($DEBUG == 1);
        exit 0;
    };

    ## Print the long, sanitized YouTube URL for testing and debugging
    print "\n$download\n" if ($DEBUG == 1);

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

    ## Background the script. Use "ps" if you need to look for the process
    ## running or use "ls -al" to look at the file size and date.
    fork and exit;

    ## Download the video
    system("wget", "-4Ncq", "-e", "convert-links=off", "--load-cookies", "/dev/null", "--tries=10", "--timeout=20", "--no-check-certificate", "$download", "-O", "$filename");

    ## Print the error code of wget
    print "     error code: $?\n" if ($DEBUG == 1);

    ## Exit Status: Check if the file exists and we received the correct error code
    ## from system call. If the download experienced any problems the script will run again and try
    ## continue the download till the file is downloaded.

    if ($? == 0 && -e "$filename" && ! -z "$filename")
    {
        print " Finished: $filename\n\n" if ($DEBUG == 1);
        $retry = 0;
    }
    else
    {
        print STDERR "\n FAILED: $filename\n\n" if ($DEBUG == 1);
        $retry = 1;
        $retryCounter++;
        sleep $retryCounter;
    }

} # while

#### EOF #####

将此脚本复制到一个 .txt 文件中,并将其重命名为 youtube_wget.pl
使用以下命令使其可执行:
chmod 755 youtube_wget.pl

当你想在终端下载时,请输入以下命令:
./youtube_wget.pl http://www.youtube.com/watch?v=ejkm5uGoxs4

将YouTube的URL更改为您的YouTube视频URL。

保存位置:视频将保存在当前目录中。

来源:YouTube下载器Wget Perl脚本


5我相信OP已经安装了wget,因为他询问了如何以特定的方式使用它。 - Radu Rădeanu
考虑到这是一个仅调用wget的脚本(并向用户公开完全不同的接口),我会选择 youtube-dl答案youtube-dl更加强大。 - Blacklight Shining
你可以通过执行命令ps -aux | grep wget来检查下载是否完成。 - suhailvs
在我的机器上运行得很好(14.04) - Garrett
上班去不了 - max pleaner

cclive

安装cclive

使用这个易于使用的命令行工具,我们可以从YouTube或其他流媒体网站下载免费内容。

cclive主要是一个“视频下载工具”,但它也可以与视频播放器软件(如“mplayer”)一起使用,用于观看流媒体视频,而不是Adobe Flash播放器。

要从给定位置下载视频(在YouTube上按下“分享”按钮以获取视频的URL)。复制此链接,并在终端中使用以下命令执行clive:

cclive -s best http://youtu.be/D6z6hn6wZlg

还有许多其他选项可控制下载行为,包括选择下载格式的选项,所有这些选项都在 manpage 中有详细说明。我们还可以从文件中排队一系列的 URL。

(将 -f 更改为 -s;-f 已被弃用)。

仅适用于 Ubuntu 18.10 及更早版本。


对于Firefox用户:

由于我认为我的答案在这里更加合适,所以我将在这里提供便利。

你可以使用flashgot,它是Firefox的一个插件,我发现它非常有用,可以从YouTube和其他网站下载视频。(然而,你仍然需要在浏览器中安装Flash插件,以使视频能够被flashgot检测到。)

  • Mozilla官方插件站点安装flashgot并重启Firefox。确保插件已启用;通过菜单 工具扩展插件 进行检查。在扩展选项卡中的 flashgot 条目中,您还可以设置一些首选项。

  • 导航到您的YouTube视频,并在YouTube视频设置中设置质量,然后开始播放视频几秒钟,然后暂停。

  • 在YouTube页面中右键单击,选择flashgot media;可能会有多个视频;要么同时下载两者,要么选择较大的文件尺寸(这将是更高质量的视频)。您可以使用浏览器的下载管理器或通过flashgot options设置另一个程序。

Enter image description here


特别强大的下载管理器,尤其是使用Axel来以最快的速度下载(http://askubuntu.com/a/180370/47206)。 - user47206
我使用它与DownThemAll下载管理器一起。它甚至可以嗅探到一些较难找到的流。 - Joe
Flashgot和DownThemAll都被Firefox Quantum(57)破坏了。它们目前仍然在Firefox SE(52)中可用。 - Joe

我想提供一种使用VLC媒体播放器下载YouTube视频的简单四步方法:
  1. 在VLC媒体播放器中以网络流媒体的形式打开视频链接(Ctrl + N或点击媒体>打开网络串流...)
  2. 选择媒体信息并复制位置(Ctrl + I或右键点击视频区域>工具>媒体信息)
  3. 将其粘贴到浏览器的地址栏中
  4. 保存。

不再使用浏览器来保存文件,我现在使用uGet来进行下载,并且能够恢复下载进度。非常感谢!:) - Damodaran
我喜欢这个解决方案 - 8oh8
1我不明白为什么这个没有得到很多赞!这是我在所有方法中找到的最直接的方法。无需下载任何外部软件,几乎每个人都使用VLC。向你致敬.. :) - Ali_Waris
有没有选择媒体质量的选项? - suhailvs
这在Ubuntu 18.04上有效。"superpctricks"的链接现在已经失效了,但是这4个步骤还是可以正常工作的。谢谢hmayag! - Charles
仍然适用于Ubuntu 18 :) - zanshin13

4k下载器!除了从URL下载视频之外,它还可以下载整个播放列表,并在需要时将其转换为MP3或其他格式。

http://www.4kdownload.com/

我用它很久了,它简单易用,而且还有适用于Ubuntu的图形用户界面(GUI)。

Enter image description here


第一次听说这个。太棒了! - Hrvoje T
非常感谢,我之前不知道有这个选项...这是最好的选择。 - waqar