Python如何使用pytube下载在线视频?代码实现

2021年3月27日18:15:48 发表评论 1,796 次浏览

YouTube是非常受欢迎的视频共享网站。从YouTube下载视频是一项艰巨的任务。下载下载器并使用该下载器获取视频, 或者访问其他任何可获取视频并保存在计算机上的网站。使用Python, 此任务非常简单。很少有代码行可以为你从YouTube下载视频。为此, 有一个名为" pytube"的python库。 pytube是一个轻量级的, 无依赖的Python库, 用于从网络上下载视频。

pytube不是本机库。你需要先安装它, 然后才能使用它。点了点就可以轻松安装。在终端或命令提示符中, 键入以下命令以安装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=123x"
  
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' )
  
yt.set_filename( 'lsbin Video' ) #to set the name of the file
  
#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循环下载视频。

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=123x" , "https://www.youtube.com/watch?v=123x"
      ] #list of youtube links which need to be downloaded
for i in link:
     try :
         #object creation using YouTube which was imported in the beginning
         yt = YouTube(i)
     except :
         print ( "Connection Error" ) #to handle exception
      
     #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
link = open ( 'links_file.txt' , 'r' ) #opening the file
  
for i in link:
     try :
         #object creation using YouTube which was imported in the beginning
         yt = YouTube(i)
     except :
         print ( "Connection Error" ) #to handle exception
      
     #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. 两者之间的连接中断也会引发错误, 并且在这种情况下将无法下载视频。

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: