Documentation
¶
Overview ¶
Package videosift extracts the minimum set of visually unique frames from a video file, suitable for processing with image-based machine learning models such as content moderation classifiers.
The package runs up to four FFmpeg-based extraction strategies in parallel (scene change detection, keyframe extraction, uniform temporal sampling, and MPDecimate), then deduplicates the full candidate set using perceptual hashing and Hamming distance comparison.
FFmpeg and FFprobe must be installed and available on PATH (or specified via Config.FFmpegPath / Config.FFprobePath). No CGo is required.
Basic usage:
frames, err := videosift.Extract(ctx, "input.mp4", videosift.DefaultConfig())
for _, f := range frames {
fmt.Printf("[%s] %.3fs → %s\n", f.Strategy, f.TimestampSec, f.Path)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoBinaries is returned when ffmpeg or ffprobe cannot be found. ErrNoBinaries = errors.New("videosift: ffmpeg/ffprobe not found in PATH") // ErrNoStrategies is returned when all extraction strategies are disabled. ErrNoStrategies = errors.New("videosift: all extraction strategies disabled") // ErrNoFrames is returned when the pipeline produces zero frames. ErrNoFrames = errors.New("videosift: no frames extracted") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Strategy toggles — all enabled by default.
Scene bool
Keyframe bool
Temporal bool
MPDecimate bool
// SceneThreshold is the scene-change score [0.0, 1.0] that triggers
// frame selection. Lower values are more sensitive. Default: 0.4.
SceneThreshold float64
// TemporalInterval is the number of seconds between uniformly sampled
// frames (i.e. fps=1/TemporalInterval). Default: 2.0.
TemporalInterval float64
// MPDecimate filter parameters. Defaults map to FFmpeg's built-in defaults.
MPDecimateHi int // default 768 (64*12)
MPDecimateLo int // default 320 (64*5)
MPDecimateFrac float64 // default 0.33
// HashAlgo selects pHash or dHash for Hamming-distance deduplication.
// Default: HashPHash.
HashAlgo HashAlgo
// HammingThreshold is the maximum Hamming distance at which two frames
// are considered duplicates; the later frame is dropped. Default: 8.
// Set to 0 to disable hash-based deduplication.
HammingThreshold int
// HashResizeWidth is the pixel width frames are scaled to before hashing.
// Smaller values speed up hashing with minimal accuracy loss. Default: 256.
// Set to 0 to disable rescaling (not recommended for large videos).
HashResizeWidth int
// MaxFrames caps the size of the final frame set. When the deduplicated
// count exceeds MaxFrames, frames are selected by uniform stride to
// preserve temporal spread. 0 means no cap.
MaxFrames int
// WorkDir is the directory where extracted PNGs are written.
// If empty, Extract creates a temporary directory and removes it on return.
// If set, the caller owns the directory and is responsible for cleanup.
WorkDir string
// FFmpegPath and FFprobePath override binary discovery via PATH.
FFmpegPath string
FFprobePath string
// Concurrency limits how many strategy goroutines run simultaneously.
// Defaults to the number of enabled strategies (all run in parallel).
Concurrency int
}
Config controls every tunable parameter of the extraction pipeline. Use DefaultConfig() to obtain sensible defaults, then override as needed.
Note the zero-value asymmetry: applyDefaults fills in every zero-valued field except HammingThreshold and HashResizeWidth, whose zero value means "disabled" rather than "use default". A from-scratch Config{} therefore runs with hash dedup and resize off; use DefaultConfig() to get 8/256.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with all strategies enabled and sensible defaults suitable for most content-moderation workloads.
type FFmpegError ¶
FFmpegError wraps a non-zero ffmpeg or ffprobe exit with its captured stderr.
func (*FFmpegError) Error ¶
func (e *FFmpegError) Error() string
func (*FFmpegError) Unwrap ¶
func (e *FFmpegError) Unwrap() error
type Frame ¶
type Frame struct {
// Index is the 0-based position in the final ordered set.
Index int
// TimestampSec is the presentation timestamp of this frame in seconds.
TimestampSec float64
// Strategy is the extraction method that first produced this frame.
Strategy Strategy
// Path is the absolute path to the extracted PNG file.
Path string
// Hash is the perceptual hash value, available for downstream use.
Hash uint64
// Video holds source video metadata. The same pointer is shared across
// all frames in a result set.
Video *VideoInfo
}
Frame is one extracted, deduplicated frame in the final result set.
func Extract ¶
Extract runs the full extraction pipeline for videoPath and returns the deduplicated, ordered set of frames.
When cfg.WorkDir is empty, Extract creates a temporary directory for intermediate files and removes it before returning, leaving only the final []Frame paths valid for the lifetime of the returned slice — callers must copy the files if they need them after Extract returns. When WorkDir is set by the caller, the extracted PNGs persist and the caller is responsible for cleanup.
type HashAlgo ¶
type HashAlgo string
HashAlgo selects the perceptual hash function used for deduplication.