vid

package
v0.0.0-...-2538462 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Overview

Package vid contains various utilities for reading from video files and cameras.

Index

Constants

This section is empty.

Variables

View Source
var (
	// FourCCMJPEG means Motion-JPEG.
	FourCCMJPEG = FourCC(v4l2.PixelFmtMJPEG)
	// FourCCYUYV means YUYV 4:2:2.
	FourCCYUYV = FourCC(v4l2.PixelFmtYUYV)
	// FourCCYUV420 means yuv420p.
	FourCCYUV420 = FourCCFromString("YU12")
)

Functions

func Probe

func Probe(path string) (fileProbe *FFProbeJSON, vidProbe *FFStream, err error)

Probe runs ffprobe an a video file.

func ProbeSize

func ProbeSize(path string) (w, h int, err error)

ProbeSize runs ffprobe to determine the frame size of a video file.

Types

type CamConfig

type CamConfig struct {
	// For example /dev/video0.
	DeviceFile string

	// Format is the image format FourCC to request from the camera, for example "MJPG".
	// To list available formats and frame sizes:
	//
	//   v4l2-ctl --list-formats-ext --device /dev/video2
	Format FourCC `json:"-"`
	// FormatStr is Format converted to a string. It is only used for JSON serialization and does not need to be set
	// when opening a camera.
	FormatStr string `json:"Format"`
	FrameSize image.Point
}

CamConfig describes an available camera device with a given pixel format and frame size.

func DetectCams

func DetectCams() ([]CamConfig, error)

DetectCams returns a list of detected cameras and their supported pixel formats and frame sizes. This works even if some of the devices are currently in use. Cameras which list no available pixel formats, or produce errors on open, are ignored. Only fixed frame sizes are included.

type CamSrc

type CamSrc struct {
	// contains filtered or unexported fields
}

CamSrc is a video frame source which supports video4linux. Use NewCamSrc to open one.

func NewCamSrc

func NewCamSrc(c CamConfig) (ret *CamSrc, err error)

NewCamSrc tries to open the specified frame source for frame reading.

func (*CamSrc) Close

func (s *CamSrc) Close() error

Close implements Src.

func (*CamSrc) GetFPS

func (s *CamSrc) GetFPS() float64

GetFPS implements Src.

func (*CamSrc) GetFrame

func (s *CamSrc) GetFrame() (image.Image, *time.Time, error)

GetFrame implements Src.

func (*CamSrc) GetFrameRaw

func (s *CamSrc) GetFrameRaw() ([]byte, FourCC, *time.Time, error)

GetFrameRaw returns a raw frame in the specified pixel format from the camera.

func (*CamSrc) IsLive

func (s *CamSrc) IsLive() bool

IsLive implements Src.

type FFDisposition

type FFDisposition struct {
	Default         int `json:"default"`
	Dub             int `json:"dub"`
	Original        int `json:"original"`
	Comment         int `json:"comment"`
	Lyrics          int `json:"lyrics"`
	Karaoke         int `json:"karaoke"`
	Forced          int `json:"forced"`
	HearingImpaired int `json:"hearing_impaired"`
	VisualImpaired  int `json:"visual_impaired"`
	CleanEffects    int `json:"clean_effects"`
	AttachedPic     int `json:"attached_pic"`
	TimedThumbnails int `json:"timed_thumbnails"`
}

FFDisposition is a part of ffprobe JSON output. See FFProbeJSON.

type FFFormat

type FFFormat struct {
	Filename       string `json:"filename"`
	NbStreams      int    `json:"nb_streams"`
	NbPrograms     int    `json:"nb_programs"`
	FormatName     string `json:"format_name"`
	FormatLongName string `json:"format_long_name"`
	StartTime      string `json:"start_time"`
	Duration       string `json:"duration"`
	Size           string `json:"size"`
	BitRate        string `json:"bit_rate"`
	ProbeScore     int    `json:"probe_score"`
	Tags           FFTags `json:"tags"`
}

FFFormat is a part of ffprobe JSON output. See FFProbeJSON.

type FFProbeJSON

type FFProbeJSON struct {
	Streams []FFStream `json:"streams"`
	Format  FFFormat   `json:"format"`
}

FFProbeJSON represents ffprobe JSON output.

type FFStream

