13
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

YouTube動画をOpenCVでキャプチャするスクリプト

Posted at

はじめに

タイトルそのまま、YouTube動画をOpenCVでキャプチャするスクリプト
YouTubeライブ配信でも動作しました

環境

Windows10で実行しました。

PS C:\> python -V
Python 3.9.5
PS C:\> pip list | Select-String -Pattern "pafy"

pafy                  0.5.5

PS C:\> pip list | Select-String -Pattern "youtube-dl"

youtube-dl            2021.6.6

以下2つのモジュールをインストールします。

  • pafy
    • pip install pafy
  • youtube_dl
    • sudo pip install --upgrade youtube_dl

スクリプト

変数 url にYoutube動画のURLを設定し実行します。

YouTube動画をキャプチャするスクリプト
import cv2
import pafy

# キャプチャするYouTube動画のURLを設定します
url = "https://youtu.be/PaCyCcWseO4"

video = pafy.new(url)
best = video.getbest(preftype="mp4")
cap = cv2.VideoCapture(best.url)

while (True):
    ret,frame = cap.read()
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break    

cap.release()
cv2.destroyAllWindows()
13
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?