使用 Python 下载 YouTube 视频到某个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40713268/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:53:52  来源:igfitidea点击:

Download YouTube video using Python to a certain directory

pythonpython-3.xpython-2.7

提问by Sharmili Nag

I have tried the following code to download a video in YouTube and it is working, but I want to save the video at a particular location. Now it is saving the video in C:/Users/Download. If I want to save the video in the desktop, what changes do I need in the code?

我已尝试使用以下代码在 YouTube 中下载视频并且它正在运行,但我想将视频保存在特定位置。现在它正在将视频保存在C:/Users/Download. 如果想把视频保存在桌面,代码需要做哪些修改?

from __future__ import unicode_literals
import youtube_dl
import urllib
import shutil
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=n06H7OcPd-g'])

回答by Daksh Miglani

I found out a really cool python module that allows you to download videos from youtube easily. TO install it:

我发现了一个非常酷的 python 模块,它允许您轻松地从 youtube 下载视频。安装它:

pip install pytube

Now, You can download your video like this -

现在,您可以像这样下载视频 -

from pytube import YouTube
yt = YouTube("https://www.youtube.com/watch?v=n06H7OcPd-g")
yt = yt.get('mp4', '720p')
yt.download('/path/to/download/directory')

Boom, Now you can easily scrape such videos using Python easily; Now, We Drink!

Boom,现在您可以轻松地使用 Python 轻松抓取此类视频;现在,我们喝酒!

Update 1:

更新 1:

Thanks to @Chiramisu's comment, You can use the following one-liner to download the highest quality video:

感谢@Chiramisu 的评论,您可以使用以下单行下载最高质量的视频:

YouTube('video_url').streams.first().download('save_path')

For Windows, please specify path with double backslashes ex:

对于 Windows,请使用双反斜杠指定路径,例如:

YouTube('video_url').streams.first().download('C:\Users\username\save_path')

Update 2:

更新 2:

If pytube doesn't seem to work for you, try using youtube-dl:

如果 pytube 似乎不适合您,请尝试使用 youtube-dl:

sudo -H pip install --upgrade youtube-dl

Now Download Videos:

现在下载视频:

from __future__ import unicode_literals
import youtube_dl

ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=BaW_jenozKc'])

More info on ytdl in python here.

有关 python 中 ytdl 的更多信息,请点击此处

回答by Azadeh Khojandi

downloading videos from youtube in python 3.x for the reference you can check https://python-pytube.readthedocs.io/en/latest/user/quickstart.html#downloading-a-video

在 python 3.x 中从 youtube 下载视频以供参考,您可以查看https://python-pytube.readthedocs.io/en/latest/user/quickstart.html#downloading-a-video

from pytube import YouTube
import os

def downloadYouTube(videourl, path):

    yt = YouTube(videourl)
    yt = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
    if not os.path.exists(path):
        os.makedirs(path)
    yt.download(path)

downloadYouTube('https://www.youtube.com/watch?v=zNyYDHCg06c', './videos/FindingNemo1')

回答by johan

You should put it inside ydl_opts:

你应该把它放在里面ydl_opts

ydl_opts = {
    'outtmpl': os.path.join(download_path, '%(title)s-%(id)s.%(ext)s'),
}

In your case, download_pathshould be 'C:/Users/Desktop'. Use %(title)s.%(ext)sinstead of %(title)s-%(id)s.%(ext)sif you prefer a file name without video ID.

在你的情况下,download_path应该是'C:/Users/Desktop'. 如果您更喜欢没有视频 ID 的文件名,请使用%(title)s.%(ext)s代替%(title)s-%(id)s.%(ext)s

Or you can just os.chdir(path)to change the directory to where you want the download to be before you start your download.

或者,您可以os.chdir(path)在开始下载之前将目录更改为您希望下载的位置。

from __future__ import unicode_literals
import youtube_dl
import os

ydl_opts = {}
os.chdir('C:/Users/Desktop')
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=n06H7OcPd-g'])

回答by Mostafa Omer

Path = "The Path That You Want"
Location = '%s \%(extractor)s-%(id)s-%(title)s.%(ext)s'.replace("%s ", Path)
ytdl_format_options = {
'outtmpl': Location
}

