navidrome/engine/transcoder/ffmpeg.go
2020-04-10 10:56:58 -04:00

55 lines
1.3 KiB
Go

package transcoder
import (
"context"
"io"
"os"
"os/exec"
"strconv"
"strings"
"github.com/deluan/navidrome/log"
)
type Transcoder interface {
Start(ctx context.Context, command, path string, maxBitRate int, format string) (f io.ReadCloser, err error)
}
func New() Transcoder {
path, err := exec.LookPath("ffmpeg")
if err != nil {
log.Error("Unable to find ffmpeg", err)
}
log.Debug("Found ffmpeg", "path", path)
return &ffmpeg{}
}
type ffmpeg struct{}
func (ff *ffmpeg) Start(ctx context.Context, command, path string, maxBitRate int, format string) (f io.ReadCloser, err error) {
arg0, args := createTranscodeCommand(command, path, maxBitRate, format)
log.Trace(ctx, "Executing ffmpeg command", "cmd", arg0, "args", args)
cmd := exec.Command(arg0, args...)
cmd.Stderr = os.Stderr
if f, err = cmd.StdoutPipe(); err != nil {
return
}
if err = cmd.Start(); err != nil {
return
}
go cmd.Wait() // prevent zombies
return
}
// Path will always be an absolute path
func createTranscodeCommand(cmd, path string, maxBitRate int, format string) (string, []string) {
split := strings.Split(cmd, " ")
for i, s := range split {
s = strings.Replace(s, "%s", path, -1)
s = strings.Replace(s, "%b", strconv.Itoa(maxBitRate), -1)
split[i] = s
}
return split[0], split[1:]
}