Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to stream video from https:// (e.g. YouTube) into python with OpenCV?

This link has a tidy little example of how to use python's OpenCV library, cv2 to stream data from a camera into your python shell. I'm looking to do some experiments and would like to use the following YouTube video feed: https://www.youtube.com/watch?v=oCUqsPLvYBQ.

I've tried adapting the example as follows:

import numpy as np
import cv2

cap = cv2.VideoCapture('https://www.youtube.com/watch?v=oCUqsPLvYBQ')

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

Which produces the error:

WARNING: Couldn't read movie file https://www.youtube.com/watch?v=oCUqsPLvYBQ
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /tmp/opencv20160107-29960-t5glvv/opencv-2.4.12/modules/highgui/src/window.cpp, line 261

Is there a simple fix that would allow me to stream this video feed into my python shell via cv2? Not absolutely committed to cv2, either, if there are other libraries out there that will accomplish the same purpose.

like image 999
aaron Avatar asked May 31 '16 20:05

aaron


People also ask

Can OpenCV capture video?

OpenCV provides a very simple interface to do this. Let's capture a video from the camera (I am using the built-in webcam on my laptop), convert it into grayscale video and display it. Just a simple task to get started. To capture a video, you need to create a VideoCapture object.

Which function in OpenCV is used to capture videos?

OpenCV VideoCapture OpenCV provides the VideoCature() function which is used to work with the Camera. We can do the following task: Read video, display video, and save video. Capture from the camera and display it.


3 Answers

you need to have 2 things installed

  1. pafy (pip install pafy)
  2. youtube_dl (sudo pip install --upgrade youtube_dl)

after installing these two packages you can use the youtube url to play the streaming videos from youtube. Please refer the code below

url = 'https://youtu.be/W1yKqFZ34y4'
vPafy = pafy.new(url)
play = vPafy.getbest(preftype="webm")

#start the video
cap = cv2.VideoCapture(play.url)
while (True):
    ret,frame = cap.read()
    """
    your code here
    """
    cv2.imshow('frame',frame)
    if cv2.waitKey(20) & 0xFF == ord('q'):
        break    

cap.release()
cv2.destroyAllWindows()
like image 182
Vaibhav K Avatar answered Oct 05 '22 10:10

Vaibhav K


it is possible with pafy (https://pypi.python.org/pypi/pafy)

import cv2, pafy

url = "https://www.youtube.com/watch?v=aKX8uaoy9c8"
videoPafy = pafy.new(url)
best = videoPafy.getbest(preftype="webm")

video=cv2.VideoCapture(best.url)
like image 28
Sylwek Avatar answered Oct 05 '22 11:10

Sylwek


@incBrain's suggestion to download the youtube video to local mp4 was the way to go here. Here were the steps that I used to set up a remote server environment on EC2, with output piped into my local computer via X11 forwarding:

  • ssh -X -i "<ssh_key.pem>" ubuntu@<IP-address>.compute-1.amazonaws.com (Note the -X option is an important addition here. It's what we use to pass output from the EC-2 server to a local X11 client)
  • sudo pip install --upgrade youtube_dl (I know, sudo pip is bad. I blame the site instructions)
  • Download youtube video to local file: youtube-dl https://www.youtube.com/watch?v=VUjF1fRw9sA -o motocross.mp4
  • python demo_cv.py

X11 forwarding can be tricky. If you run into any hangups there this post might be helpful to you also.

like image 31
aaron Avatar answered Oct 05 '22 10:10

aaron