with youtube_dl.YoutubeDL(ytdl_format_options) as ydl:
     ydl.download(['https://www.youtube.com/watch?v=n06H7OcPd-g'])

I personally don't know the library very well, but here is my knowledge the youtube_dl have ytdl_format_options it gives you the options to add some I don't know what it called but let say parameters like above outtmp1 give you the option to specify the location, id, title, or quiet to see the log or not and there is so much more. almost everything you can get it from this URL:https://github.com/ytdl-org/youtube-dl/blob/master/README.md#format-selection

我个人不太了解该库,但这是我的知识 youtube_dl 有 ytdl_format_options 它为您提供了添加一些的选项我不知道它叫什么但可以说像上面的参数 outtmp1 给您指定的选项位置、ID、标题或安静看日志与否,还有更多。几乎所有你可以从这个 URL 得到它的东西:https: //github.com/ytdl-org/youtube-dl/blob/master/README.md#format-selection

回答by John Zwinck

youtube_dlhas a giant list of options: https://github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L128-L278

youtube_dl有一个巨大的选项列表:https: //github.com/rg3/youtube-dl/blob/master/youtube_dl/YoutubeDL.py#L128-L278

But I don't see any that control the output directory. So you can move the file afterward. For that, see: How to move a file in Python.

但我没有看到任何控制输出目录的东西。所以你可以在之后移动文件。为此,请参阅:如何在 Python 中移动文件

回答by Saeid

It will save the file where your .py application is in. for example, if your .py program is in your desktop folder and you run your app from the desktop, the output will save on your desktop. the only thing you need is to save your .py file in Desktop and then open a command line and go in Desktop using cd command after it run your .py file using python YOURAPP.py but if you want to download it and then save it in another place, you need to download it like you do now (in your temporary place) then moves itvia file libraries in python.

它将保存.py 应用程序所在的文件。例如,如果您的 .py 程序在您的桌面文件夹中并且您从桌面运行您的应用程序,则输出将保存在您的桌面上。您唯一需要做的就是将您的 .py 文件保存在桌面中,然后打开命令行并在使用 python YOURAPP.py 运行您的 .py 文件后使用 cd 命令进入桌面,但如果您想下载它然后保存它在另一个地方,您需要像现在一样下载它(在您的临时位置),然后通过 python 中的文件库移动它

回答by matan h

I have created a library that makes it simpler.

我创建了一个使它更简单的库。

To install:

安装:

$ pip install mhyt
# or
$ sudo pip install mhyt

And use it like:

并像这样使用它:

from mhyt import yt_download

yt_download("url","file.format",ismucic=True)

回答by Youssof H.

I will give you a hint. Make a bash script that moves the downloaded video to a specific path. You just have to pass an argument to the bash script with the name of the downloaded video.

我会给你一个提示。制作一个 bash 脚本,将下载的视频移动到特定路径。您只需要使用下载的视频的名称将参数传递给 bash 脚本。

Python script:

蟒蛇脚本:

import subprocess
vid_name = ""
subprocess.call(["/path/to/movevideo.sh",vid_name])

Bash script:

bash脚本:

Windows:

视窗:

#!/bin/sh
move path\to$1 path\to\desktop
exit 0

Linux:

Linux:

#!/bin/sh
mv path/to/ path/to/desktop
exit 0

回答by Aakash Makwana

I guess you are a bit confused, try this code, end to end

我猜你有点困惑,试试这个代码,端到端

    from __future__ import unicode_literals
    import youtube_dl
    import urllib
    import shutil
    ydl_opts = {}
    with youtube_dl.YoutubeDL(ydl_opts) as ydl:
        ydl.download(['https://www.youtube.com/watch?v=n06H7OcPd-g'])

    #Moving your source file to destination folder
    source_file = 'C:\Users\Sharmili Nag\Aahatein - Agnee (splitsvilla 4 theme song) Best audio quality-n06H7OcPd-g.mp4'
    destination_folder = 'C:\Users\Sharmili Nag\Desktop\Aahatein - Agnee (splitsvilla 4 theme song) Best audio quality-n06H7OcPd-g.mp4'
    shutil.move(source_file, destination_folder)

In case this code worked out for you, kindly mark the answer as correct.

如果此代码适合您,请将答案标记为正确。