ffmpego

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2021 License: BSD-2-Clause Imports: 15 Imported by: 6

README

ffmpego

ffmpego is a Go wrapper around the ffmpeg command for reading and writing videos. It can be used to programmatically manipulate media with a simple, friendly interface.

Usage

Writing a video

To encode a video, create a VideoWriter and write image.Images to it. Here's the simplest possible example of encoding a video:

fps := 24.0
width := 50
height := 50

vw, _ := ffmpego.NewVideoWriter("output.mp4", width, height, fps)

for i := 0; i < 24; i++ {
    // Create your image.
    frame := image.NewGray(image.Rect(0, 0, width, height))

    vw.WriteFrame(frame)
}

vw.Close()

Reading a video

Decoding a video is similarly straightforward. Simply create a VideoReader and read image.Images from it:

vr, _ := NewVideoReader("input.mp4")

for {
    frame, err := vr.ReadFrame()
    if err == io.EOF {
        break
    }
    // Do something with `frame` here...
}

vr.Close()

Installation

This project depends on the ffmpeg command. If you have ffmpeg installed, ffmpego should already work out of the box.

If you do not already have ffmpeg, you can typically install it using your OS's package manager.

Ubuntu:

$ apt install ffmpeg

macOS:

$ brew install ffmpeg

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AudioInfo

type AudioInfo struct {
	// Frequency stores the frequency in Hz.
	Frequency int
}

AudioInfo stores information about an audio file.

func GetAudioInfo

func GetAudioInfo(path string) (info *AudioInfo, err error)

GetAudioInfo gets information about a audio file.

type AudioReader

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

A AudioReader decodes an audio file using ffmpeg.

func NewAudioReader

func NewAudioReader(path string) (*AudioReader, error)

func NewAudioReaderResampled

func NewAudioReaderResampled(path string, frequency int) (*AudioReader, error)

NewAudioReaderResampled creates an AudioReader that automatically changes the input frequency.

func (*AudioReader) AudioInfo

func (a *AudioReader) AudioInfo() *AudioInfo

AudioInfo gets information about the current video.

func (*AudioReader) Close

func (a *AudioReader) Close() error

Close stops the decoding process and closes all associated files.

func (*AudioReader) ReadSamples

func (a *AudioReader) ReadSamples(out []float64) (int, error)

ReadSamples reads up to len(samples) from the file.

Returns the number of samples actually read, along with an error if one was encountered.

If fewer samples than len(out) are read, an error must be returned. At the end of decoding, io.EOF is returned.

type AudioWriter

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

An AudioWriter encodes an audio file using ffmpeg.

func NewAudioWriter

func NewAudioWriter(path string, frequency int) (*AudioWriter, error)

NewAudioWriter creates a AudioWriter which is encoding mono-channel audio to the given file.

func (*AudioWriter) Close

func (v *AudioWriter) Close() error

Close closes the audio file and waits for encoding to complete.

func (*AudioWriter) WriteSamples

func (v *AudioWriter) WriteSamples(samples []float64) error

WriteSamples writes audio samples to the file.

The samples should be in the range [-1, 1].

type ChildPipeStream

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

A ChildPipeStream uses a pipe to communicate with subprocesses.

This is not supported on Windows.

func NewChildPipeStream

func NewChildPipeStream(reading bool) (*ChildPipeStream, error)

NewChildPipeStream creates a ChildPipeStream.

If the reading flag is true, then the stream should be read from. Otherwise it should be written to.

func (*ChildPipeStream) Cancel

func (c *ChildPipeStream) Cancel() error

func (*ChildPipeStream) Connect

func (c *ChildPipeStream) Connect() (io.ReadWriteCloser, error)

func (*ChildPipeStream) ExtraFiles

func (c *ChildPipeStream) ExtraFiles() []*os.File

func (*ChildPipeStream) ResourceURL

func (c *ChildPipeStream) ResourceURL() string

type ChildSocketStream

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

A ChildSocketStream uses a TCP socket to communicate with subprocesses.

