ffmpeg

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2022 License: AGPL-3.0 Imports: 24 Imported by: 1

Documentation

Index

Constants

View Source
const (
	Mp4                Container  = "mp4"
	M4v                Container  = "m4v"
	Mov                Container  = "mov"
	Wmv                Container  = "wmv"
	Webm               Container  = "webm"
	Matroska           Container  = "matroska"
	Avi                Container  = "avi"
	Flv                Container  = "flv"
	Mpegts             Container  = "mpegts"
	Aac                AudioCodec = "aac"
	Mp3                AudioCodec = "mp3"
	Opus               AudioCodec = "opus"
	Vorbis             AudioCodec = "vorbis"
	MissingUnsupported AudioCodec = ""
	Mp4Ffmpeg          string     = "mov,mp4,m4a,3gp,3g2,mj2" // browsers support all of them
	M4vFfmpeg          string     = "mov,mp4,m4a,3gp,3g2,mj2" // so we don't care that ffmpeg
	MovFfmpeg          string     = "mov,mp4,m4a,3gp,3g2,mj2" // can't differentiate between them
	WmvFfmpeg          string     = "asf"
	WebmFfmpeg         string     = "matroska,webm"
	MatroskaFfmpeg     string     = "matroska,webm"
	AviFfmpeg          string     = "avi"
	FlvFfmpeg          string     = "flv"
	MpegtsFfmpeg       string     = "mpegts"
	H264               string     = "h264"
	H265               string     = "h265" // found in rare cases from a faulty encoder
	Hevc               string     = "hevc"
	Vp8                string     = "vp8"
	Vp9                string     = "vp9"
	Mkv                string     = "mkv" // only used from the browser to indicate mkv support
	Hls                string     = "hls" // only used from the browser to indicate hls support
	MimeWebm           string     = "video/webm"
	MimeMkv            string     = "video/x-matroska"
	MimeMp4            string     = "video/mp4"
	MimeHLS            string     = "application/vnd.apple.mpegurl"
	MimeMpegts         string     = "video/MP2T"
)
View Source
const CopyStreamCodec = "copy"

Variables

View Source
var CodecH264 = Codec{
	Codec: "libx264",

	MimeType: MimeMp4,
	// contains filtered or unexported fields
}
View Source
var CodecHEVC = Codec{
	Codec: "libx265",

	MimeType: MimeMp4,
	// contains filtered or unexported fields
}
View Source
var CodecHLS = Codec{
	Codec: "libx264",

	MimeType: MimeMpegts,
	// contains filtered or unexported fields
}
View Source
var CodecMKVAudio = Codec{
	Codec: CopyStreamCodec,

	MimeType: MimeMkv,
	// contains filtered or unexported fields
}

it is very common in MKVs to have just the audio codec unsupported copy the video stream, transcode the audio and serve as Matroska

View Source
var CodecVP8 = Codec{
	Codec: "libvpx",

	MimeType: MimeWebm,
	// contains filtered or unexported fields
}
View Source
var CodecVP9 = Codec{
	Codec: "libvpx-vp9",

	MimeType: MimeWebm,
	// contains filtered or unexported fields
}

ContainerToFfprobe maps user readable container strings to ffprobe's format_name. On some formats ffprobe can't differentiate

View Source
var DefaultSupportedCodecs = []string{H264, H265}

only support H264 by default, since Safari does not support VP8/VP9

View Source
var ErrUnsupportedFormat = errors.New("unsupported image format")
View Source
var FrameRegex = regexp.MustCompile(`frame=\s*([0-9]+)`)
View Source
var TimeRegex = regexp.MustCompile(`time=\s*(\d+):(\d+):(\d+.\d+)`)

Functions

func Download

func Download(ctx context.Context, configDirectory string) error

func DownloadSingle added in v0.4.0

func DownloadSingle(ctx context.Context, configDirectory, url string) error

func GetFrameFromRegex

func GetFrameFromRegex(str string) int

func GetPaths

