使用pytube从YouTube下载音频

时间:2018-03-10 00:31:00

标签: youtube python-3.5 youtube-data-api 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")

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

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

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

1 个答案:

答案 0 :(得分:2)

升级您的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)