stash/pkg/ffmpeg/encoder.go

63 lines
1.2 KiB
Go
Raw Normal View History

2019-02-09 12:30:49 +00:00
package ffmpeg
import (
2019-02-14 23:42:52 +00:00
"github.com/stashapp/stash/pkg/logger"
2019-02-09 12:30:49 +00:00
"io/ioutil"
"os/exec"
2019-03-28 23:29:45 +00:00
"strings"
2019-02-09 12:30:49 +00:00
)
2019-02-10 05:30:54 +00:00
type Encoder struct {
2019-02-09 12:30:49 +00:00
Path string
}
2019-02-10 05:30:54 +00:00
func NewEncoder(ffmpegPath string) Encoder {
return Encoder{
2019-02-09 12:30:49 +00:00
Path: ffmpegPath,
}
}
2019-02-10 05:30:54 +00:00
func (e *Encoder) run(probeResult VideoFile, args []string) (string, error) {
2019-02-09 12:30:49 +00:00
cmd := exec.Command(e.Path, args...)
stderr, err := cmd.StderrPipe()
if err != nil {
logger.Error("FFMPEG stderr not available: " + err.Error())
}
stdout, err := cmd.StdoutPipe()
if nil != err {
logger.Error("FFMPEG stdout not available: " + err.Error())
}
if err = cmd.Start(); err != nil {
return "", err
}
buf := make([]byte, 80)
for {
n, err := stderr.Read(buf)
if n > 0 {
data := string(buf[0:n])
2019-03-28 22:58:13 +00:00
time := GetTimeFromRegex(data)
if time > 0 && probeResult.Duration > 0 {
2019-02-09 12:30:49 +00:00
progress := time / probeResult.Duration
logger.Infof("Progress %.2f", progress)
}
}
if err != nil {
break
}
}
stdoutData, _ := ioutil.ReadAll(stdout)
stdoutString := string(stdoutData)
if err := cmd.Wait(); err != nil {
2019-03-28 23:29:45 +00:00
logger.Errorf("ffmpeg error when running command <%s>", strings.Join(cmd.Args, " "))
2019-02-09 12:30:49 +00:00
return stdoutString, err
}
return stdoutString, nil
}