allow custom codec flags to ffmpeg, output stderr

This commit is contained in:
tocariimaa 2024-12-31 13:37:59 -03:00
parent c1f35def47
commit d14376431e

View file

@ -2,6 +2,7 @@ package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"os"
@ -24,24 +25,35 @@ func makeConcatDemuxFile(manifest UgoiraManifest, frameDescFile *os.File) {
frameDescFile.WriteString(fmt.Sprintf("file '%s'\n", lastFrame.File))
}
func callFFmpeg(frameDescPath, outputFilePath, workDir string) error {
// TODO: allow custom ffmpeg flags
const ffmpegArgs = `-hide_banner
-loglevel warning
-f concat -i %s
-an
-fps_mode vfr
-q:v 10
-c:v libvpx-vp9
-b:v 1M
-lossless 1 %s`
func callFFmpeg(frameDescPath, outputFilePath, workDir, ffmpegArgs string) error {
const ffmpegBaseArgs = `-hide_banner
-loglevel warning
-f concat -i %s
-fps_mode vfr`
// Default codec arguments, for a VP9 WebM
const ffmpegDefaultArgs = `-an
-q:v 10
-c:v libvpx-vp9
-b:v 1M
-crf 5 %s`
cmdArgs := fmt.Sprintf(ffmpegArgs, frameDescPath, outputFilePath)
var ffmpegArgsTemp string
if len(ffmpegArgs) > 0 {
ffmpegArgsTemp = ffmpegBaseArgs + " " + ffmpegArgs
} else {
ffmpegArgsTemp = ffmpegBaseArgs + " " + ffmpegDefaultArgs
}
cmdArgs := fmt.Sprintf(ffmpegArgsTemp, frameDescPath, outputFilePath)
cmdArgsSplit := strings.Fields(cmdArgs)
ffmpegProc := exec.Command("ffmpeg", cmdArgsSplit...)
var stderr bytes.Buffer
ffmpegProc.Stderr = &stderr
ffmpegProc.Dir = workDir
if err := ffmpegProc.Run(); err != nil {
fmt.Fprintf(os.Stderr, "FFmpeg stderr:\n%s\n", stderr.String())
if exitErr, ok := err.(*exec.ExitError); ok {
return fmt.Errorf("ffmpeg exited with non zero code: %d", exitErr.ExitCode())
}
@ -86,7 +98,7 @@ func unpackUgoira(ugoiraPath, outDir string) error {
return nil
}
func ugoira2video(manifest UgoiraManifest, ugoiraFileName, vidOut string) error {
func ugoira2video(manifest UgoiraManifest, ugoiraFileName, vidOut, ffmpegArgs string) error {
tdir, err := os.MkdirTemp("", "u2vwd-")
if err != nil {
return err
@ -108,7 +120,7 @@ func ugoira2video(manifest UgoiraManifest, ugoiraFileName, vidOut string) error
return fmt.Errorf("Could not unpack Ugoira: %w", err)
}
makeConcatDemuxFile(manifest, fraFp)
if err := callFFmpeg(fraFp.Name(), vidOutAbs, tdir); err != nil {
if err := callFFmpeg(fraFp.Name(), vidOutAbs, tdir, ffmpegArgs); err != nil {
return err
}
return nil