Skip to content
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

Closed
Smart-Stadium opened this issue Jun 3, 2020 · 30 comments · Fixed by #183, #186 or #274
Closed

live stream from youtube #133

Smart-Stadium opened this issue Jun 3, 2020 · 30 comments · Fixed by #183, #186 or #274
Labels
BUG 🐛 Vidgear api's error, flaw or fault EXTERNAL BUG 👾 Bug caused by external dependent library SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Milestone

Comments

@Smart-Stadium
Copy link

Smart-Stadium commented Jun 3, 2020

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->

# import libraries
from vidgear.gears import CamGear
import cv2
stream = CamGear(source='https://www.youtube.com/watch?v=d29O7DT6NHQ', 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.
@abhiTronix abhiTronix added BUG CONFIRMED ✅ Bug is confirmed! EXTERNAL BUG 👾 Bug caused by external dependent library labels Jun 3, 2020
@abhiTronix
Copy link
Owner

abhiTronix commented Jun 3, 2020

Bug Discovered

@Smart-Stadium This is a bug in OpenCV's FFmpeg backend which cannot handle hls/m3u8 streams provided by youtube-dl or pafy, if you request it for certain real-time youtube streams. Thereby, the real culprit here is FFmpeg which has following hls/m3u8 bug:

zaps166/QMPlay2#139
https://trac.ffmpeg.org/ticket/5348
https://trac.ffmpeg.org/ticket/6287
https://trac.ffmpeg.org/ticket/7804

 

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 [hls,applehttp @ 0x7f4454000b80] Failed to reload playlist 0f=0/0

 

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.

@abhiTronix abhiTronix added the WORK IN PROGRESS 🚧 currently been worked on. label Jun 3, 2020
@abhiTronix
Copy link
Owner

abhiTronix commented Jun 4, 2020

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 print(cv2.getBuildInformation()) python command and see if output contains something similar as follows:

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!

@imranilyas

This comment has been minimized.

@abhiTronix

This comment has been minimized.

@imranilyas

This comment has been minimized.

@abhiTronix

This comment has been minimized.

@imranilyas

This comment has been minimized.

@abhiTronix

This comment has been minimized.

@imranilyas

This comment has been minimized.

@abhiTronix

This comment has been minimized.

@abhiTronix abhiTronix added WAITING FOR RESPONSE ⏳ Waiting for the user response. and removed WAITING FOR RESPONSE ⏳ Waiting for the user response. labels Dec 12, 2020
@imranilyas

This comment has been minimized.

@abhiTronix

This comment has been minimized.

@abhiTronix

This comment has been minimized.

@imranilyas

This comment has been minimized.

@abhiTronix abhiTronix removed the WORKAROUND PRESENT 🆗 A workaround is present for this issue, but not resolved! label Dec 24, 2020
@abhiTronix abhiTronix added this to Backlog in VidGear v0.2.4 via automation Dec 4, 2021
@abhiTronix abhiTronix modified the milestones: 0.2.0, 0.2.4 Dec 4, 2021
@abhiTronix abhiTronix added BUG 🐛 Vidgear api's error, flaw or fault WORK IN PROGRESS 🚧 currently been worked on. and removed PARTIALLY RESOLVED ⌛ This issue is partially resolved through a temporary solution. labels Dec 4, 2021
@abhiTronix
Copy link
Owner

abhiTronix commented Dec 4, 2021

@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()

@abhiTronix abhiTronix reopened this Dec 4, 2021
VidGear v0.2.4 automation moved this from Backlog to In progress Dec 4, 2021
@abhiTronix
Copy link
Owner

abhiTronix commented Dec 4, 2021

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

VidGear v0.2.4 automation moved this from In progress to Done Dec 4, 2021
@abhiTronix abhiTronix added SOLVED 🏁 This issue/PR is resolved now. Goal Achieved! and removed WORK IN PROGRESS 🚧 currently been worked on. labels Dec 4, 2021
@Smart-Stadium
Copy link
Author

Thank you for your job. My project is finished but as soon as I have a little bit time, I'll test.

@Teagueporter
Copy link

I'm getting a Invalid NAL unit 0, skipping error. I have
stream = CamGear(
source="https://youtu.be/j1GLs_fMn1s", stream_mode=True, logging=True, **options
).start()

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.

@abhiTronix
Copy link
Owner

abhiTronix commented May 23, 2022

@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.

@Teagueporter
Copy link

@abhiTronix ok sounds good! Thank you! Do you know how long it might take?

@abhiTronix
Copy link
Owner

abhiTronix commented May 24, 2022

@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/

@abhiTronix
Copy link
Owner

@Teagueporter This code will work for the time being. Please report any issues if found. Goodluck.

@Teagueporter
Copy link

Thank you!

@Teagueporter
Copy link

Teagueporter commented May 25, 2022

@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
error

@abhiTronix
Copy link
Owner

@Teagueporter I'm not seeing any deffcode logs except version. Paste the complete logs with verbose=True in FFdecoder.

@Teagueporter
Copy link

Teagueporter commented May 25, 2022

@abhiTronix
Screenshot from 2022-05-25 10-18-50
I think this is what you are looking for

This is my code
Screenshot from 2022-05-25 10-19-54

@abhiTronix
Copy link
Owner

@Teagueporter still no invalid nal unit errors in log.

@Teagueporter
Copy link

Teagueporter commented May 25, 2022

@abhiTronix Sorry I get the error in my terminal, here is the screen shot of that error
Screenshot from 2022-05-25 12-27-14

.
What happens is the video will play for about 4 to 5 seconds but the invail nal unit error will happen almost as soon as I begin the program

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BUG 🐛 Vidgear api's error, flaw or fault EXTERNAL BUG 👾 Bug caused by external dependent library SOLVED 🏁 This issue/PR is resolved now. Goal Achieved!
Projects
No open projects
4 participants