gomjpeg

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2025 License: MIT Imports: 14 Imported by: 0

README

Golang GOMJPEG package GoDoc Go Report Card

This is golang mjpeg package that can be used to fetch images from a mjpeg stream in background and convert to golang image. This can be used by applications to render to x11 window or save to local files.

Sample Usage

For a complete set of examples, please refer to the example/main.go file.

package main

import (
	"log"
	"time"

	"github.com/wiless/gomjpeg"
)

func main() {
	// Create a new MJPEG client with options.
	// The URL should be the address of your MJPEG stream.
	// AutoStopTimer will stop the stream after the given seconds of inactivity.
	opts := gomjpeg.MjpegOpts{URL: "http://example.com/mjpeg_stream", AutoStopTimer: 15}
	opts.EnableLog = true // Enable logging for debugging purposes.

	// Tip: You can also configure the client using environment variables.
	// Create a .env file in the same directory as your application with the following content:
	//
	// MJPEG_URL=http://another-example.com/mjpeg_stream
	// MJPEG_AUTOSTOP_TIMER=30
	//
	// The client will automatically load these variables and override the options set in the code.

	mjpegClient := gomjpeg.NewMjpeg(opts)

	// You can optionally get a status channel to monitor the stream's status.
	go func() {
		for _ = range mjpegClient.GetStatusChannel() {
			log.Printf("Stream status: %s", mjpegClient.GetStatusCodeString())
		}
	}()

	// Start the stream. This returns a channel that will receive the images.
	imageChannel := mjpegClient.Start()

	// Read images from the channel in a separate goroutine.
	go func() {
		for img := range imageChannel {
			// Do something with the image, e.g., save it, display it, etc.
			// fmt.Printf("\r[%s] Received image with dimensions: %dx%d", time.Now().Local().Format("15:04:05.00"), img.Bounds().Dx(), img.Bounds().Dy())
		}
	}()

	// Let the stream run for 10 seconds and then stop it.
	log.Println("Running stream for 10 seconds...")
	time.Sleep(10 * time.Second)
	mjpegClient.Stop()
	log.Println("Stream stopped.")

	log.Println("Done.")
}

Configuration

  • The MJPEG stream object can be configured using the MjpegOpts struct.
  • The stream can also be configured using environment variables:
    • MJPEG_URL: URL of the MJPEG stream.
    • MJPEG_AUTOSTOP_TIMER: Auto-stop timer in seconds (-1 to disable).
    • MJPEG_RESIZE: Enable image resizing (e.g., true).
    • MJPEG_WIDTH: Width of the resized image.
    • MJPEG_HEIGHT: Height of the resized image.
    • MJPEG_ENABLE_LOG: Enable or disable logging (e.g., true).

Concept

  • The library uses the godotenv package to load environment variables from a .env file.

Documentation

Overview

Package gomjpeg provides a client for fetching and decoding MJPEG streams.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mjpeg

type Mjpeg struct {

	// ImageStream is a channel that receives decoded images from the stream.
	ImageStream chan image.Image

	// EnableLog enables or disables logging.
	EnableLog bool
	// contains filtered or unexported fields
}

Mjpeg represents an MJPEG stream client.

func NewMjpeg

func NewMjpeg(opts MjpegOpts) *Mjpeg

NewMjpeg creates a new Mjpeg client with the given options. It loads environment variables from a .env file to override the options.

func (*Mjpeg) GetControlChannel

func (m *Mjpeg) GetControlChannel() chan StreamControl

GetControlChannel returns the channel for controlling the stream.

func (*Mjpeg) GetStatusChannel

func (m *Mjpeg) GetStatusChannel() chan StatusCode

GetStatusChannel returns the channel for receiving status updates.

func (*Mjpeg) GetStatusCode

func (m *Mjpeg) GetStatusCode() StatusCode

GetStatusCode returns the current status code of the stream.

func (*Mjpeg) GetStatusCodeString

func (m *Mjpeg) GetStatusCodeString() string

GetStatusCodeString returns the current status as a string.

func (*Mjpeg) Pause

func (m *Mjpeg) Pause()

Pause pauses the MJPEG stream.

func (*Mjpeg) ResetTimer

func (m *Mjpeg) ResetTimer(duration int)

ResetTimer resets the auto-stop timer with a new duration.

func (*Mjpeg) Resume

func (m *Mjpeg) Resume()

Resume resumes the MJPEG stream.

func (*Mjpeg) Start

func (m *Mjpeg) Start() chan image.Image

Start begins fetching images from the MJPEG stream in a goroutine. It returns a channel that receives the decoded images.

func (*Mjpeg) Stop

func (m *Mjpeg) Stop()

Stop stops the MJPEG stream.

type MjpegOpts

type MjpegOpts struct {
	// URL of the MJPEG stream.
	URL string
	// AutoStopTimer stops the stream after the specified duration in seconds.
	// A value of -1 disables the auto-stop timer.
	AutoStopTimer int
	// Resize enables resizing of the received JPEG images.
	Resize bool
	// Width of the resized image.
	Width int
	// Height of the resized image.
	Height int
	// EnableLog enables or disables logging.
	EnableLog bool
}

MjpegOpts holds configuration options for the MJPEG stream.

type StatusCode

type StatusCode int

StatusCode represents the status of the MJPEG stream.

const (
	StatusPlaying StatusCode = iota
	StatusStopped
	StatusError
	StatusPaused
)

type StreamControl

type StreamControl int

StreamControl represents a command to control the MJPEG stream.

const (
	StartStream StreamControl = iota
	StopStream
	PauseStream
	ResumeStream
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL