Documentation
¶
Overview ¶
Package media provides FFmpeg-based media processing capabilities.
Crop, progress, and timecode helpers live in nested packages. Package media re-exports their types and functions so importers keep using this path.
Index ¶
- Constants
- Variables
- func ChannelLayoutName(channels int) string
- func DefaultFFmpegTimeout() time.Duration
- func NormalizeOutputWidth(width int) int
- func OutputWidthLabel(width int) string
- func ValidAudioKbps(kbps int) bool
- func ValidCRF(crf int) bool
- func ValidEncoderPreset(name string) bool
- func ValidOutputWidth(width int) bool
- func WithProgress(ctx context.Context, fn func(percent int)) context.Context
- type AudioTrack
- type ClipQuality
- type Clock
- type CropRect
- type ExecFFmpeg
- func (execFFmpeg *ExecFFmpeg) DetectCrop(ctx context.Context, input string, start, duration float64) (CropRect, error)
- func (execFFmpeg *ExecFFmpeg) ExtractClip(ctx context.Context, input, output string, start, duration float64, ...) error
- func (execFFmpeg *ExecFFmpeg) ExtractGIF(ctx context.Context, input, output string, start, duration float64, ...) error
- func (execFFmpeg *ExecFFmpeg) ExtractPreview(ctx context.Context, input, output string, start, duration float64, ...) error
- func (execFFmpeg *ExecFFmpeg) ExtractScreenshot(ctx context.Context, input, output string, timestamp float64, rect CropRect) error
- func (execFFmpeg *ExecFFmpeg) Probe(ctx context.Context, path string) (MediaInfo, error)
- func (execFFmpeg *ExecFFmpeg) SetTimeout(d time.Duration)
- type FFmpeg
- type FFmpegClock
- type MediaInfo
- type QualityPreset
- type Timecode
Constants ¶
const ( // ClipQualityLow is the low quality preset. ClipQualityLow ClipQuality = "low" // ClipQualityMedium is the medium quality preset. ClipQualityMedium ClipQuality = "medium" // ClipQualityHigh is the high quality preset. ClipQualityHigh ClipQuality = "high" // MinCRF is the lowest allowed libx264 CRF. MinCRF = 0 // MaxCRF is the highest allowed libx264 CRF. MaxCRF = 51 // MinAudioKbps is the lowest allowed AAC bitrate. MinAudioKbps = 64 // MaxAudioKbps is the highest allowed AAC bitrate. MaxAudioKbps = 640 // OutputWidth720p is 1280px wide. OutputWidth720p = 1280 // OutputWidth1080p is 1920px wide. OutputWidth1080p = 1920 // OutputWidth1440p is 2560px wide. OutputWidth1440p = 2560 // OutputWidth2160p is 3840px wide (4K). OutputWidth2160p = 3840 )
Variables ¶
var EncoderPresets = []string{
"ultrafast",
"superfast",
"veryfast",
"faster",
"fast",
"medium",
"slow",
"slower",
"veryslow",
}
EncoderPresets lists valid libx264 -preset values from fastest to slowest.
var ErrInvalidTimecode = timecode.ErrInvalidTimecode
ErrInvalidTimecode is returned when a timestamp string cannot be parsed.
var OutputWidths = []int{ OutputWidth720p, OutputWidth1080p, OutputWidth1440p, OutputWidth2160p, }
OutputWidths lists selectable clip export widths.
var QualityPresets = map[ClipQuality]QualityPreset{ ClipQualityLow: { CRF: crfLowQuality, Preset: "veryfast", AudioKbps: audioKbpsLow, MaxWidth: OutputWidth720p, }, ClipQualityMedium: { CRF: crfMediumQuality, Preset: "medium", AudioKbps: audioKbpsMedium, MaxWidth: OutputWidth1080p, }, ClipQualityHigh: { CRF: crfHighQuality, Preset: "slow", AudioKbps: audioKbpsHigh, MaxWidth: OutputWidth2160p, }, }
QualityPresets maps built-in quality identifiers to presets.
Functions ¶
func ChannelLayoutName ¶
ChannelLayoutName names common speaker layouts.
func DefaultFFmpegTimeout ¶
DefaultFFmpegTimeout returns the default FFmpeg timeout.
func NormalizeOutputWidth ¶
NormalizeOutputWidth returns width if supported, otherwise 1080p.
func OutputWidthLabel ¶
OutputWidthLabel is the UI label for an export width.
func ValidAudioKbps ¶
ValidAudioKbps reports whether kbps is in the allowed AAC range.
func ValidEncoderPreset ¶
ValidEncoderPreset reports whether name is a supported libx264 preset.
func ValidOutputWidth ¶
ValidOutputWidth reports whether width is a supported export width.
Types ¶
type AudioTrack ¶
AudioTrack is one audio stream on a source file.
type ClipQuality ¶
type ClipQuality string
ClipQuality represents a built-in clip quality identifier.
type Clock ¶
type Clock interface {
Format(duration time.Duration) string
Parse(value string) (time.Duration, error)
}
Clock formats and parses FFmpeg time durations.
See https://ffmpeg.org/ffmpeg-utils.html#Time-duration
var DefaultClock Clock = timecode.DefaultClock
DefaultClock is the FFmpeg duration clock.
type CropRect ¶
CropRect is an ffmpeg crop=W:H:X:Y rectangle.
func ParseCropdetect ¶
ParseCropdetect returns the last crop=W:H:X:Y from cropdetect logs.
Parameters:
- output: ffmpeg stderr text from a cropdetect pass.
Returns:
- crop: The last parsed rectangle, or a zero value.
- ok: True when a valid rectangle was found.
type ExecFFmpeg ¶
type ExecFFmpeg struct {
// contains filtered or unexported fields
}
ExecFFmpeg provides FFmpeg execution capabilities.
func NewExecFFmpeg ¶
func NewExecFFmpeg(ffmpegPath, ffprobePath string) *ExecFFmpeg
NewExecFFmpeg creates a new FFmpeg executor.
func (*ExecFFmpeg) DetectCrop ¶
func (execFFmpeg *ExecFFmpeg) DetectCrop( ctx context.Context, input string, start, duration float64, ) (CropRect, error)
DetectCrop samples the source with cropdetect and returns a crop rectangle.
func (*ExecFFmpeg) ExtractClip ¶
func (execFFmpeg *ExecFFmpeg) ExtractClip( ctx context.Context, input, output string, start, duration float64, preset QualityPreset, audioIndex int, rect CropRect, ) error
ExtractClip extracts a clip from a video.
func (*ExecFFmpeg) ExtractGIF ¶
func (execFFmpeg *ExecFFmpeg) ExtractGIF( ctx context.Context, input, output string, start, duration float64, width, fps int, rect CropRect, ) error
ExtractGIF extracts a GIF from a video.
func (*ExecFFmpeg) ExtractPreview ¶
func (execFFmpeg *ExecFFmpeg) ExtractPreview( ctx context.Context, input, output string, start, duration float64, audioIndex int, rect CropRect, preset QualityPreset, ) error
ExtractPreview writes a short, downscaled, browser-safe preview segment.
func (*ExecFFmpeg) ExtractScreenshot ¶
func (execFFmpeg *ExecFFmpeg) ExtractScreenshot( ctx context.Context, input, output string, timestamp float64, rect CropRect, ) error
ExtractScreenshot extracts a screenshot from a video.
func (*ExecFFmpeg) SetTimeout ¶
func (execFFmpeg *ExecFFmpeg) SetTimeout(d time.Duration)
SetTimeout sets the FFmpeg timeout.
type FFmpeg ¶
type FFmpeg interface {
Probe(ctx context.Context, path string) (MediaInfo, error)
ExtractClip(
ctx context.Context,
input, output string,
start, duration float64,
preset QualityPreset,
audioIndex int,
crop CropRect,
) error
DetectCrop(ctx context.Context, input string, start, duration float64) (CropRect, error)
ExtractGIF(
ctx context.Context,
input, output string,
start, duration float64,
width, fps int,
crop CropRect,
) error
ExtractPreview(
ctx context.Context,
input, output string,
start, duration float64,
audioIndex int,
crop CropRect,
preset QualityPreset,
) error
ExtractScreenshot(
ctx context.Context,
input, output string,
timestamp float64,
crop CropRect,
) error
}
FFmpeg provides FFmpeg media processing capabilities.
type FFmpegClock ¶
type FFmpegClock = timecode.FFmpegClock
FFmpegClock is the FFmpeg time-duration clock.
type MediaInfo ¶
type MediaInfo struct {
Duration float64 `json:"duration"`
Width int `json:"width"`
Height int `json:"height"`
VideoCodec string `json:"video_codec"`
AudioCodec string `json:"audio_codec"`
Format string `json:"format"`
BitRate int64 `json:"bit_rate"`
// ColorTransfer is ffprobe color_transfer of the first video stream.
ColorTransfer string `json:"color_transfer"`
AudioTracks []AudioTrack `json:"audio_tracks"`
}
MediaInfo represents media file information.
type QualityPreset ¶
type QualityPreset struct {
CRF int
Preset string
AudioKbps int
MaxWidth int
// WebSafeColor tone-maps HDR to Rec.709 when true.
WebSafeColor bool
}
QualityPreset represents ffmpeg clip encode settings.
func NormalizePreset ¶
func NormalizePreset(preset QualityPreset) QualityPreset
NormalizePreset fills missing or invalid encode settings with Medium.
func ResolvePreset ¶
func ResolvePreset(quality string, lookup func(string) (QualityPreset, bool)) QualityPreset
ResolvePreset maps a stored quality id onto ffmpeg settings.
Lookup is tried first so user-defined profiles win over built-in names.
type Timecode ¶
Timecode is a media timestamp.
func FromSeconds ¶
FromSeconds builds a timecode from a floating-point second count.
Parameters:
- seconds: Timestamp in seconds. Negative, NaN, Inf, and values outside the time.Duration range become zero.
Returns:
- timecode: The timestamp, or a zero value when seconds is not finite.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package crop parses ffmpeg cropdetect rectangles.
|
Package crop parses ffmpeg cropdetect rectangles. |
|
Package progress reports ffmpeg encode completion from stderr.
|
Package progress reports ffmpeg encode completion from stderr. |
|
Package timecode formats and parses FFmpeg time durations.
|
Package timecode formats and parses FFmpeg time durations. |