使用 Python 和 pafy 下載 YouTube 媒體/音訊


在本文中,我們將瞭解如何使用 **pafy** 模組提取 **Youtube** 影片的詳細資訊並在不同格式中下載它們。 請訪問連結以獲取官方文件。

使用以下命令安裝 **pafy** 模組

pip install pafy

如果您執行上述命令,則在成功安裝 **pafy** 模組後,將生成以下結果。

Collecting pafy
Using cached
https://files.pythonhosted.org/packages/b0/e8/3516f761558525b00d3eaf73744eed5c267db
20650b7b660674547e3e506/pafy-0.5.4-py2.py3-none-any.whl
Installing collected packages: pafy
Successfully installed pafy-0.5.4

透過執行以下命令檢查您是否可以匯入 **pafy** 模組。

import pafy

如果您沒有發現任何錯誤,則說明已完成。 否則,安裝以下模組以解決問題。

pip install youtube-dl

如果您執行上述命令,則在成功安裝 **youtube-dl** 模組後,將生成以下結果。

Collecting youtube-dl
Using cached
https://files.pythonhosted.org/packages/b1/ec/fe552181d6bd05a9e5b6b51f6f7ea4fed9f12
1ce595d788217e59318e47c/youtube_dl-2019.7.30-py2.py3-none-any.whl
Installing collected packages: youtube-dl
Successfully installed youtube-dl-2019.7.30

Youtube 影片詳細資訊

使用連結提取影片詳細資訊的步驟。

  • 匯入 **pafy** 模組

  • 將影片連結儲存在一個變數中。

  • 呼叫 **pafy.new(url)** 方法並將結果儲存在一個變數中。

  • 使用上述變數獲取有關影片的所有資訊。

讓我們看一個例子。

## importing the module
import pafy
## url of the video
url = "https://www.youtube.com/watch?v=cr3-J5wDLsM"
## calling the new method of pafy
result = pafy.new(url)
## getting details like title, rating, viewcount, author, length, likes, etc..,
print(f"Title: {result.title}")
print(f"Viewcount {result.viewcount}")
print(f"Author: {result.author}")
print(f"Video Length: {result.length}")
print(f"Likes: {result.likes}")
print(f"Dislikes: {result.dislikes}")
print(f"Description: {result.description}")

如果您執行上述程式,您將獲得以下結果。

Title: Indexing Overview
Viewcount 862
Author: Tutorials Point (India) Pvt. Ltd.
Video Length: 167
Likes: 6
Dislikes: 1
Description: Indexing Overview
Watch more Videos at https://tutorialspoint.tw/videotutorials/index.htm
Lecture By: Mr. Arnab Chakraborty, Tutorials Point India Private Limited

下載最佳質量的影片

  • 匯入 **pafy** 模組

  • 將影片連結儲存在一個變數中。

  • 呼叫 **pafy.new(url)** 方法並將結果儲存在一個變數中。

  • 使用上述變數使用 **getbest** 方法獲取影片的最佳質量,並將其儲存在一個變數中。

  • 在前面的變數上呼叫 **download** 方法。

請參閱以下示例。

## importing the module
import pafy
## url of the video
url = "https://www.youtube.com/watch?v=cr3-J5wDLsM"
## calling the new method of pafy
result = pafy.new(url)
## getting the best quality of video from the 'result' using the getbest()
best_quality_video = result.getbest()
## you can print it to see the quality of the video
print(best_quality_video)
## download it using the download()
best_quality_video.download()

如果您執行上述程式,您將獲得以下結果。

normal:mp4@1280x720
26,638,008 Bytes [100.00%] received. Rate: [ 820 KB/s]. ETA: [0 secs]

您可以使用 **getbest()** 方法繞過 preftype(如 3gp、mp4、WebM 等)下載任何型別的影片,請參閱以下語法並自行嘗試。

## previous steps are same
best_quality_video = result.getbest(preftype = "mp4")
## next steps are same

下載最佳質量的音訊

按照我們下載影片時所做的相同步驟操作。 呼叫 **getbestaudio()** 而不是 **getbest()**,然後使用 **download()** 方法下載它。 首先,請自行嘗試。 如果您發現很難檢視以下程式碼。## 匯入模組

import pafy
## url of the video
url = "https://www.youtube.com/watch?v=cr3-J5wDLsM"
## calling the new method of pafy
result = pafy.new(url)
## getting the best quality of video from the 'result' using the getbest()
best_quality_audio = result.getbestaudio()
## you can print it to see the quality of the video
print(best_quality_audio)
## download it using the download()
best_quality_audio.download()

如果您執行上述程式,您將獲得以下結果。

audio:m4a@128k
27,518 Bytes [100.00%] received. Rate: [ 306 KB/s]. ETA: [0 secs]
'Indexing Overview.m4a'

更新於: 2020年7月2日

952 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告