ffmpeg

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 26, 2019 License: MIT Imports: 13 Imported by: 2

README

go-ffmpeg

Usage

Configuration

Find FFmpeg from your PATH

cfg, _ := ffmpeg.DefaultConfiguration()

Or point to a specific installation of FFmpeg

cfg, _ := ffmpeg.NewConfiguration("/path/to/ffmpeg", "/path/to/ffprobe")
Transcoding
job := cfg.NewJob()
job.AddInputFile("video.mp4")
job.AddOutputFile("out.avi")

statusChan, _ := job.Start(context.Background())
for status := range statusChan {
    switch v := status.(type) {
    case *ffmpeg.Progress:
        log.Printf("%#v", v)
    case *ffmpeg.Done:
        log.Printf("done")
        return
    case *ffmpeg.Error:
        log.Fatalf(v.Error())
    }
}
FFprobe

Whenever an input is added to a job, ffprobe is used to validate it and the result is returned.

metadata, _ := job.AddInputFile("video.mp4")
fmt.Printf("the video is %v seconds long", metadata.Format.Duration)

You can also probe an input first, and then add it to a job.

input, metadata, _ := cfg.Probe("video.mp4")
fmt.Printf("the video is %v seconds long", metadata.Format.Duration)
job.AddInput(input)
Streams (no windows support)
res, _ := http.Get("http://download.blender.org/peach/bigbuckbunny_movies/big_buck_bunny_480p_surround-fix.avi")
job.AddInputReader(res.Body)
file, _ := os.Create("out.mp4")
job.AddOutputWriter(file)
Options

Each of the following methods accept ffmpeg.CliOptions from ffmpeg.Option() and ffmpeg.Flag()

  • Configuration.NewJob()
  • Job.AddInput()
  • Job.AddInputFile()
  • Job.AddInputReader()
  • Job.AddOutputFile()
  • Job.AddOutputWriter()

Need to overwrite a file?

cfg.NewJob(ffmpeg.Flag("-y"))

Need to export using a specific codec and apply a filter?

job.AddInputFile(
    "out.mp4",
    ffmpeg.Option("-codec:v", "libx264"),
    ffmpeg.Option("-filter:v", "scale=640:360"),
)
Debugging

If there a problem with ffmpeg, you can read it's raw output like this:

job.StartDebug(context.Background(), os.Stderr)

Streams on windows

A somewhat convoluted way to get a data stream back from ffmpeg is to setup a http server and output to it.

go http.ListenAndServe(":8080", func(w http.ResponseWriter, r *http.Request) {
    // read output data from r.Body
})

job.AddOutputFile("http://localhost:8080")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CliOption

type CliOption interface {
	// contains filtered or unexported methods
}

CliOption is an command line option for ffmpeg

func Flag

func Flag(name string) CliOption

Flag creates a CliOption for boolean options

func Option

func Option(name, value string) CliOption

Option creates a CliOption

type Configuration

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

Configuration represents valid paths to ffmpeg and ffprobe

func DefaultConfiguration

func DefaultConfiguration() (*Configuration, error)

DefaultConfiguration looks for and returns a configuration from the environment

func NewConfiguration

func NewConfiguration(ffmpeg, ffprobe string) (*Configuration, error)

NewConfiguration validates the given paths to ffmpeg and ffprobe and returns a configuration

func (*Configuration) NewJob

func (c *Configuration) NewJob(options ...CliOption) *Job

NewJob creates a new job

func (*Configuration) Probe added in v0.2.0

func (c *Configuration) Probe(url string) (InputMedia, *Metadata, error)

Probe reads metadata from the url using ffprobe and returns an input media to be added to a job as well as the aformentioned metadata.

func (*Configuration) ProbeReader added in v0.2.0

func (c *Configuration) ProbeReader(r io.Reader) (InputMedia, *Metadata, error)

ProbeReader reads metadata from the input stream using ffprobe and returns an input media to be added to a job as well as the aformentioned metadata.

