Closed hafidh561 closed 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()
How to read videos on youtube?
@hafidh561 You need to use
yt-dlp
or myvidgear
'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.
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()
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)
Python Code(Optional)
DeFFcode Version
0.2.4
Python version
3.9.7
Operating System version
Windows 11
Any other Relevant Information?
No response