mirror of
https://github.com/navidrome/navidrome.git
synced 2024-08-27 13:11:06 +02:00
57 lines
1.2 KiB
Go
57 lines
1.2 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) (f io.ReadCloser, err error)
|
|
}
|
|
|
|
func New() Transcoder {
|
|
_, err := exec.LookPath("ffmpeg")
|
|
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)
|
|
cmd := exec.Command(args[0], args[1:]...) // #nosec
|
|
cmd.Stderr = os.Stderr
|
|
if f, err = cmd.StdoutPipe(); err != nil {
|
|
return
|
|
}
|
|
if err = cmd.Start(); err != nil {
|
|
return
|
|
}
|
|
|
|
go func() { _ = cmd.Wait() }() // prevent zombies
|
|
|
|
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.Replace(s, "%s", path, -1)
|
|
s = strings.Replace(s, "%b", strconv.Itoa(maxBitRate), -1)
|
|
split[i] = s
|
|
}
|
|
|
|
return split
|
|
}
|