Is it possible to stream video from https:// (e.g. YouTube) into python with OpenCV?
Solution 1
you need to have 2 things installed
- pafy (pip install pafy)
- 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()
Solution 2
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)
Solution 3
@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.
Solution 4
I've added Youtube URL source support in my VidGear Python Library that automatically pipelines YouTube Video into OpenCV by providing its URL only. Here is a complete python example:
For VidGear v0.1.9
below:
# import libraries
from vidgear.gears import CamGear
import cv2
stream = CamGear(source='https://youtu.be/dQw4w9WgXcQ', y_tube = True, logging=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
# safely close video stream.
stream.stop()
For VidGear v0.2.0
and above: (y_tube
changed to stream_mode
)
# import libraries
from vidgear.gears import CamGear
import cv2
stream = CamGear(source='https://youtu.be/dQw4w9WgXcQ', stream_mode = True, logging=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
# safely close video stream.
stream.stop()
Related videos on Youtube
aaron
Working on stockbets.io: fantasy sports + a fully-featured simulated brokerage account running on U.S. public securities data. We're launching publicly soon, but give us a shout at [email protected] if you'd like to get on before that. My background is working with in bread and butter ML tools like random forests, xgboost, etc., along with some with deep learning for image recognition. More recently I've been diving into fullstack web app development with a react-based SPA running on top of a flask API server, using elastically scaling celery + redis + airflow + RMQ as the data processing backend on AWS. Looking at switching some of our infra over to FastAPI. Before jumping into the stockbets project I was the CTO at credijusto.com and the VP of engineering at Capital Technologies.
Updated on July 09, 2022Comments
-
aaron over 1 year
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 tocv2
, either, if there are other libraries out there that will accomplish the same purpose.-
aaron@incBrain:
youtube-dl
was the way to go. Thanks for the tip! -
Dimitri PodborskiI don't think it's possible to open
VideoCapture
like this. In openCV documentation on videocapture is written that the argument should be a file. But you can use youtube-dl in between.
-
-
burny over 5 yearsThis doesn't work (at least with OpenCV 3.4.0). video.read() returns always (False, None). The url is valid, e.g. wget would download the video.
-
Anand C U over 5 yearsIs it possible to access it as a stream? Instead of having to wait for the file to download.
-
aaron over 5 yearsRight on, thanks. It's been ages since I asked this question. Could someone please test this and confirm that it works so that I can select the answer??
-
Vaibhav K over 5 yearsIt is working as I have pasted the working code from my script. you can also try it.
-
Arjun Kava over 5 yearsNot Working, using opencv 3.4.0
-
wownis over 4 years@ArjunKava Do you found way to fix this issue?
-
wownis over 4 years@burny You found a way to fix it?
-
Arjun Kava over 4 yearsNope, @wownis. I had downloaded a video stream in the chunk and then used video capture.
-
balalaika over 4 yearsHow is this accepted as an answer VideoCapture() accepts only filenames according to documentation and any youtube stream url just returns nothing.
-
Mustard Tiger over 4 yearsIOError: ERROR: W1yKqFZ34y4: "token" parameter not in video info for unknown reason; please report this issue on yt-dl.org/bug . Make sure you are using the latest version; see yt-dl.org/update on how to update. Be sure to call youtube-dl with the --verbose flag and include its com plete output.
-
WilderField about 4 yearsTraceback (most recent call last): File "test2.py", line 18, in <module> cv2.imshow('frame',frame) cv2.error: OpenCV(3.4.2) /tmp/build/80754af9/opencv-suite_1535558553474/work/modules/highgui/src/window.cpp:356: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'
-
ruckc about 4 yearsI tried this and got None frame, same as I got with pafy.
-
abhiTronix about 4 years@ruckc This is an issue with the
opencv-python
library that doesn't support thehttps
protocol due to lack of OpenSSL support in its FFmpeg build, more insight of this bug is here. The good news is that I recently pushed a PR in opencv-python library that completely fixes this issue and has been merge by its author. So kindly wait few days when these bug-free binaries officially get available on PyPi to download. Goodluck. -
abhiTronix about 4 years@ruckc This bug is now fixed. Kindly update to latest opencv-python.
-
Francis Gonzales almost 4 years@ArjunKava the solution is to upgrade your openCV version :-)
-
Mumbaikar007 over 3 yearscan you make this code switch to live...? I mean, whenever I use it for a live stream that has been playing on YouTube for 20 mins, this code always starts reading from 00.00. I want it to start from 20 mins
-
abhiTronix over 3 years@Mumbaikar007 Raise this issue on our GitHub repo. here, we will surely work on it.
-
burny over 3 yearsAlso, we have legal constraints that disallows us from actually downloading the file and requires us to use streaming only.
-
Dr Sheldon over 2 yearstry play = vPafy.getbest(preftype="mp4") instead of play = vPafy.getbest(preftype="webm")