ff

module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: Apache-2.0

README

ff Go Reference codecov experimental

Helpers for running ffmpeg commands in Go.

Example

You need ffmpeg in PATH to use this package. Alternatively, you can provide path to binary in ffrun.Options.

package main

import (
	"context"
	"fmt"
	"os"
	"path/filepath"

	"github.com/go-faster/errors"

	"github.com/ernado/ff/ffprobe"
	"github.com/ernado/ff/ffrun"
)

func run() error {
	ctx := context.Background()
	i := ffrun.New(ffrun.Options{})
	filePath := filepath.Join("ffrun", "_testdata", "bbb.mp4")

	// Probe.
	probe, err := i.Probe(ctx, filePath)
	if err != nil {
		return errors.Wrap(err, "probe")
	}

	summary, err := ffprobe.ParseSummary(probe)
	if err != nil {
		return errors.Wrap(err, "summary")
	}

	fmt.Println("Duration:", summary.Duration)

	// Encode.
	tempDir, err := os.MkdirTemp("", "output")
	if err != nil {
		return errors.Wrap(err, "temp dir")
	}
	defer func() {
		_ = os.RemoveAll(tempDir)
		fmt.Println("cleaned up")
	}()

	outputPath := filepath.Join(tempDir, "output.mp4")
	if err := i.Run(ctx, ffrun.RunOptions{
		Input:  filePath,
		Output: outputPath,
		Progress: func(p ffrun.Progress) {
			fmt.Println("progress:", p.Complete)
		},
		Args: []string{
			"-t", "5",
			"-ac", "2",
		},
	}); err != nil {
		return errors.Wrap(err, "run")
	}
	fmt.Println("done")
	return nil
}

func main() {
	if err := run(); err != nil {
		_, _ = fmt.Fprintf(os.Stderr, "error: %+v\n", err)
		os.Exit(1)
	}
}

Directories

Path Synopsis
Package ffmpeg simplifies interaction with ffmpeg.
Package ffmpeg simplifies interaction with ffmpeg.
Package ffprobe wraps helpers for working with ffprobe.
Package ffprobe wraps helpers for working with ffprobe.
Package ffrun provides a wrapper to run ffmpeg.
Package ffrun provides a wrapper to run ffmpeg.

Jump to

Keyboard shortcuts

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