使用 pytube 从 YouTube 下载音频

oma*_*ich 5 youtube youtube-data-api python-3.5 pytube

我正在使用 pytube 和 Python 3.5 从 YouTube 下载视频,但我需要将视频转换为带有 .avi 扩展名的音频。

这是我目前正在使用的代码:

from pytube import YouTube

yt = YouTube(str(input("Enter the video link: ")))
videos = yt.get_videos()

s = 1
for v in videos:
    print(str(s)+". "+str(v))
    s += 1

n = int(input("Enter the number of the video: "))
vid = videos[n-1]

destination = str(input("Enter the destination: "))
vid.download(destination)

print(yt.filename+"\nHas been successfully downloaded")
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我可以下载视频。

我的问题是:如何直接下载 YouTube 上扩展名为 的视频的音频.avi

YouTube 数据 API 可以帮助我专门下载音频吗?

小智 16

升级您的 pytube,因为 YouTube().get_videos() 似乎已被弃用。关于下载视频:您可以使用类似YouTube().streams.filter(only_audio=True).all(). 打印该功能可以引导您找到要下载的音频类型。为了给它 .avi 格式,我认为你可以使用任何转换器。我正在使用 python 3.6。

from pytube import YouTube
yt=YouTube(link)
t=yt.streams.filter(only_audio=True).all()
t[0].download(/path)
Run Code Online (Sandbox Code Playgroud)