Is it possible to stream videos from https: // (e.g. YouTube) to python using OpenCV?

This link contains a small example of how to use the OpenCV library for python, cv2 to stream data from the camera to the python shell. I want to do some experiments and would like to use the following YouTube video: https://www.youtube.com/watch?v=oCUqsPLvYBQ .

I tried to adapt 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 

What causes 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 will allow me to transfer this video to my python shell via cv2 ? Not entirely committed to cv2 if there are other libraries that will accomplish the same goal.

+12
source share
4 answers

you need to install 2 things

  1. Pafy (install pipy)
  2. youtube_dl (installing sudo pip --upgrade youtube_dl)

After installing these two packages, you can use the YouTube URL to play streaming video from your handset. Please refer to 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.... """ cv2.imshow('frame',frame) if cv2.waitKey(20) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() 
+10
source

possibly 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) 
+3
source

The @incBrain offering to download youtube video to local mp4 was the way to go here. Here are the steps that I used to configure the remote server environment on EC2, with the output sent to my local computer using X11 forwarding:

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

Forwarding X11 can be difficult. If you encounter any freezes there, this post may be useful to you.

+2
source

First, please update the latest opencv-python or opencv-contrib-python binaries (v4.1.1.26) installed on your computer that have fixed a critical error . Install it from pypi as follows:

 sudo pip install -U opencv-python #or install opencv-contrib-python similarly 

You can then install my Python VidGear library , which automatically translates YouTube videos to OpenCV, providing only its URL. Here is a complete Python example:

 # import libraries from vidgear.gears import CamGear import cv2 stream = CamGear(source='https://youtu.be/dQw4w9WgXcQ', y_tube =True).start() # YouTube Video URL as input # infinite loop while True: frame = stream.read() # read frames # check if frame is None if frame is None: #if True break the infinite loop break # do something with frame here cv2.imshow("Output Frame", frame) # Show output window key = cv2.waitKey(1) & 0xFF # check for 'q' key-press if key == ord("q"): #if 'q' key-pressed break out break cv2.destroyAllWindows() # close output window stream.stop() # safely close video stream. 

Source code

0
source

All Articles