func GetPaths(paths []string) (string, string)

func GetTimeFromRegex

func GetTimeFromRegex(str string) float64

func IsStreamable added in v0.3.0

func IsStreamable(videoCodec string, audioCodec AudioCodec, container Container) bool

func IsValidAudio added in v0.2.0

func IsValidAudio(audio AudioCodec, validCodecs []AudioCodec) bool

func IsValidAudioForContainer added in v0.2.0

func IsValidAudioForContainer(audio AudioCodec, format Container) bool

func IsValidCodec

func IsValidCodec(codecName string, supportedCodecs []string) bool

func IsValidCombo added in v0.2.0

func IsValidCombo(codecName string, format Container, supportedVideoCodecs []string) bool

IsValidCombo checks if a codec/container combination is valid. Returns true on validity, false otherwise

func IsValidForContainer added in v0.2.0

func IsValidForContainer(format Container, validContainers []Container) bool

func KillRunningEncoders

func KillRunningEncoders(path string)

func WriteHLSPlaylist added in v0.3.0

func WriteHLSPlaylist(probeResult VideoFile, baseUrl string, w io.Writer)

Types

type AudioCodec added in v0.2.0

type AudioCodec string

type Codec added in v0.3.0

type Codec struct {
	Codec string

	MimeType string
	// contains filtered or unexported fields
}

type Container added in v0.2.0

type Container string

func MagicContainer added in v0.2.0

func MagicContainer(filePath string) Container

MagicContainer returns the container type of a file path. Returns the zero-value on errors or no-match. Implements mkv or webm only, as ffprobe can't distinguish between them and not all browsers support mkv

func MatchContainer added in v0.2.0

func MatchContainer(format string, filePath string) Container

type Encoder

type Encoder string

func (*Encoder) CopyVideo added in v0.2.0

func (e *Encoder) CopyVideo(probeResult VideoFile, options TranscodeOptions)

CopyVideo will copy the video stream as is, and drop the audio stream.

func (*Encoder) GetTranscodeStream added in v0.3.0

func (e *Encoder) GetTranscodeStream(options TranscodeStreamOptions) (*Stream, error)

func (*Encoder) ImageThumbnail added in v0.11.0

func (e *Encoder) ImageThumbnail(image *bytes.Buffer, format *string, maxDimensions int, path string) ([]byte, error)

func (*Encoder) SceneMarkerImage

func (e *Encoder) SceneMarkerImage(probeResult VideoFile, options SceneMarkerOptions) error

func (*Encoder) SceneMarkerVideo

func (e *Encoder) SceneMarkerVideo(probeResult VideoFile, options SceneMarkerOptions) error

func (*Encoder) ScenePreviewVideoChunk

func (e *Encoder) ScenePreviewVideoChunk(probeResult VideoFile, options ScenePreviewChunkOptions, preset string, fallback bool) error

func (*Encoder) ScenePreviewVideoChunkCombine

func (e *Encoder) ScenePreviewVideoChunkCombine(probeResult VideoFile, concatFilePath string, outputPath string) error

func (*Encoder) ScenePreviewVideoToImage

func (e *Encoder) ScenePreviewVideoToImage(probeResult VideoFile, width int, videoPreviewPath string, outputPath string) error

func (*Encoder) Screenshot

func (e *Encoder) Screenshot(probeResult VideoFile, options ScreenshotOptions) error

func (*Encoder) SpriteScreenshot added in v0.7.0

func (e *Encoder) SpriteScreenshot(probeResult VideoFile, options SpriteScreenshotOptions) (image.Image, error)

func (*Encoder) SpriteScreenshotSlow added in v0.13.0

func (e *Encoder) SpriteScreenshotSlow(probeResult VideoFile, options SpriteScreenshotOptions) (image.Image, error)

SpriteScreenshotSlow uses the select filter to get a single frame from a videofile instead of seeking It is very slow and should only be used for files with very small duration in secs / frame count

func (*Encoder) Transcode

