8

我正在寻找使用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 文件已下载。当我使用播放列表链接(示例)尝试此操作时,仅下载第一个视频。没有错误输出。

我希望能够在不重新提示用户的情况下下载整个播放列表。

4

7 回答 7

15

您可以导入Playlist来实现这一点。尽管在GitHub repo中有一个部分,但 redoc 中没有提到 Playlist 。脚本的来源在 repo 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不起作用。然而,视频仍在下载,如此处所示

于 2019-02-15T16:27:21.520 回答
14

上述解决方案不再有效。这是一个下载 Youtube 播放列表中引用的视频的声音流的代码。使用的是 Pytube3,而不是 pytube。请注意,播放列表必须是公开的,下载才能成功。此外,如果您想下载完整视频而不是仅下载音轨,则必须修改 Youtube 标签常量的值。空的 Playlist.videos 列表修复取自 Stackoverflow 帖子:PyTube3 播放列表返回空列表

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)
于 2020-08-03T15:36:57.007 回答
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()功能已被弃用。

于 2020-05-05T12:50:00.963 回答
3

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

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)
于 2021-05-04T16:18:46.073 回答
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)
于 2021-04-12T21:19:06.337 回答
0

虽然这似乎是一个已解决的问题,但这是我的解决方案之一,它可以帮助您将播放列表专门下载为视频1080P 30 FPS720P 30 FPS (如果 1080P 不可用)

from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PLeo1K3hjS3uvCeTYTeyfe0-rN5r8zn9rw')
print('Number of videos in playlist: %s' % len(playlist.video_urls))

# Loop through all videos in the playlist and download them
for video in playlist.videos:
    try:
        print(video.streams.filter(file_extension='mp4'))
        stream = video.streams.get_by_itag(137) # 137 = 1080P30
        stream.download()
    except AttributeError:
        stream = video.streams.get_by_itag(22) # 22, 136 = 720P30; if 22 still don't work, try 136
        stream.download()
    except:
        print("Something went wrong.")
于 2021-08-19T15:37:15.367 回答
0

这对我有用。只需获取播放列表中的 URL 并一一下载:

from pytube import Playlist

playlist = Playlist('URL')
print('Number of videos in playlist: %s' % len(playlist.video_urls))
for video_url in playlist.video_urls:
    print(video_url)
    urls.append(video_url)
    for url in urls:
        my_video = YouTube(url)

        print("*****************DOWNLOAD VID*************")
        print(my_video.title)

        my_video = my_video.streams.get_highest_resolution()
        path = "PATH"
        my_video.download(path)
        print("VIDEO DOWNLOAD DONNNNE")
于 2021-12-12T19:30:19.563 回答