pytube——下載YouTube視頻的python庫

pytube的翻譯文檔


描述


Youtube是世界上非常流行的視頻分享平臺,並且作爲一個極客,你可能會遇到這樣一種情形,就是你想去寫點東西下載視頻。鑑於此情況,我推薦pytube給你。
pytube是用python寫的輕量級庫,它沒有第三方的依賴並且目的是爲了變得高度可靠。
pytube沒有假定,意思是沒有內建的方法獲得‘最’高質量的視頻。pytube僅僅給出了所有可以獲得的形式和方法,給開發者自己定義所有的’最高‘是什麼。
pytube使管程簡單,允許你能給清楚的知道不同下載事件的堆棧函數調用,比如on progress 或者or complete
最後,pytube也包括一種命令行的實現,允許你快速從終端下載視頻。

安裝


下載使用pip通過pypi
pip install pytube

庫的用法


from pytube import YouTube
# not necessary, just for demo purposes.//不是很必要,只是爲了演示的目的
from pprint import pprint
yt = YouTube("http://www.youtube.com/watch?v=Ik-RsDGPI5Y")

# Once set, you can see all the codec and quality options YouTube has made
# available for the perticular video by printing videos.
# 一旦選定,你可以看見youtube提供的所有編碼和質量選項通過打印視頻的方法
print(yt.get_videos())

# [<Video: MPEG-4 Visual (.3gp) - 144p>,
#  <Video: MPEG-4 Visual (.3gp) - 240p>,
#  <Video: Sorenson H.263 (.flv) - 240p>,
#  <Video: H.264 (.flv) - 360p>,
#  <Video: H.264 (.flv) - 480p>,
#  <Video: H.264 (.mp4) - 360p>,
#  <Video: H.264 (.mp4) - 720p>,
#  <Video: VP8 (.webm) - 360p>,
#  <Video: VP8 (.webm) - 480p>]

# The filename is automatically generated based on the video title.  You
# can override this by manually setting the filename.
# 文件名自動根據視頻名生成,你可以覆蓋它通過手動設置文件名

# view the auto generated filename:   查看自動生成的文件名
print(yt.filename)

# Pulp Fiction - Dancing Scene [HD]

# set the filename: 設置文件名的方法
yt.set_filename('Dancing Scene from Pulp Fiction')

# You can also filter the criteria by filetype.  你可以通過過濾器過濾指定的類型
print(yt.filter('flv'))

# [<Video: Sorenson H.263 (.flv) - 240p>,
#  <Video: H.264 (.flv) - 360p>,
#  <Video: H.264 (.flv) - 480p>]

# Notice that the list is ordered by lowest resolution to highest. If you
# wanted the highest resolution available for a specific file type, you
# can simply do:(注意:列表是從最低到最高的質量排序,如果你想要更高的特定文件類型的分辨率,你可以僅通過:
print(yt.filter('mp4')[-1])
# <Video: H.264 (.mp4) - 720p>

# You can also get all videos for a given resolution  (你也可以得到所有的視頻通過給一個分辨率)
print(yt.filter(resolution='480p'))

# [<Video: H.264 (.flv) - 480p>,
#  <Video: VP8 (.webm) - 480p>]

# To select a video by a specific resolution and filetype you can use the get
# method.(爲了挑選一個特定分辨率和文件類型,你可以使用get方法)

video = yt.get('mp4', '720p')

# NOTE: get() can only be used if and only if one object matches your criteria.(get()能夠被使用的前提必須是一個對象符合標準)
# for example:(例如:)

print(yt.videos)

#[<Video: MPEG-4 Visual (.3gp) - 144p>,
# <Video: MPEG-4 Visual (.3gp) - 240p>,
# <Video: Sorenson H.263 (.flv) - 240p>,
# <Video: H.264 (.flv) - 360p>,
# <Video: H.264 (.flv) - 480p>,
# <Video: H.264 (.mp4) - 360p>,
# <Video: H.264 (.mp4) - 720p>,
# <Video: VP8 (.webm) - 360p>,
# <Video: VP8 (.webm) - 480p>]

# Since we have two H.264 (.mp4) available to us... now if we try to call get()
# on mp4...(既然我們有2個H.264(.mp4)可獲得,我們可以嘗試使用get)

video = yt.get('mp4')
# MultipleObjectsReturned: 2 videos met criteria.  (多個對象符合標準)

# In this case, we'll need to specify both the codec (mp4) and resolution
# (either 360p or 720p).(在這種情況下,我們需要明確編碼和分辨率)

# Okay, let's download it! (a destination directory is required) //行,那麼我們下載它(需要一個目的目錄)
video.download('/tmp/')

命令行使用方法

你可以下載一個視頻通過簡單的傳遞-e參數或者--extension=轉換並且設置它爲你希望的文件類型
$ pytube -e mp4 http://www.youtube.com/watch?v=Ik-RsDGPI5Y
同樣的:
$ pytube -r 720p http://www.youtube.com/watch?v=Ik-RsDGPI5Y
當運行沒有分辨率或者擴展,它會給出一個所有可以下載的列表

$ pytube http://www.youtube.com/watch?v=Ik-RsDGPI5Y
  Resolution      Extension
 ----------------------------
 0  3gp             144p
 1  3gp             240p
 2  mp4             360p
 3  mp4             720p
 4  webm            360p
 Enter choice:

你可以看見可得到的一系列的可獲得的格式通過傳遞-s或者--show-available標誌
$ pytube -s http://www.youtube.com/watch?v=Ik-RsDGPI5Y
你也可以明確下載的文件路徑通過(-p或者--path=)
$ pytube -e mp4 -p ~/Downloads/ http://www.youtube.com/watch?v=Ik-RsDGPI5Y
或者可以選擇文件名(-f或者--filename=):
$ pytube -e mp4 -f "Dancing Scene from Pulp Fiction" http://www.youtube.com/watch?v=Ik-RsDGPI5Y
你可以可以通過明確分辨率或者期望的類型
$ pytube -e mp4 -r 720p http://www.youtube.com/watch?v=Ik-RsDGPI5Y


注意:

本文是本人翻譯github上的Readme文檔,時間是2017年5月22日,可能以後作者會有修改。原文地址如下:https://github.com/nficano/pytube.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章