abhiTronix / deffcode

A cross-platform High-performance FFmpeg based Real-time Video Frames Decoder in Pure Python 🎞️⚡
https://abhitronix.github.io/deffcode
Apache License 2.0
178 stars 3 forks source link

[Question]: Read YouTube Videos #34

Closed hafidh561 closed 1 year ago

hafidh561 commented 1 year ago

Issue guidelines

Issue Checklist

Describe your Question

Hello, I hope you're in a good way. By the way, I have some questions. How to read videos on youtube? I tried to code on Basic Recipes Network Streams and got some errors. Thanks

Terminal log output(Optional)

File "C:\ProgramData\Anaconda3\lib\site-packages\deffcode\ffdecoder.py", line 188, in __init__
    Sourcer(
  File "C:\ProgramData\Anaconda3\lib\site-packages\deffcode\sourcer.py", line 307, in probe_stream
    raise ValueError(
ValueError: Invalid source with no decodable audio or video stream provided. Aborting!

Python Code(Optional)

self.options_cap = {}
self.options_cap["-rtsp_transport"] = "tcp"
self.configs["data_path"] = "https://www.youtube.com/watch?v=Tnz0i9VpncA"
self.cap = FFdecoder(source=self.configs["data_path"], frame_format="bgr24", **self.options_cap).formulate()

DeFFcode Version

0.2.4

Python version

3.9.7

Operating System version

Windows 11

Any other Relevant Information?

No response

abhiTronix commented 1 year ago

How to read videos on youtube?

@hafidh561 You need to use yt-dlp or my vidgear 's CamGear API (recommended) as follows:

# 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/uCy5OuSQnyA", 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

    # 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()
hafidh561 commented 1 year ago

How to read videos on youtube?

@hafidh561 You need to use yt-dlp or my vidgear 's CamGear API (recommended) as follows:

# 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/uCy5OuSQnyA", 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

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

Thanks for your answers @abhiTronix . I try this and it's worked for me. But, there is a problem again. The output of the frame is (240, 426, 3) even though, the source of the video on youtube has the best resolution is 1280x720. The quality I used is best for default.

abhiTronix commented 1 year ago

I try this and it's worked for me. But, there is a problem again. The output of the frame is (240, 426, 3) even though, the source of the video on youtube has the best resolution is 1280x720. The quality I used is best for default.

@hafidh561 You can use STREAM_RESOLUTION option in CamGear API:

# set desired quality as 720p
options = {"STREAM_RESOLUTION": "720p"}

# 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/uCy5OuSQnyA", stream_mode=True, logging=True, **options
).start()