📜  Pytube |用于下载 YouTube 视频的Python库

📅  最后修改于: 2021-10-21 06:05:04             🧑  作者: Mango

YouTube 是非常流行的视频分享网站。从 YouTube 下载视频是一项艰巨的工作。下载下载器并使用它获取视频,或者访问任何其他获取视频并保存在您计算机上的网站。使用Python,这项任务非常简单。几行代码即可为您从 YouTube 下载视频。为此,有一个名为“pytube”的Python库。 pytube 是一个轻量级、无依赖的Python库,用于从网络下载视频。
pytube 不是本地库。您需要先安装它,然后才能使用它。当你有 pip 时,安装很容易。在终端或命令提示符中,键入以下命令以安装 pytube。

pip install pytube

如果您没有 pip,请将其安装为外部库。

下载单个视频

pytube 库使视频下载非常容易。通过将链接作为参数传递来创建 YouTube 模块的对象。然后,获取视频的适当扩展名和分辨率。您可以根据自己的方便设置文件的名称,在另一种情况下,将保留原始名称。之后,使用下载函数下载文件,该函数有一个参数,即下载文件的位置。

# importing the module 
from pytube import YouTube 
  
# where to save 
SAVE_PATH = "E:/" #to_do 
  
# link of the video to be downloaded 
link="https://www.youtube.com/watch?v=xWOoBJUqlbI"
  
try: 
    # object creation using YouTube
    # which was imported in the beginning 
    yt = YouTube(link) 
except: 
    print("Connection Error") #to handle exception 
  
# filters out all the files with "mp4" extension 
mp4files = yt.filter('mp4') 
  
#to set the name of the file
yt.set_filename('GeeksforGeeks Video')  
  
# get the video with the extension and
# resolution passed in the get() function 
d_video = yt.get(mp4files[-1].extension,mp4files[-1].resolution) 
try: 
    # downloading the video 
    d_video.download(SAVE_PATH) 
except: 
    print("Some Error!") 
print('Task Completed!') 

下载文件需要一些时间,因为从 Web 上下载了大量数据。根据连接速度,执行程序所需的时间会有所不同。如果您希望下载文件数量,请使用下一个案例。

下载多个视频

下载多个视频的基本任务与下载单个视频相同。我们可以使用 for 循环来下载视频。

from pytube import YouTube 
  
#where to save 
SAVE_PATH = "E:/" #to_do 
  
#link of the video to be downloaded 
link=["https://www.youtube.com/watch?v=xWOoBJUqlbI", 
    "https://www.youtube.com/watch?v=xWOoBJUqlbI"
    ]
  
for i in link: 
    try: 
          
        # object creation using YouTube
        # which was imported in the beginning 
        yt = YouTube(i) 
    except: 
          
        #to handle exception 
        print("Connection Error") 
      
    #filters out all the files with "mp4" extension 
    mp4files = yt.filter('mp4') 
  
    # get the video with the extension and
    # resolution passed in the get() function 
    d_video = yt.get(mp4files[-1].extension,mp4files[-1].resolution) 
    try: 
        # downloading the video 
        d_video.download(SAVE_PATH) 
    except: 
        print("Some Error!") 
print('Task Completed!') 

在这里,我们使用了一个 for 循环来下载多个文件,如图所示。可以使用文件处理将所有链接保存在需要下载的文件中。

使用文件处理下载多个视频

使用文件处理,我们可以打开包含链接组的文件。遍历文本文件的每个链接并应用非常基本的视频下载程序在这里完成。在这里,我们有一个名为“links_file.txt”的文本文件,其中包含需要下载的所有链接。

from pytube import YouTube 
  
# where to save 
SAVE_PATH = "E:/" #to_do 
  
# link of the video to be downloaded 
# opening the file 
link=open('links_file.txt','r') 
  
for i in link: 
    try: 
          
        # object creation using YouTube
        # which was imported in the beginning 
        yt = YouTube(i) 
    except: 
          
        #to handle exception
        print("Connection Error")  
      
    #filters out all the files with "mp4" extension 
    mp4files = yt.filter('mp4') 
      
    # get the video with the extension and
    # resolution passed in the get() function 
    d_video = yt.get(mp4files[-1].extension,mp4files[-1].resolution) 
    try: 
          
        # downloading the video 
        d_video.download(SAVE_PATH) 
    except: 
        print("Some Error!") 
print('Task Completed!') 

要点:

  1. 确保您已连接到互联网以下载视频。否则会引发错误。
  2. 不要在任何循环中使用 set_filename()函数。在这种情况下,只会下载一个视频。
  3. 您可以每次使用另一个名称数组修改名称。
  4. 中间的连接中断也会引发错误,在这种情况下将不会下载视频。