package pkg import ( "errors" "fmt" "os/exec" ) // FFmpeg describes how to invoke ffmpeg. Args are prepended to the arguments // passed by ytcat, which allows ffmpeg to be launched through a wrapper such as // nix. type FFmpeg struct { Command string Args []string } // ResolveFFmpeg determines how to invoke ffmpeg. // // The order is: // 1. the executable given by the -ffmpeg flag, if set; it must exist // 2. ffmpeg from PATH // 3. ffmpeg from nix, if nix is available func ResolveFFmpeg(configured string) (FFmpeg, error) { if configured != "" { path, err := exec.LookPath(configured) if err != nil { return FFmpeg{}, fmt.Errorf("configured ffmpeg %q is not available: %w", configured, err) } return FFmpeg{Command: path}, nil } if path, err := exec.LookPath("ffmpeg"); err == nil { return FFmpeg{Command: path}, nil } return ffmpegFromNix() } // ffmpegFromNix runs ffmpeg straight out of nixpkgs so that ytcat does not // require ffmpeg in the user or system environment. It only applies when nix is // installed: on systems without nix the lookup fails and the caller reports // that ffmpeg is missing. func ffmpegFromNix() (FFmpeg, error) { nix, err := exec.LookPath("nix") if err != nil { return FFmpeg{}, errors.New("ffmpeg not found in PATH and nix is not available; install ffmpeg or pass -ffmpeg") } return FFmpeg{ Command: nix, Args: []string{ "--extra-experimental-features", "nix-command flakes", "shell", "nixpkgs#ffmpeg", "-c", "ffmpeg", }, }, nil }