使用Pytube从YouTube下载播放列表

时间:2019-02-15 14:03:24

标签: python pytube

我希望使用PyTube库下载YouTube播放列表。目前,我可以一次下载一个视频。我不能一次下载多个视频。

目前,我的意思是

import pytube

link = input('Please enter a url link\n')
yt = pytube.YouTube(link)
stream = yt.streams.first()
finished = stream.download()
print('Download is complete')

这将导致以下输出

>> Download is complete

然后下载了YouTube文件。当我尝试使用播放列表链接(An example)时,只会下载第一个视频。没有错误输出。

我希望能够下载整个播放列表,而无需重新提示用户。

5 个答案:

答案 0 :(得分:6)

以上解决方案不再起作用。这是一个代码,可下载YouTube播放列表中引用的视频的声音流。使用的是Pytube3,而不是pytube。请注意,播放列表必须是公开的,下载才能成功。另外,如果您要下载完整的视频而不是仅下载音轨,则必须修改Youtube标签常量的值。空的Playlist.videos列表修复来自以下Stackoverflow帖子:PyTube3 Playlist returns empty list

import re
from pytube import Playlist

YOUTUBE_STREAM_AUDIO = '140' # modify the value to download a different stream
DOWNLOAD_DIR = 'D:\\Users\\Jean-Pierre\\Downloads'

playlist = Playlist('https://www.youtube.com/playlist?list=PLzwWSJNcZTMSW-v1x6MhHFKkwrGaEgQ-L')

# this fixes the empty playlist.videos list
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")

print(len(playlist.video_urls))

for url in playlist.video_urls:
    print(url)

# physically downloading the audio track
for video in playlist.videos:
    audioStream = video.streams.get_by_itag(YOUTUBE_STREAM_AUDIO)
    audioStream.download(output_path=DOWNLOAD_DIR)

答案 1 :(得分:4)

from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PL6gx4Cwl9DGCkg2uj3PxUWhMDuTw3VKjM')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for video_url in playlist.video_urls:
    print(video_url)
playlist.download_all()
https://www.youtube.com/watch?v=HjuHHI60s44
https://www.youtube.com/watch?v=Z40N7b9NHTE
https://www.youtube.com/watch?v=FvziRqkLrEU
https://www.youtube.com/watch?v=XN2-87haa8k
https://www.youtube.com/watch?v=VgI4UKyL0Lc
https://www.youtube.com/watch?v=BvPIgm2SMG8
https://www.youtube.com/watch?v=DpdmUmglPBA
https://www.youtube.com/watch?v=BmVmJi5dR9c
https://www.youtube.com/watch?v=pYNuKXjcriM
https://www.youtube.com/watch?v=EWONqLqSxYc
https://www.youtube.com/watch?v=EKmLXiA4zaQ
https://www.youtube.com/watch?v=-DHCm9AlXvo
https://www.youtube.com/watch?v=7cRaGaIZQlo
https://www.youtube.com/watch?v=ZkcEB96iMFk
https://www.youtube.com/watch?v=5Fcf-8LPvws
https://www.youtube.com/watch?v=xWLgdSgsBFo
https://www.youtube.com/watch?v=QcKYFEgfV-I
https://www.youtube.com/watch?v=BtSQIxDPnLc
https://www.youtube.com/watch?v=O5kh_-6e4kk
https://www.youtube.com/watch?v=RuWVDz-48-o
https://www.youtube.com/watch?v=-yjc5Y7Wbmw
https://www.youtube.com/watch?v=C5T59WsrNCU
https://www.youtube.com/watch?v=MWldNGdX9zE

我正在使用pytube3 9.6.4,不是pytube

现在Playlist.video_urls运行良好。

不推荐使用Playlist.populate_video_urls()函数。

答案 2 :(得分:2)

您可以导入Playlist以实现此目的。尽管GitHub repo found here中有一个部分,但重做中没有引用播放列表。脚本的源代码位于仓库here中。

from pytube import Playlist

playlist = Playlist('https://www.youtube.com/watch?v=58PpYacL-VQ&list=UUd6MoB9NC6uYN2grvUNT-Zg')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
playlist.download_all()

注意:我发现支持方法Playlist.video_urls不起作用。视频仍被下载,但是as evidenced here

答案 3 :(得分:2)

如果需要下载播放列表中每个项目的最高质量视频

playlist = Playlist('https://www.youtube.com/watch?v=VZclsCzhzt4&list=PLk-w4cD8sJ6N6ffzp5A4PQaD76RvdpHLP')

for video in playlist.videos:
    print('downloading : {} with url : {}'.format(video.title, video.watch_url))
    video.streams.\
        filter(type='video', progressive=True, file_extension='mp4').\
        order_by('resolution').\
        desc().\
        first().\
        download(cur_dir)

答案 4 :(得分:2)

此代码允许您将播放列表下载到指定的文件夹

import re
from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=Pd5k1hvD2apA0DwI3XMiSDqp')   
DOWNLOAD_DIR = 'D:\Video'
playlist._video_regex = re.compile(r"\"url\":\"(/watch\?v=[\w-]*)")    
print(len(playlist.video_urls))    
for url in playlist.video_urls:
    print(url)    
for video in playlist.videos:
    print('downloading : {} with url : {}'.format(video.title, video.watch_url))
    video.streams.\
        filter(type='video', progressive=True, file_extension='mp4').\
        order_by('resolution').\
        desc().\
        first().\
        download(DOWNLOAD_DIR)