type Disposition

type Disposition 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"`
}

Disposition represents stream disposition

type Done

type Done struct {
}

Done represents the completion of a job

type Error

type Error struct {
	Arguments []string
	// contains filtered or unexported fields
}

Error represents an error that occurred during a job

type Format

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

Format represents video format

type InputMedia added in v0.2.0

type InputMedia interface {
	// contains filtered or unexported methods
}

InputMedia represents some input media

type Job

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

Job represents an ffmpeg job

func (*Job) AddInput added in v0.2.0

func (j *Job) AddInput(input InputMedia, options ...CliOption)

AddInput adds an input

func (*Job) AddInputFile

func (j *Job) AddInputFile(url string, options ...CliOption) (*Metadata, error)

AddInputFile adds an input file

func (*Job) AddInputReader

func (j *Job) AddInputReader(r io.Reader, options ...CliOption) (*Metadata, error)

AddInputReader adds an input reader

func (*Job) AddOutputFile

func (j *Job) AddOutputFile(file string, options ...CliOption)

AddOutputFile adds an output file

func (*Job) AddOutputReader

func (j *Job) AddOutputReader(w io.Writer, options ...CliOption)

AddOutputReader adds an output reader

func (*Job) Start

func (j *Job) Start(ctx context.Context) (<-chan Status, error)

Start starts the job

func (*Job) StartDebug

func (j *Job) StartDebug(ctx context.Context, w io.Writer) (<-chan Status, error)

StartDebug starts the job and writes all output to w

type Metadata

type Metadata struct {
	Streams []Stream    `json:"streams"`
	Format  *Format     `json:"format"`
	Error   *ProbeError `json:"error"`
}

Metadata represents the output of ffprobe

type OutputMedia added in v0.2.0

type OutputMedia interface {
	// contains filtered or unexported methods
}

OutputMedia represents some output media

type ProbeError added in v0.2.0

type ProbeError struct {
	Code    int64  `json:"code"`
	Message string `json:"string"`
}

ProbeError represents an error emitted by ffprobe

func (*ProbeError) Error added in v0.2.0

func (e *ProbeError) Error() string

Error returns an error message

type Progress

type Progress struct {
	Frame   int64
	Fps     float64
	Time    float64
	Bitrate string
	Speed   float64
}

Progress represents an ffmpeg progress line

type Status

type Status interface {
	// contains filtered or unexported methods
}

Status represents the status of a job

type Stream

type Stream struct {
	Index              int         `json:"index"`
	ID                 string      `json:"id"`
	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"`
	Height             int         `json:"height"`
	CodedWidth         int         `json:"coded_width"`
	CodedHeight        int         `json:"coded_height"`
	HasBFrames         int         `json:"has_b_frames"`
	SampleAspectRatio  string      `json:"sample_aspect_ratio"`
	DisplayAspectRatio string      `json:"display_aspect_ratio"`
	PixFmt             string      `json:"pix_fmt"`
	Level              int         `json:"level"`
	ChromaLocation     string      `json:"chroma_location"`
	Refs               int         `json:"refs"`
	QuarterSample      string      `json:"quarter_sample"`
	DivxPacked         string      `json:"divx_packed"`
	RFrameRrate        string      `json:"r_frame_rate"`
	AvgFrameRate       string      `json:"avg_frame_rate"`
	TimeBase           string      `json:"time_base"`
	DurationTs         int         `json:"duration_ts"`
	Duration           string      `json:"duration"`
	Disposition        Disposition `json:"disposition"`
	BitRate            string      `json:"bit_rate"`
}

Stream represents stream metadata

type Tags

type Tags struct {
	MajorBrand       string `json:"major_brand"`
	MinorVersion     string `json:"minor_version"`
	CompatibleBrands string `json:"compatible_brands"`
	Encoder          string `json:"encoder"`
}

Tags represents format tags

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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