Thursday, June 18, 2020

Raspberry pi - Getting Started with OpenCV (Playing Youtube Video)

for getting started with OpenCV i tried to do something simple and straightforward.

Playing a video from youtube URL.

Why?

As most of the videos i am going to do image processing on is on my youtube channel.


This short tutorial requires that you have opencv, pafy and youtube_dl installed.

For those that want to see the final code looks something like the following:



So lets break down the simple script above.

  1. We create a pafy object. This object takes in the url of the youtube video and then returns a pafy object.
  2. Pafy in short is a library to donwload youtube content and get the metadata for the videos.
  3. We use the pafy object method getbest to select the best video/audio for the stream. Here i have strictly typed it to mp4. You can try out with other formats.
  4. The function retunrs a pafy stream object. This stream object gives us the videos url, dimensions, butrate, and other relevant information. That you can read here. Maybe you want to work with the dimension and make this script in some other way as shown here.
  5. Next we pass the url from the stream object of pafy to the video capture object of OpenCV. This returns a capture object.
  6. Now we have to do is iterate through the video by reading the capture object frame by frame. 
  7. The capture read function as mentioned above returns the frame that we want to display.
  8. This is the frame where all the algo we want to test will be put into code.
  9. Then we display the frame in a "frame" named container and viola we have our video.

Here is a snapshot of some random video being played.
Youtube video playback on Raspberry Pi

No comments:

Post a Comment