通过Python脚本下载YouTube视频

3 投票
3 回答
6804 浏览
提问于 2025-04-16 18:33

我正在尝试用 这个脚本 用Python下载YouTube视频。

目前我这样使用:

youtube-dl "http://www.youtube.com/watch?v=dvsdgyuv"

在文档中,他们写了我可以使用这些:

id: The sequence will be replaced by the video identifier.
url: The sequence will be replaced by the video URL.
uploader: The sequence will be replaced by the nickname of the person who uploaded the video.
upload_date: The sequence will be replaced by the upload date in YYYYMMDD format.
title: The sequence will be replaced by the literal video title.
stitle: The sequence will be replaced by a simplified video title, restricted to alphanumeric characters and dashes.
ext: The sequence will be replaced by the appropriate extension (like flv or mp4).
epoch: The sequence will be replaced by the Unix epoch when creating the file.
autonumber: The sequence will be replaced by a five-digit number that will be increased with each download, starting at zero.`enter code here`

但是他们没有说明我该怎么用。

我想知道怎么才能让下载的视频文件名和视频标题一样。

他们告诉我使用这个,但我不知道怎么在命令行上使用它。

%(title)s-%(id)s.%(ext)s.

3 个回答

0

使用一个Python脚本:

import youtube_dl

youtube_playlist_url = 'youtube_playlist_url'
destination_folder = './youtube_playlist/'

ydl_opts = {'outtmpl': destination_folder+'/%(title)s.%(ext)s'}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
    ydl.download([youtube_playlist_url])

来源:https://www.youtube.com/watch?v=Mn0zj8Ql7Fs

4

你最好的选择是 http://np1.github.io/pafy/。这个东西真不错,百分之百推荐!

3

在文档的底部,有一段说明:

-o 选项让用户可以指定输出文件名的模板。基本用法是在下载单个文件时,不需要设置任何模板参数,比如你可以这样写:youtube-dl -o funny_video.flv "http://some/video"。不过,这个模板可以包含一些特殊的序列,这些序列在下载每个视频时会被替换掉。这些特殊序列的格式是 %(NAME)s。简单来说,就是一个百分号后面跟着一个括号里的名字,最后是一个小写的s。允许的名字有:...

你可以这样运行命令来使用这些特殊的输出参数:

youtube-dl "http://www.youtube.com/watch?v=dvsdgyuv" -o "%(title)s-%(id)s.%(ext)s."

撰写回答