-
-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
live stream from youtube #133
Comments
Bug Discovered@Smart-Stadium This is a bug in OpenCV's FFmpeg backend which cannot handle hls/m3u8 streams provided by zaps166/QMPlay2#139
Steps to Reproduce this Bug:First, Use youtube-dl to grab Youtube Video HLS m3u8 stream url: youtube-dl -g "https://youtu.be/d29O7DT6NHQ" Then play that URL using FFmpeg's FFplay: ffplay "https://manifest.googlevideo.com/api/manifest/hls_playlist/expire/1591217239/ei/97fXXv3rA4mywgOv4brYCA/ip/103.206.248.130/id/d29O7DT6NHQ.906/itag/96/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D137/hls_chunk_host/r3---sn-bpb5oupj-qxae.googlevideo.com/playlist_duration/30/manifest_duration/30/vprv/1/playlist_type/DVR/initcwndbps/5040/mh/hQ/mm/44/mn/sn-bpb5oupj-qxae/ms/lva/mv/m/mvi/2/pl/24/dover/11/keepalive/yes/mt/1591195523/disable_polymer/true/sparams/expire,ei,ip,id,itag,source,requiressl,ratebypass,live,goi,sgoap,sgovp,playlist_duration,manifest_duration,vprv,playlist_type/sig/AOq0QJ8wRQIhAP_SCb7nBIMiIv3KvXim1H54wNHBFPVmCscvz4VieJp0AiBZX03sFC1cD3YYTBPnt2-sjxsZ_HnrFrG8s7XBg1tppQ%3D%3D/lsparams/hls_chunk_host,initcwndbps,mh,mm,mn,ms,mv,mvi,pl/lsig/AG3C_xAwRQIgcc8v2_DuEfBByN_g0YiCfRxLmDEmMH2u04G7WM_rXGYCIQCFHnW8SjP2drb0nENv9SIeReNXOygJXbMvO6xYfBhtRA%3D%3D/playlist/index.m3u8" and it stops after few seconds with error
But if you play the same hls/m3u8 stream with MPV media player (or even VLC Player) using command: mpv "https://manifest.googlevideo.com/api/manifest/hls_playlist/expire/1591217239/ei/97fXXv3rA4mywgOv4brYCA/ip/103.206.248.130/id/d29O7DT6NHQ.906/itag/96/source/yt_live_broadcast/requiressl/yes/ratebypass/yes/live/1/goi/160/sgoap/gir%3Dyes%3Bitag%3D140/sgovp/gir%3Dyes%3Bitag%3D137/hls_chunk_host/r3---sn-bpb5oupj-qxae.googlevideo.com/playlist_duration/30/manifest_duration/30/vprv/1/playlist_type/DVR/initcwndbps/5040/mh/hQ/mm/44/mn/sn-bpb5oupj-qxae/ms/lva/mv/m/mvi/2/pl/24/dover/11/keepalive/yes/mt/1591195523/disable_polymer/true/sparams/expire,ei,ip,id,itag,source,requiressl,ratebypass,live,goi,sgoap,sgovp,playlist_duration,manifest_duration,vprv,playlist_type/sig/AOq0QJ8wRQIhAP_SCb7nBIMiIv3KvXim1H54wNHBFPVmCscvz4VieJp0AiBZX03sFC1cD3YYTBPnt2-sjxsZ_HnrFrG8s7XBg1tppQ%3D%3D/lsparams/hls_chunk_host,initcwndbps,mh,mm,mn,ms,mv,mvi,pl/lsig/AG3C_xAwRQIgcc8v2_DuEfBByN_g0YiCfRxLmDEmMH2u04G7WM_rXGYCIQCFHnW8SjP2drb0nENv9SIeReNXOygJXbMvO6xYfBhtRA%3D%3D/playlist/index.m3u8" It plays just fine. |
Workaround for this Bug :Use different backend if available like Gstreamer: But first check your OpenCV to be built with GStreamer support. You can easily check it by running Video I/O:
...
GStreamer: YES (ver 1.8.3)
... See this FAQ to compile OpenCV with GStreamer support.Then, if Gstreamer support is present, then use this code: For v0.1.9 and below:# import required libraries
from vidgear.gears import CamGear
import cv2
# Add YouTube Video URL as input source of your choice and change backend
stream = CamGear(source='https://youtu.be/d29O7DT6NHQ', y_tube=True, logging=True, backend=cv2.CAP_GSTREAMER).start()
# loop over
while True:
# read frames from stream
frame = stream.read()
# check for frame if Nonetype
if frame is None:
break
# {do something with the frame here}
# Show output window
cv2.imshow("Output Frame", frame)
# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# close output window
cv2.destroyAllWindows()
# safely close video stream
stream.stop()
# safely close video stream. For v0.2.0 and above: (GSTREAMER backend enforced automatically)# import required libraries
from vidgear.gears import CamGear
import cv2
# Add YouTube Video URL as input source (for e.g https://youtu.be/bvetuLwJIkA)
# and enable Stream Mode (`stream_mode = True`)
stream = CamGear(
source="https://youtu.be/bvetuLwJIkA", stream_mode=True, logging=True
).start()
# loop over
while True:
# read frames from stream
frame = stream.read()
# check for frame if Nonetype
if frame is None:
break
# {do something with the frame here}
# Show output window
cv2.imshow("Output", frame)
# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# close output window
cv2.destroyAllWindows()
# safely close video stream
stream.stop() And problem is gone. Goodluck! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@Smart-Stadium I think #274 resolves this issue too. # install normally
pip install -U vidgear[core] Test your code: # import required libraries
from vidgear.gears import CamGear
import cv2
options = {"STREAM_RESOLUTION": "720p"}
# Add YouTube Video URL as input source (for e.g https://youtu.be/bvetuLwJIkA)
# and enable Stream Mode (`stream_mode = True`)
stream = CamGear(
source="https://youtu.be/j1GLs_fMn1s", stream_mode=True, logging=True, **options
).start()
# loop over
while True:
# read frames from stream
frame = stream.read()
# check for frame if Nonetype
if frame is None:
break
# {do something with the frame here}
# Show output window
cv2.imshow("Output", frame)
# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# close output window
cv2.destroyAllWindows()
# safely close video stream
stream.stop() |
Successfully resolved and merged in commit: dc26c00 Usage docs are available at: https://abhitronix.github.io/vidgear/latest/gears/camgear/usage/#using-camgear-with-youtube-videos |
Thank you for your job. My project is finished but as soon as I have a little bit time, I'll test. |
I'm getting a Invalid NAL unit 0, skipping error. I have and sometimes the video will play for a couple seconds and then freeze and it won't play any more. The goal is I'm trying to get a live stream from youtube using openCV and then apply some background segmentation. |
@Teagueporter this is core FFmpeg problem used by OpenCV as its backend. The only solution is deffcode for this problem, I'll integrate it into vidgear soon. |
@abhiTronix ok sounds good! Thank you! Do you know how long it might take? |
@Teagueporter Yeah, That might take a while. But you can use Deffcode right now as follows with CamGear, it's pretty simply actually: # import required libraries
from vidgear.gears import CamGear
# import the necessary packages
from deffcode import FFdecoder
# Add YouTube Video URL as input source (for e.g https://youtu.be/uCy5OuSQnyA)
# and enable Stream Mode (`stream_mode = True`)
stream = CamGear(
source="https://youtu.be/j1GLs_fMn1s", stream_mode=True, logging=True
).start()
# get Video's metadata as JSON object
video_metadata = stream.ytv_metadata
# get data like `url`
source_url = video_metadata["url"]
# close CamGear
stream.stop()
# initialize and formulate the decoder for BGR24 output
decoder = FFdecoder(source_url, frame_format="bgr24").formulate()
# grab the BGR24 frame from the decoder
for frame in decoder.generateFrame():
# check if frame is None
if frame is None:
break
# Do here whatever you want
# Show output window
cv2.imshow("Output", frame)
# check for 'q' key if pressed
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
# close output window
cv2.destroyAllWindows()
# terminate the decoder
decoder.terminate() Make sure you install deffcode and FFmpeg correctly on your machine. You can find related docs here: https://abhitronix.github.io/deffcode/latest/installation/ |
@Teagueporter This code will work for the time being. Please report any issues if found. Goodluck. |
Thank you! |
@abhiTronix Sorry for so many questions but does GStreamer have to be working? I have deffcode and CamGear and i'm using the same layout as the code you posted above but I'm still getting an Invalid Nal unit 0 after 3ish seconds |
@Teagueporter I'm not seeing any deffcode logs except version. Paste the complete logs with |
@abhiTronix |
@Teagueporter still no invalid nal unit errors in log. |
@abhiTronix Sorry I get the error in my terminal, here is the screen shot of that error . |
Hello, I'm trying to read live stream from youtube but after 10 seconds the streaming blocks.
I tried the following URL : https://www.youtube.com/watch?v=d29O7DT6NHQ - https://www.youtube.com/watch?v=1EiC9bvVGnk.
I red your FAQ & Troubleshooting but I don't find a solution.
I'm working with the last version of Python (3.8 64 bits) and the version of Camgear on Windows 10.
Your code is very simple and works with simple youtube video and RSTP video without problem->
The text was updated successfully, but these errors were encountered: