Package: mjpeg-streamer
mjpeg-streamerOverviewThe mjpeg-streamer package provides a simple, flexible and efficient way to stream MJPEG video from OpenCV-compatible sources over HTTP. It provides a flexible interface that allows users to specify the video source and configure various parameters such as image size, quality, and FPS.InstallationYou can install the package via pip:pipinstallmjpeg-streamerI would really recommend using--prefer-binary, especially with older versions of Python (e.g. 3.6). This will install pre-compiled binaries instead of building from source, which is much faster and less error-prone.pipinstallmjpeg-streamer--prefer-binaryLatest versions of dependencies (e.g. Numpy) don't always ship with pre-compiled binaries for older versions of Python, so this option installs the latest compatible version instead, even though it might be a bit older.UsageLibraryHere's a simple example that shows how to use themjpeg_streamerpackage to stream video from a webcam:importcv2frommjpeg_streamerimportMjpegServer,Streamcap=cv2.VideoCapture(0)stream=Stream("my_camera",size=(640,480),quality=50,fps=30)server=MjpegServer("localhost",8080)server.add_stream(stream)server.start()whileTrue:_,frame=cap.read()cv2.imshow(stream.name,frame)ifcv2.waitKey(1)==ord("q"):breakstream.set_frame(frame)server.stop()cap.release()cv2.destroyAllWindows()This example starts the MJPEG server onlocalhost:8080and streams video from the webcam with the index of0. The video is resized to 640x480 pixels, compressed with JPEG quality of 50, and streamed at 30 FPS.To view the video stream, you can open a web browser and navigate tohttp://localhost:8080/my_camera.To view the streams index, you can open a web browser and navigate tohttp://localhost:8080.Don't forget to check out theexamplesdirectory for more examples.Check out theclass referencefor more details on the classes and methods provided by the package.Command Line InterfaceThe package also provides a simple command line interface that allows you to stream video from multiple sources using a single command. Here's an example that shows how to stream video from a webcam and a video file:$mjpeg-streamer-s0-s"video file.mp4"--prefix"ender 3 pro"--quality75--fps24--show-bandwidth

Streamsindex:http://localhost:8080
Availablestreams:

http://localhost:8080/ender_3_pro_0
http://localhost:8080/ender_3_pro_video_file_mp4
--------------------------------


PressCtrl+Ctostoptheserver

ender_3_pro_video_file_mp4:599.28KB/s|ender_3_pro_0:824.44KB/sThis command starts the MJPEG server onlocalhost:8080and streams video from the webcam with the index of0and the video filevideo file.mp4. The streams are prefixed withender 3 proand compressed with JPEG quality of 75 and streamed at 24 FPS. The bandwidth of each stream is displayed in the console.To view the video streams, you can open a web browser and navigate to the URLs displayed in the console.Runmjpeg-streamer --helpfor more information on the available options.Note thatthe command line interface is limited and doesn't provide the same level of flexibility as the library. It's recommended to use the library if you need to customize the video streams or integrate them into your own application.Class ReferenceStreamA class that represents a single video stream. A stream consists of a sequence of frames that can be set and retrieved using the set_frame and get_frame methods.Constructor:Stream(name:str,size:Optional[Tuple[int,int]]=None,quality:int=50,fps:int=30)Creates a new Stream instance with the given unique name, image size, JPEG quality (1-100), and FPS.Methods:set_frameset_frame(frame:np.ndarray)Sets the current frame to the given Numpy array (OpenCV frame).get_bandwidthget_bandwidth()Returns the bandwidth of the stream in bytes per second.Tip: Divide the result by 1024 to get the bandwidth in kilobytes per second.get_frameget_frame()Returns the current frame as a Numpy array.get_frame_processedget_frame_processed()Returns the current frame as a Numpy array after processing it with the specified image size and JPEG quality.MjpegServerA class that represents an MJPEG server. The server can serve multiple video streams, each identified by a unique name.Constructor:MjpegServer(host:str="localhost",port:int=8080)Creates a new MjpegServer instance with the given host and port.Methods:add_streamadd_stream(stream:Stream)Adds a new video stream to the server.startstart()Starts the server in a separate thread.stopstop()Stops the server.ContributingContributions are welcome! Please open an issue or submit a pull request.LicenseThis project is licensed under the AGPL-3.0 License - see theLICENSEfile for details.TL;DR: You can use, modify, and distribute this software for free or for profit, but you must make the source code available to your users and include a copy ofthis licensein your project. Your modified work's license should also mention the original author and a link to this repository.