media

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package media turns command-line arguments into inputs and wraps the local audio tools (ffmpeg, ffprobe, sox).

This file (inputs) belongs to the foundation; tools, trimming, capture, and durations live in the other files of the package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Capture

func Capture(ctx context.Context, t Tools, seconds int, device string) (outPath string, cleanup func(), err error)

Capture records seconds of audio from the default input device (or device) into a temporary WAV file, using ffmpeg when installed and sox otherwise. Call cleanup when done with outPath.

Device names by platform (ffmpeg):

macOS    avfoundation audio index or name: "0", ":MacBook Pro Microphone"
Linux    PulseAudio source ("default", "alsa_input.usb-…"), or ALSA as
         "alsa:hw:1,0" / "hw:1,0"; PulseAudio falls back to ALSA
Windows  DirectShow device name: "Microphone (USB Audio)"; the first
         audio device by default

With sox, device is passed as AUDIODEV.

func Duration

func Duration(t Tools, path string) (d time.Duration, ok bool)

Duration returns the length of a local or remote media file using ffprobe. ok is false when ffprobe is missing or cannot tell.

func InstallHint

func InstallHint(tool string) string

InstallHint returns the command that installs tool on this system (ffprobe ships with ffmpeg).

func IsMediaFile

func IsMediaFile(name string) bool

IsMediaFile reports whether name has an audio or video extension.

func MissingTool

func MissingTool(tool, purpose string) *output.Error

MissingTool is the error for a command that needs a tool that is not installed (exit 2, with the install command as the hint).

func Trim

func Trim(ctx context.Context, t Tools, in string, at, dur time.Duration) (outPath string, cleanup func(), err error)

Trim cuts dur of audio starting at at from in (a file path or URL) into a temporary FLAC file. Call cleanup when done with outPath.

Types

type Input

type Input struct {
	Path    string `json:"path,omitempty"`
	URL     string `json:"url,omitempty"`
	IsStdin bool   `json:"stdin,omitempty"`
}

Input is one thing to recognize: a local file, a URL, or audio read from stdin (spooled to a temporary file at Path, with IsStdin set).

func ExpandInputs

func ExpandInputs(args []string, stdin io.Reader) (inputs []Input, batch bool, err error)

ExpandInputs resolves recognize-style arguments:

  • a URL (http/https) or an existing file: a single input;
  • a directory: every audio/video file under it, recursively (hidden files and dirs skipped);
  • a glob ("*.mp3", "music/**/*.flac"): matching audio/video files;
  • "-": stdin, either a list of files/URLs (one per line, # comments) or audio bytes, which are spooled to a temporary file (call Cleanup);
  • several arguments: all of the above, combined.

batch is true for directories, globs, lists, and multiple arguments, even when they resolve to a single file. Duplicate paths are dropped.

func (Input) Cleanup

func (in Input) Cleanup()

Cleanup removes the temporary file holding stdin audio. It does nothing for other inputs.

func (Input) Name

func (in Input) Name() string

Name is how the input is shown to people: the URL, the path, or "stdin".

type Tools

type Tools struct {
	FFmpeg, FFprobe, Sox string
}

Tools holds the absolute paths of the local audio tools, or "" for each one that is not installed.

func Find

func Find() Tools

Find looks up ffmpeg, ffprobe, and sox on PATH.

Jump to

Keyboard shortcuts

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