【问题标题】:Using Pytube to download playlist from YouTube使用 Pytube 从 YouTube 下载播放列表
【发布时间】:2019-02-15 14:03:23
【问题描述】:

我希望使用 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) 时,只会下载第一个视频。没有错误输出。

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

【问题讨论】:

  • “显示错误”不是问题描述。请在您的帖子中包含完整回溯。
  • 我刚刚运行了你的代码,它运行良好。
  • 但它一次只下载一个视频,但我想一次下载整个播放列表@Glazbee
  • 我已经为你发布了答案。
  • pytube 不再维护,考虑改用 pytube3

标签: python pytube


【解决方案1】:

您可以导入Playlist 来实现这一点。尽管GitHub repo found here 中有一个部分,但重做中没有对播放列表的引用。脚本的来源在 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 不起作用。然而,视频仍在下载,as evidenced here

【讨论】:

  • playlist.video_urls 列表由populate_video_urls() 方法填充,该方法由playlist.download_all() 调用。调用 populate_video_urls() 似乎确实按预期填满了列表。
  • playlist.download_all() 现在已被弃用并且不起作用。
【解决方案2】:

上述解决方案不再有效。这是一个下载 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)

【讨论】:

  • 在一个包含 1500 多个视频的播放列表上尝试这个,我只能设法从中获得 55 个 URL...有什么建议吗?
  • @PhysicsLover999 你安装了这个确切的 repo 吗? github.com/pytube/pytube
【解决方案3】:
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() 函数已被弃用。

【讨论】:

  • 前面的两个例子对我不起作用。我收到消息:Number of videos in playlist: 0 C:\Users\...\test.py:4: DeprecationWarning: Call to deprecated function download_all (This function will be removed in the future. Please iterate through .videos). playlist.download_all('out/') C:\ProgramData\Anaconda3\lib\site-packages\pytube\contrib\playlist.py:216: DeprecationWarning: Call to deprecated function _path_num_prefix_generator (This function will be removed in the future.). prefix_gen = self._path_num_prefix_generator(reverse_numbering)
  • 我补充说,我检查了示例的 url 确实有超过 0 个视频(我尝试了其他一些播放列表 url,但没有任何效果)。我正在使用 pytube3 9.6.4
  • 我的问题的答案在这里找到:stackoverflow.com/questions/62661930/…
【解决方案4】:

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

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)

【讨论】:

  • 解决 Windows 上的路径问题:import pathlib ... DOWNLOAD_DIR = pathlib.Path("D:/DOWNLOAD/folder")
【解决方案5】:

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

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)

【讨论】:

    【解决方案6】:

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

    【讨论】:

      【解决方案7】:

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

      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")
      

      【讨论】:

        【解决方案8】:
        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
        

        下载所有播放列表

        【讨论】:

        • A code-only answer is not high quality。虽然此代码可能很有用,但您可以通过说明其工作原理、工作方式、何时应该使用以及它的局限性来改进它。请edit您的回答包括解释和相关文档的链接。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-12-12
        • 2020-11-08
        • 2018-07-03
        • 2018-09-19
        • 2020-12-15
        • 2018-08-18
        • 1970-01-01
        相关资源
        最近更新 更多