This should be supported on all operating systems, but some may prevent process from listening on sockets.

func NewChildSocketStream

func NewChildSocketStream() (*ChildSocketStream, error)

NewChildSocketStream creates a ChildSocketStream.

func (*ChildSocketStream) Cancel

func (c *ChildSocketStream) Cancel() error

func (*ChildSocketStream) Connect

func (c *ChildSocketStream) Connect() (io.ReadWriteCloser, error)

func (*ChildSocketStream) ExtraFiles

func (c *ChildSocketStream) ExtraFiles() []*os.File

func (*ChildSocketStream) ResourceURL

func (c *ChildSocketStream) ResourceURL() string

type ChildStream

type ChildStream interface {
	// ExtraFiles returns the files that should be passed
	// to the executed command in order for it to be able
	// to access the stream.
	ExtraFiles() []*os.File

	// ResourceURL gets the URL or filename that the child
	// process can use to access this stream.
	//
	// It is intended to be passed as a CLI option.
	ResourceURL() string

	// Connect should be called once the sub-process is
	// running. If successful, it will return an object
	// that maps to the subprocess.
	//
	// While the return value is a ReadWriter, only either
	// Read or Write should be used.
	//
	// After Connect() is called, you needn't call Cancel()
	// on the ChildStream, but must call Close() on the
	// returned io.ReadWriteCloser.
	Connect() (io.ReadWriteCloser, error)

	// Cancel disposes of resources in this process
	// associated with the stream.
	// This is only intended to be used if Connect()
	// cannot be called.
	Cancel() error
}

A ChildStream is a connection to an ffmpeg process. Typically a ChildStream can only be used for either reading or writing, but not both.

If you create a ChildStream, you must either call Connect() or Cancel() in order to properly dispose of system resources.

Pass the ExtraFiles() to the executed command to ensure that it can access the stream.

func CreateChildStream

func CreateChildStream(reading bool) (ChildStream, error)

CreateChildStream creates a ChildStream suitable for use on the current operating system.

If the reading flag is true, then the stream should be read from. Otherwise it should be written to.

type VideoInfo

type VideoInfo struct {
	Width  int
	Height int
	FPS    float64
}

VideoInfo is information about an encoded video.

func GetVideoInfo

func GetVideoInfo(path string) (info *VideoInfo, err error)

GetVideoInfo gets information about a video file.

type VideoReader

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

A VideoReader decodes a video file using ffmpeg.

func NewVideoReader

func NewVideoReader(path string) (*VideoReader, error)

func NewVideoReaderResampled

func NewVideoReaderResampled(path string, fps float64) (*VideoReader, error)

NewVideoReaderResampled creates a VideoReader that automatically changes the input frame rate.

func (*VideoReader) Close

func (v *VideoReader) Close() error

Close stops the decoding process and closes all associated files.

func (*VideoReader) ReadFrame

func (v *VideoReader) ReadFrame() (image.Image, error)

ReadFrame reads the next frame from the video.

If the video is finished decoding, nil will be returned along with io.EOF.

func (*VideoReader) VideoInfo

func (v *VideoReader) VideoInfo() *VideoInfo

VideoInfo gets information about the current video.

type VideoWriter

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

A VideoWriter encodes a video file using ffmpeg.

func NewVideoWriter

func NewVideoWriter(path string, width, height int, fps float64) (*VideoWriter, error)

NewVideoWriter creates a VideoWriter which is encoding to the given file.

func NewVideoWriterWithAudio

func NewVideoWriterWithAudio(path string, width, height int, fps float64, audioFile string) (*VideoWriter, error)

NewVideoWriterWithAudio creates a VideoWriter which copies audio from an existing video or audio file.

func (*VideoWriter) Close

func (v *VideoWriter) Close() error

Close closes the video file and waits for encoding to complete.

func (*VideoWriter) WriteFrame

func (v *VideoWriter) WriteFrame(img image.Image) error

WriteFrame adds a frame to the current video.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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