func (e *Encoder) Transcode(probeResult VideoFile, options TranscodeOptions)

func (*Encoder) TranscodeAudio added in v0.2.0

func (e *Encoder) TranscodeAudio(probeResult VideoFile, options TranscodeOptions)

TranscodeAudio will copy the video stream as is, and transcode audio.

func (*Encoder) TranscodeVideo added in v0.2.0

func (e *Encoder) TranscodeVideo(probeResult VideoFile, options TranscodeOptions)

TranscodeVideo transcodes the video, and removes the audio. In some videos where the audio codec is not supported by ffmpeg, ffmpeg fails if you try to transcode the audio

type FFProbe added in v0.11.0

type FFProbe string

FFProbe

func (*FFProbe) GetReadFrameCount added in v0.13.0

func (f *FFProbe) GetReadFrameCount(vf *VideoFile) (int64, error)

GetReadFrameCount counts the actual frames of the video file

func (*FFProbe) NewVideoFile added in v0.11.0

func (f *FFProbe) NewVideoFile(videoPath string, stripExt bool) (*VideoFile, error)

Execute exec command and bind result to struct.

type FFProbeJSON

type FFProbeJSON struct {
	Format struct {
		BitRate        string `json:"bit_rate"`
		Duration       string `json:"duration"`
		Filename       string `json:"filename"`
		FormatLongName string `json:"format_long_name"`
		FormatName     string `json:"format_name"`
		NbPrograms     int    `json:"nb_programs"`
		NbStreams      int    `json:"nb_streams"`
		ProbeScore     int    `json:"probe_score"`
		Size           string `json:"size"`
		StartTime      string `json:"start_time"`
		Tags           struct {
			CompatibleBrands string          `json:"compatible_brands"`
			CreationTime     models.JSONTime `json:"creation_time"`
			Encoder          string          `json:"encoder"`
			MajorBrand       string          `json:"major_brand"`
			MinorVersion     string          `json:"minor_version"`
			Title            string          `json:"title"`
			Comment          string          `json:"comment"`
		} `json:"tags"`
	} `json:"format"`
	Streams []FFProbeStream `json:"streams"`
	Error   struct {
		Code   int    `json:"code"`
		String string `json:"string"`
	} `json:"error"`
}

type FFProbeStream

type FFProbeStream struct {
	AvgFrameRate       string `json:"avg_frame_rate"`
	BitRate            string `json:"bit_rate"`
	BitsPerRawSample   string `json:"bits_per_raw_sample,omitempty"`
	ChromaLocation     string `json:"chroma_location,omitempty"`
	CodecLongName      string `json:"codec_long_name"`
	CodecName          string `json:"codec_name"`
	CodecTag           string `json:"codec_tag"`
	CodecTagString     string `json:"codec_tag_string"`
	CodecTimeBase      string `json:"codec_time_base"`
	CodecType          string `json:"codec_type"`
	CodedHeight        int    `json:"coded_height,omitempty"`
	CodedWidth         int    `json:"coded_width,omitempty"`
	DisplayAspectRatio string `json:"display_aspect_ratio,omitempty"`
	Disposition        struct {
		AttachedPic     int `json:"attached_pic"`
		CleanEffects    int `json:"clean_effects"`
		Comment         int `json:"comment"`
		Default         int `json:"default"`
		Dub             int `json:"dub"`
		Forced          int `json:"forced"`
		HearingImpaired int `json:"hearing_impaired"`
		Karaoke         int `json:"karaoke"`
		Lyrics          int `json:"lyrics"`
		Original        int `json:"original"`
		TimedThumbnails int `json:"timed_thumbnails"`
		VisualImpaired  int `json:"visual_impaired"`
	} `json:"disposition"`
	Duration          string `json:"duration"`
	DurationTs        int    `json:"duration_ts"`
	HasBFrames        int    `json:"has_b_frames,omitempty"`
	Height            int    `json:"height,omitempty"`
	Index             int    `json:"index"`
	IsAvc             string `json:"is_avc,omitempty"`
	Level             int    `json:"level,omitempty"`
	NalLengthSize     string `json:"nal_length_size,omitempty"`
	NbFrames          string `json:"nb_frames"`
	NbReadFrames      string `json:"nb_read_frames"`
	PixFmt            string `json:"pix_fmt,omitempty"`
	Profile           string `json:"profile"`
	RFrameRate        string `json:"r_frame_rate"`
	Refs              int    `json:"refs,omitempty"`
	SampleAspectRatio string `json:"sample_aspect_ratio,omitempty"`
	StartPts          int    `json:"start_pts"`
	StartTime         string `json:"start_time"`
	Tags              struct {
		CreationTime models.JSONTime `json:"creation_time"`
		HandlerName  string          `json:"handler_name"`
		Language     string          `json:"language"`
		Rotate       string          `json:"rotate"`
	} `json:"tags"`
	TimeBase      string `json:"time_base"`
	Width         int    `json:"width,omitempty"`
	BitsPerSample int    `json:"bits_per_sample,omitempty"`
	ChannelLayout string `json:"channel_layout,omitempty"`
	Channels      int    `json:"channels,omitempty"`
	MaxBitRate    string `json:"max_bit_rate,omitempty"`
	SampleFmt     string `json:"sample_fmt,omitempty"`
	SampleRate    string `json:"sample_rate,omitempty"`
}

