gpt4 book ai didi

python-3.x - 如何使用 youtube-dl 下载英文 YouTube 视频字幕?

转载 作者:行者123 更新时间:2023-12-05 03:55:27 24 4
gpt4 key购买 nike

我尝试使用以下 Python 3.x 代码下载字幕和视频。它只是不起作用。

这是我的代码:

from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'outtmpl': '/PATH/%(title)s'+'.mp4',
'format':' (bestvideo[width>=?1080]/bestvideo)+bestaudio/best',
'subtitle': '--write-srt --sub-lang en',
}
url = input("Enter your URL: ")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
print("Downloaded!")

最佳答案

youtube-dl 需要设置 'writesubtitles': True 才能下载字幕。此外,您应该指定 [ext=mp4] 否则程序可能会下载与 mp4 格式不兼容的 .webm 文件。下面的代码解决了这些问题:

from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'outtmpl': '/Downloads/%(title)s_%(ext)s.mp4',
'format': '(bestvideo[width>=1080][ext=mp4]/bestvideo)+bestaudio/best',
'writesubtitles': True,
'subtitle': '--write-sub --sub-lang en',
}
url = input("Enter your URL: ")
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
print("Download Successful!")

此外,确保将 ffmpeg.exe 保存在您的 youtube-dl 文件夹中,用于合并视频和音频文件。可以领取from here .

关于python-3.x - 如何使用 youtube-dl 下载英文 YouTube 视频字幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60115288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com