In this example, we will be Live-Streaming video-frames from Raspberry Pi (with Camera Module connected) using PiGear API and StreamGear API's Real-time Frames Mode:
Use -window_size & -extra_window_size FFmpeg parameters for controlling number of frames to be kept in Chunks. Less these value, less will be latency.
After every few chunks (equal to the sum of -window_size & -extra_window_size values), all chunks will be overwritten in Live-Streaming. Thereby, since newer chunks in manifest/playlist will contain NO information of any older ones, and therefore resultant DASH/HLS stream will play only the most recent frames.
In this mode, StreamGear DOES NOT automatically maps video-source audio to generated streams. You need to manually assign separate audio-source through -audio attribute of stream_params dictionary parameter.
PiGear API now fully supports the newer picamera2 python library under the hood for Raspberry Pi camera modules. Follow this guide ➶ for its installation.
# import required librariesfromvidgear.gearsimportPiGearfromvidgear.gearsimportStreamGearfromlibcameraimportTransformimportcv2# formulate various Picamera2 API # configurational parametersoptions={"queue":True,"buffer_count":4,"controls":{"Brightness":0.5,"ExposureValue":2.0},"transform":Transform(hflip=1),"auto_align_output_config":True,# auto-align camera configuration}# open pi video stream with defined parametersstream=PiGear(resolution=(640,480),framerate=60,logging=True,**options).start()# enable livestreaming and retrieve framerate from CamGear Stream and# pass it as `-input_framerate` parameter for controlled frameratestream_params={"-input_framerate":stream.framerate,"-livestream":True}# describe a suitable manifest-file location/namestreamer=StreamGear(output="dash_out.mpd",**stream_params)# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the frame here}# send frame to streamerstreamer.stream(frame)# Show output windowcv2.imshow("Output Frame",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()# safely close streamerstreamer.close()
Under the hood, PiGear API (version 0.3.3 onwards) prioritizes the new picamera2 API backend.
However, PiGear API seamlessly switches to the legacy picamera backend, if the picamera2 library is unavailable or not installed.
It is advised to enable logging(logging=True) to see which backend is being used.
The picamera library is built on the legacy camera stack that is NOT (and never has been) supported on 64-bit OS builds.
You could also enforce the legacy picamera API backend in PiGear by using the enforce_legacy_picamera user-defined optional parameter boolean attribute.
# import required librariesfromvidgear.gearsimportPiGearfromvidgear.gearsimportStreamGearimportcv2# formulate various Picamera API # configurational parametersoptions={"hflip":True,"exposure_mode":"auto","iso":800,"exposure_compensation":15,"awb_mode":"horizon","sensor_mode":0,}# open pi video stream with defined parametersstream=PiGear(resolution=(640,480),framerate=60,logging=True,**options).start()# enable livestreaming and retrieve framerate from CamGear Stream and# pass it as `-input_framerate` parameter for controlled frameratestream_params={"-input_framerate":stream.framerate,"-livestream":True}# describe a suitable manifest-file location/namestreamer=StreamGear(output="dash_out.mpd",**stream_params)# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the frame here}# send frame to streamerstreamer.stream(frame)# Show output windowcv2.imshow("Output Frame",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()# safely close streamerstreamer.close()
# import required librariesfromvidgear.gearsimportPiGearfromvidgear.gearsimportStreamGearfromlibcameraimportTransformimportcv2# formulate various Picamera2 API # configurational parametersoptions={"queue":True,"buffer_count":4,"controls":{"Brightness":0.5,"ExposureValue":2.0},"transform":Transform(hflip=1),"auto_align_output_config":True,# auto-align camera configuration}# open pi video stream with defined parametersstream=PiGear(resolution=(640,480),framerate=60,logging=True,**options).start()# enable livestreaming and retrieve framerate from CamGear Stream and# pass it as `-input_framerate` parameter for controlled frameratestream_params={"-input_framerate":stream.framerate,"-livestream":True}# describe a suitable manifest-file location/namestreamer=StreamGear(output="hls_out.m3u8",format="hls",**stream_params)# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the frame here}# send frame to streamerstreamer.stream(frame)# Show output windowcv2.imshow("Output Frame",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()# safely close streamerstreamer.close()
Under the hood, PiGear API (version 0.3.3 onwards) prioritizes the new picamera2 API backend.
However, PiGear API seamlessly switches to the legacy picamera backend, if the picamera2 library is unavailable or not installed.
It is advised to enable logging(logging=True) to see which backend is being used.
The picamera library is built on the legacy camera stack that is NOT (and never has been) supported on 64-bit OS builds.
You could also enforce the legacy picamera API backend in PiGear by using the enforce_legacy_picamera user-defined optional parameter boolean attribute.
# import required librariesfromvidgear.gearsimportPiGearfromvidgear.gearsimportStreamGearimportcv2# formulate various Picamera API # configurational parametersoptions={"hflip":True,"exposure_mode":"auto","iso":800,"exposure_compensation":15,"awb_mode":"horizon","sensor_mode":0,}# open pi video stream with defined parametersstream=PiGear(resolution=(640,480),framerate=60,logging=True,**options).start()# enable livestreaming and retrieve framerate from CamGear Stream and# pass it as `-input_framerate` parameter for controlled frameratestream_params={"-input_framerate":stream.framerate,"-livestream":True}# describe a suitable manifest-file location/namestreamer=StreamGear(output="hls_out.m3u8",format="hls",**stream_params)# loop overwhileTrue:# read frames from streamframe=stream.read()# check for frame if NonetypeifframeisNone:break# {do something with the frame here}# send frame to streamerstreamer.stream(frame)# Show output windowcv2.imshow("Output Frame",frame)# check for 'q' key if pressedkey=cv2.waitKey(1)&0xFFifkey==ord("q"):break# close output windowcv2.destroyAllWindows()# safely close video streamstream.stop()# safely close streamerstreamer.close()