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]: How to set USB Camera resolution ratio? #46

Closed GavinJIAW closed 3 months ago

GavinJIAW commented 3 months ago

Issue guidelines

Issue Checklist

Describe your Question

My code is not work

Terminal log output(Optional)

No response

Python Code(Optional)

# import the necessary packages
from deffcode import FFdecoder
import cv2

ffparams = {
            "-video_size": f'{1920}x{1080}'
            }
# initialize and formulate the decoder with "0" index source for BGR24 output
decoder = FFdecoder("0", frame_format="bgr24", verbose=True,**ffparams).formulate()

# grab the BGR24 frames from decoder
for frame in decoder.generateFrame():

    # check if frame is None
    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()

# terminate the decoder
decoder.terminate()

DeFFcode Version

0.2.3

Python version

3.10

Operating System version

Windows10

Any other Relevant Information?

No response

abhiTronix commented 3 months ago

@GavinJIAW Use following code, as explained here: https://abhitronix.github.io/deffcode/latest/recipes/advanced/update-metadata/?h=size#overriding-source-video-metadata-in-ffdecoder-api

# import the necessary packages
from deffcode import FFdecoder
import cv2

# initialize and formulate the decoder using suitable source
decoder = FFdecoder("foo.mp4", verbose=True)

# override source metadata values
# !!! [WARNING] Make sure each value datatype matches the table !!!
decoder.metadata = {
    "source_video_resolution": [1920, 1080],  # 1920x1080 frame-size
}

# finally formulate the decoder
decoder.formulate()

# [NOTE] uncomment following line to debug values
# print(decoder.metadata)

# let's grab the 1280x720 sized gray frames from decoder
for frame in decoder.generateFrame():

    # check if frame is None
    if frame is None:
        break

    # {do something with gray frame here}

    # Show gray frames in output window
    cv2.imshow("Output gray", 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()