type FFStream struct {
	Index            int           `json:"index"`
	CodecName        string        `json:"codec_name"`
	CodecLongName    string        `json:"codec_long_name"`
	Profile          string        `json:"profile"`
	CodecType        string        `json:"codec_type"`
	CodecTimeBase    string        `json:"codec_time_base"`
	CodecTagString   string        `json:"codec_tag_string"`
	CodecTag         string        `json:"codec_tag"`
	Width            int           `json:"width,omitempty"`
	Height           int           `json:"height,omitempty"`
	CodedWidth       int           `json:"coded_width,omitempty"`
	CodedHeight      int           `json:"coded_height,omitempty"`
	HasBFrames       int           `json:"has_b_frames,omitempty"`
	PixFmt           string        `json:"pix_fmt,omitempty"`
	Level            int           `json:"level,omitempty"`
	ColorRange       string        `json:"color_range,omitempty"`
	ColorSpace       string        `json:"color_space,omitempty"`
	ColorTransfer    string        `json:"color_transfer,omitempty"`
	ColorPrimaries   string        `json:"color_primaries,omitempty"`
	ChromaLocation   string        `json:"chroma_location,omitempty"`
	Refs             int           `json:"refs,omitempty"`
	IsAvc            string        `json:"is_avc,omitempty"`
	NalLengthSize    string        `json:"nal_length_size,omitempty"`
	RFrameRate       string        `json:"r_frame_rate"`
	AvgFrameRate     string        `json:"avg_frame_rate"`
	TimeBase         string        `json:"time_base"`
	StartPts         int           `json:"start_pts"`
	StartTime        string        `json:"start_time"`
	DurationTS       int           `json:"duration_ts"`
	Duration         string        `json:"duration"`
	BitRate          string        `json:"bit_rate"`
	BitsPerRawSample string        `json:"bits_per_raw_sample,omitempty"`
	NbFrames         string        `json:"nb_frames"`
	Disposition      FFDisposition `json:"disposition"`
	Tags             FFTags        `json:"tags"`
	SampleFmt        string        `json:"sample_fmt,omitempty"`
	SampleRate       string        `json:"sample_rate,omitempty"`
	Channels         int           `json:"channels,omitempty"`
	ChannelLayout    string        `json:"channel_layout,omitempty"`
	BitsPerSample    int           `json:"bits_per_sample,omitempty"`
	MaxBitRate       string        `json:"max_bit_rate,omitempty"`
}

FFStream is a part of ffprobe JSON output. See FFProbeJSON.

type FFTags

type FFTags struct {
	CreationTime     time.Time `json:"creation_time"`
	Language         string    `json:"language"`
	HandlerName      string    `json:"handler_name"`
	MajorBrand       string    `json:"major_brand"`
	MinorVersion     string    `json:"minor_version"`
	CompatibleBrands string    `json:"compatible_brands"`
	Encoder          string    `json:"encoder"`
}

FFTags is a part of ffprobe JSON output. See FFProbeJSON.

type FileSrc

type FileSrc struct {
	// contains filtered or unexported fields
}

FileSrc is a video file source. Use NewFileSrc() to get an instance.

func NewFileSrc

func NewFileSrc(path string, verbose bool) (src *FileSrc, err error)

NewFileSrc creates a new FileSrc.

func (*FileSrc) Close

func (s *FileSrc) Close() error

Close implements Src.

func (*FileSrc) GetFPS

func (s *FileSrc) GetFPS() float64

GetFPS implements Src.

func (*FileSrc) GetFrame

func (s *FileSrc) GetFrame() (image.Image, *time.Time, error)

GetFrame implements Src.

func (*FileSrc) GetFrameRaw

func (s *FileSrc) GetFrameRaw() ([]byte, FourCC, *time.Time, error)

GetFrameRaw implements Src.

func (*FileSrc) IsLive

func (s *FileSrc) IsLive() bool

IsLive implements Src.

type FourCC

type FourCC int32

FourCC is a FourCC pixel format.

func FourCCFromString

func FourCCFromString(fcc string) FourCC

FourCCFromString converts a string to a numeric FourCC code. Returns 0 on failure.

func (FourCC) String

func (f FourCC) String() string

String converts a FourCC code to string, e.g. 1448695129 to YUYV.

type JPEGScanner

