Read youtube video with pafy and opencv

Ulf Hamster 2 min.
python opencv pafy

Load Packages

%%capture
!pip install pafy youtube_dl opencv-python 
import pafy
import cv2
from google.colab.patches import cv2_imshow

Extract Meta Data from Youtube

url = 'https://www.youtube.com/watch?v=rs3axbj7XjM'
video = pafy.new(url)

General information about the video

print(f"Title: {video.title}")
print(f"Channel: {video.author}")
print(f"Category: {video.category}")
print(f"Published: {video.published}")
print(f"Views: {video.viewcount}")
print(f"Rating: {video.rating}")
print("Like/Dislike ratio: {:5.2f}%  (Likes: {:d} vs Dislikes {:d})".format(
    (video.likes * 100 / (video.likes + video.dislikes)), video.likes, video.dislikes))
print(f"Length: {video.duration} ({video.length} sec)")
Title: The story behind Philippe Coutinho's magnificent goals vs. Werder Bremen | FC Bayern
Channel: FC Bayern München
Category: Sports
Published: 2019-12-17 16:02:58
Views: 71557
Rating: 4.9657283
Like/Dislike ratio: 99.14%  (Likes: 4050 vs Dislikes 35)
Length: 00:02:53 (173 sec)

Check available media data

What video streams are available? What's their MIME type and resolution?

for s in video.streams:
    print(s)
    
best = video.getbest()
print(f"\nHighest resolution: {best.resolution} ({best.extension})")
normal:webm@640x360
normal:mp4@640x360
normal:mp4@1280x720

Highest resolution: 1280x720 (mp4)

What audio quality is available? What's the MIME type? what's the bit rate?

for a in video.audiostreams:
    print(a)

best = video.getbestaudio()
print(f"\nHighest bit rate: {best.bitrate} ({best.extension})")
audio:webm@50k
audio:webm@70k
audio:m4a@128k
audio:webm@160k

Highest bit rate: 160k (webm)

All combination of streams

for s in video.allstreams:
    msg = "{:^7s} {:^5s} {:^10s}". format(
        s.mediatype, s.extension, s.quality)
    print(msg)
 audio  webm     50k    
 audio  webm     70k    
 audio   m4a     128k   
 audio  webm     160k   
 video  webm   256x144  
 video   mp4   256x144  
 video  webm   426x240  
 video   mp4   426x240  
 video  webm   640x360  
 video   mp4   640x360  
 video  webm   854x480  
 video   mp4   854x480  
 video  webm   1280x720 
 video   mp4   1280x720 
 video  webm  1920x1080 
 video   mp4  1920x1080 
normal  webm   640x360  
normal   mp4   640x360  
normal   mp4   1280x720 

Video Frame by Frame

Pick a specific video stream from Pafy.streams

s = video.streams[1]
print(type(s))
print(s)
<class 'pafy.backend_youtube_dl.YtdlStream'>
normal:mp4@640x360

It's a Stream object with a download URL

print(s.url)
https://r1---sn-5hne6nsk.googlevideo.com/videoplayback?expire=1576681763&ei=w-z5XbCcMMKS8gOq_YuAAg&ip=35.204.149.183&id=o-AEjonf6XxTeZ9-pJIqruhV414wDrjhEBayHjPgmve6U9&itag=18&source=youtube&requiressl=yes&mm=31%2C26&mn=sn-5hne6nsk%2Csn-5goeen7r&ms=au%2Conr&mv=m&mvi=0&pl=20&initcwndbps=23446250&mime=video%2Fmp4&gir=yes&clen=12955056&ratebypass=yes&dur=172.779&lmt=1576605745597234&mt=1576660068&fvip=1&fexp=23842630&c=WEB&txp=5431432&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cmime%2Cgir%2Cclen%2Cratebypass%2Cdur%2Clmt&sig=ALgxI2wwRQIgUvgx8kBrX9sWtfKcwX5udvrOx3ZqDTtnF5NIN5OP5IgCIQDH1FQgUO8_Mdb025wTruJsS9hk1l59R-30vVfR4B3rYA%3D%3D&lsparams=mm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AHylml4wRQIhAM1YXfAJEddP4KbLdA7hdKzS0nhlZLRaXKGIkWadVfwXAiA0kGji3jCiKaIx6v86gxwe-qYcGs7bK4QRtSAoVvVrCw%3D%3D
#start the video
cap = cv2.VideoCapture(s.url)
i = 0
cnt = 1
while (True):
    ret, frame = cap.read()
    # display every 20th frame
    if (i % 10) == 0 and (i > 25*91+18):
        # cv2.imshow('frame', frame)
        cv2_imshow(frame)
        cnt = cnt + 1
    # abort after 5 frames
    if cnt > 5:
        break
    # frame counter
    i = i + 1
    

png

png

png

png

png

Links