Capturing Youtube by OpenCV

Reference

https://www.366service.com/jp/qa/ad87eecb1989830886612272067df0eb
It needs some changes.

Environment

macOS BigSur

Visual Studio Code

Python 3.8.5 64bit

Prerequisites

$ pip install cv2
$ pip install pafy

Code for macOS

import cv2
import pafy

# Tokyo Station
url = 'https://youtu.be/6YRUWrkV2R4'
video = pafy.new(url)

streams = video.streams
for i in streams:
    print(i)

# get best resolution regardless of format
best = video.getbest()

# start the video
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()

投稿者: admin

Free Software Engineer

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です