type JPEGScanner struct {
	// contains filtered or unexported fields
}

JPEGScanner works similar to bufio.Scanner but on JPEG images. Useful to pull frame after frame out of a MJPEG stream.

func NewJPEGScanner

func NewJPEGScanner(r io.Reader) *JPEGScanner

NewJPEGScanner creates a new JPEGScanner. Responsibility to close reader remains with caller.

func (*JPEGScanner) Scan

func (s *JPEGScanner) Scan() ([]byte, error)

Scan reads until it has read an entire JPEG image, and returns the buffer containing its data. https://github.com/corkami/formats/blob/master/image/jpeg.md.

type PiCam3Config

type PiCam3Config struct {
	// ROI to extract. Defaults to full image if empty.
	Rect image.Rectangle
	// Constant lens focus, 0=infinity, 2=approx. 0.5m.
	Focus float64
	// Rotate image by 180 degree if true.
	Rotate180 bool
	// Pixel format.
	Format FourCC
	// Frames per second.
	FPS int
}

PiCam3Config is the configuration for a PiCam3Src.

type PiCam3Src

type PiCam3Src struct {
	// contains filtered or unexported fields
}

PiCam3Src is a video frame source which reads frames from a Raspberry PI 3 camera module. It uses the `libcamera-vid` utility internally. Use NewPiCam3Src() to open one.

func NewPiCam3Src

func NewPiCam3Src(c PiCam3Config) (*PiCam3Src, error)

NewPiCam3Src creates a new PiCam3Src.

func (*PiCam3Src) Close

func (s *PiCam3Src) Close() error

Close implements Src.

func (*PiCam3Src) GetFPS

func (s *PiCam3Src) GetFPS() float64

GetFPS implements Src.

func (*PiCam3Src) GetFrame

func (s *PiCam3Src) GetFrame() (image.Image, *time.Time, error)

GetFrame implements Src.

func (*PiCam3Src) GetFrameRaw

func (s *PiCam3Src) GetFrameRaw() ([]byte, FourCC, *time.Time, error)

GetFrameRaw implements Src.

func (*PiCam3Src) IsLive

func (s *PiCam3Src) IsLive() bool

IsLive implements Src.

type Src

type Src interface {
	// GetFrame retrieves the next frame.
	// Note that the underlying image buffer remains owned by the video source,
	// it must not be changed by the caller and might be overwritten on the next
	// invocation.
	// Returns io.EOF after the last frame, after which Close() should be called
	// on the instance before discarding it.
	GetFrame() (image.Image, *time.Time, error)

	// GetFrameRaw retrieves the next frame in the raw pixel format of the source.
	// Note that the underlying image buffer remains owned by the video source,
	// it must not be changed by the caller and might be overwritten on the next
	// invocation.
	// Not all sources might implement this.
	GetFrameRaw() ([]byte, FourCC, *time.Time, error)

	// IsLive returns if the src is a live source (e.g. camera).
	IsLive() bool

	// GetFPS returns the current frame rate of this source.
	GetFPS() float64

	// Close closes the frame source and frees resources.
	Close() error
}

Src describes a frame source.

type SrcBuf

type SrcBuf struct {
	// contains filtered or unexported fields
}

SrcBuf buffers a video source. Use NewSrcBuf to create an instance.

func NewSrcBuf

func NewSrcBuf(src Src, maxFailedFrames int) *SrcBuf

NewSrcBuf creates a new SrcBuf. Will not close src, caller needs to do that after last frame is read.

func (*SrcBuf) Close

func (s *SrcBuf) Close() error

Close implements Src.

func (*SrcBuf) GetFPS

func (s *SrcBuf) GetFPS() float64

GetFPS implements Src.

func (*SrcBuf) GetFrame

func (s *SrcBuf) GetFrame() (image.Image, *time.Time, error)

GetFrame returns the next frame. As soon as this returns an error once, the instance needs to be discarded. The underlying image buffer will be owned by the caller, src will not reuse or modify it.

func (*SrcBuf) GetFrameRaw

func (s *SrcBuf) GetFrameRaw() ([]byte, FourCC, *time.Time, error)

GetFrameRaw implements Src.

func (*SrcBuf) IsLive

func (s *SrcBuf) IsLive() bool

IsLive implements Src.

Jump to

Keyboard shortcuts

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