quintodrome/core/transcoder/ffmpeg.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.2 KiB
Go
Raw Normal View History

2020-02-25 06:01:39 -09:00
package transcoder
import (
"context"
"io"
"os"
"os/exec"
"strconv"
"strings"
"github.com/navidrome/navidrome/log"
)
2020-02-25 06:01:39 -09:00
type Transcoder interface {
Start(ctx context.Context, command, path string, maxBitRate int) (f io.ReadCloser, err error)
}
2020-02-25 06:01:39 -09:00
func New() Transcoder {
2020-10-06 13:24:16 -08:00
_, err := exec.LookPath("ffmpeg")
2020-04-10 05:36:26 -08:00
if err != nil {
log.Error("Unable to find ffmpeg", err)
}
return &ffmpeg{}
}
type ffmpeg struct{}
func (ff *ffmpeg) Start(ctx context.Context, command, path string, maxBitRate int) (f io.ReadCloser, err error) {
args := createTranscodeCommand(command, path, maxBitRate)
log.Trace(ctx, "Executing ffmpeg command", "cmd", args)
2020-04-26 08:53:36 -08:00
cmd := exec.Command(args[0], args[1:]...) // #nosec
cmd.Stderr = os.Stderr
if f, err = cmd.StdoutPipe(); err != nil {
2020-02-25 06:01:39 -09:00
return
}
if err = cmd.Start(); err != nil {
2020-02-25 06:01:39 -09:00
return
}
2020-04-26 08:35:26 -08:00
go func() { _ = cmd.Wait() }() // prevent zombies
2020-02-25 06:01:39 -09:00
return
}
// Path will always be an absolute path
func createTranscodeCommand(cmd, path string, maxBitRate int) []string {
split := strings.Split(cmd, " ")
for i, s := range split {
s = strings.ReplaceAll(s, "%s", path)
s = strings.ReplaceAll(s, "%b", strconv.Itoa(maxBitRate))
split[i] = s
}
return split
}