使用 Pytube 从 YouTube 下载播放列表

Using Pytube to download playlist from YouTube

我想使用 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 文件已下载。当我尝试使用播放列表 link (An example) 时,只会下载第一个视频。没有错误输出。

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

您可以导入 Playlist 来实现此目的。尽管在 GitHub repo found here. The source of the script is in the repo here.

中有一个部分,但在 redoc 中没有对播放列表的引用
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

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() 函数已被弃用。

上述解决方案不再有效。这是下载 Youtube 播放列表中引用的视频的声音流的代码。使用的是 Pytube3,不是 pytube。请注意,播放列表必须为 public 才能下载成功。另外,如果你想下载完整的视频而不是只下载音轨,你必须修改 Youtube 标签常量的值。空 Playlist.videos 列表修复取自此 Whosebug post:

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)

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

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)

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

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)

虽然这似乎是一个已解决的问题,但这是我的解决方案之一,可以帮助您下​​载播放列表,专门作为 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.")

这对我有用。只需将播放列表中的网址一一下载即可:

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")
import pytube
from pytube import Playlist
playlist = Playlist('plylist link')
num = 0
for v in playlist.videos:
    print(v.watch_url)
    one = pytube.YouTube(v.watch_url)
    one_v = one.streams.get_highest_resolution()
    name = f"{0}" + one_v.default_filename
    one_v.download()
    num = num + 1

下载所有播放列表

这可以完美地下载完整的播放列表

检查 pytube github 安装

pip install pytube

现在,转到此文件夹并打开 cipher.py

D:\ProgramData\Anaconda3\Lib\site-packages\pytube\

在第 273 行替换

function_patterns = [
        r'a\.[a-zA-Z]\s*&&\s*\([a-z]\s*=\s*a\.get\("n"\)\)\s*&&\s*.*\|\|\s*(.*)\(',
        r'\([a-z]\s*=\s*([a-zA-Z0-9$]{3})(\[\d+\])?\([a-z]\)',
    ]

main.py

from pytube import Playlist
playlist = Playlist('https://www.youtube.com/playlist?list=PLwdnzlV3ogoXUifhvYB65lLJCZ74o_fAk')

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:
    video.streams.get_highest_resolution().download()