quintodrome/core/transcoder/transcoder.go

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

87 lines
1.8 KiB
Go
Raw Normal View History

2020-02-25 06:01:39 -09:00
package transcoder
import (
"context"
"errors"
"fmt"
"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) (io.ReadCloser, error)
}
2020-02-25 06:01:39 -09:00
func New() Transcoder {
2021-07-21 08:46:30 -08:00
return &externalTranscoder{}
}
2021-07-21 08:46:30 -08:00
type externalTranscoder struct{}
func (e *externalTranscoder) Start(ctx context.Context, command, path string, maxBitRate int) (io.ReadCloser, error) {
args := createTranscodeCommand(command, path, maxBitRate)
2021-07-21 08:46:30 -08:00
log.Trace(ctx, "Executing transcoding command", "cmd", args)
j := &Cmd{ctx: ctx, args: args}
j.PipeReader, j.out = io.Pipe()
err := j.start()
if err != nil {
return nil, err
}
go j.wait()
return j, nil
}
type Cmd struct {
*io.PipeReader
out *io.PipeWriter
ctx context.Context
args []string
cmd *exec.Cmd
}
func (j *Cmd) start() error {
cmd := exec.CommandContext(j.ctx, j.args[0], j.args[1:]...) // #nosec
cmd.Stdout = j.out
cmd.Stderr = os.Stderr
j.cmd = cmd
if err := cmd.Start(); err != nil {
return fmt.Errorf("starting cmd: %w", err)
}
return nil
}
func (j *Cmd) wait() {
if err := j.cmd.Wait(); err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
_ = j.out.CloseWithError(fmt.Errorf("%s exited with non-zero status code: %d", j.args[0], exitErr.ExitCode()))
} else {
_ = j.out.CloseWithError(fmt.Errorf("waiting %s cmd: %w", j.args[0], err))
}
2020-02-25 06:01:39 -09:00
return
}
if j.ctx.Err() != nil {
_ = j.out.CloseWithError(j.ctx.Err())
2020-02-25 06:01:39 -09:00
return
}
_ = j.out.Close()
}
// 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
}