type SceneMarkerOptions

type SceneMarkerOptions struct {
	ScenePath  string
	Seconds    int
	Width      int
	OutputPath string
	Audio      bool
}

type ScenePreviewChunkOptions

type ScenePreviewChunkOptions struct {
	StartTime  float64
	Duration   float64
	Width      int
	OutputPath string
	Audio      bool
}

type ScreenshotOptions

type ScreenshotOptions struct {
	OutputPath string
	Quality    int
	Time       float64
	Width      int
	Verbosity  string
}

type SpriteScreenshotOptions added in v0.7.0

type SpriteScreenshotOptions struct {
	Time  float64
	Frame int
	Width int
}

type Stream added in v0.3.0

type Stream struct {
	Stdout  io.ReadCloser
	Process *os.Process
	// contains filtered or unexported fields
}

func (*Stream) Serve added in v0.3.0

func (s *Stream) Serve(w http.ResponseWriter, r *http.Request)

type TranscodeOptions

type TranscodeOptions struct {
	OutputPath       string
	MaxTranscodeSize models.StreamingResolutionEnum
}

type TranscodeStreamOptions added in v0.3.0

type TranscodeStreamOptions struct {
	ProbeResult      VideoFile
	Codec            Codec
	StartTime        string
	MaxTranscodeSize models.StreamingResolutionEnum
	// transcode the video, remove the audio
	// in some videos where the audio codec is not supported by ffmpeg
	// ffmpeg fails if you try to transcode the audio
	VideoOnly bool
}

func GetTranscodeStreamOptions added in v0.3.0

func GetTranscodeStreamOptions(probeResult VideoFile, videoCodec Codec, audioCodec AudioCodec) TranscodeStreamOptions

type VideoFile

type VideoFile struct {
	JSON        FFProbeJSON
	AudioStream *FFProbeStream
	VideoStream *FFProbeStream

	Path         string
	Title        string
	Comment      string
	Container    string
	Duration     float64
	StartTime    float64
	Bitrate      int64
	Size         int64
	CreationTime time.Time

	VideoCodec   string
	VideoBitrate int64
	Width        int
	Height       int
	FrameRate    float64
	Rotation     int64
	FrameCount   int64

	AudioCodec string
}

func (*VideoFile) GetAudioStream

func (v *VideoFile) GetAudioStream() *FFProbeStream

func (*VideoFile) GetVideoStream

func (v *VideoFile) GetVideoStream() *FFProbeStream

func (*VideoFile) SetTitleFromPath

func (v *VideoFile) SetTitleFromPath(stripExtension bool)

Jump to

Keyboard shortcuts

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