Initial work on downsampling

The http connection is being closed before sending all data. May have something to do with the Range header
This commit is contained in:
Deluan
2016-03-04 13:12:56 -05:00
parent 9a246b5432
commit 7225807bad
10 changed files with 186 additions and 29 deletions
+47
View File
@@ -0,0 +1,47 @@
package stream
import (
"github.com/astaxie/beego"
"io"
"os"
"os/exec"
"strconv"
"strings"
)
func Stream(path string, bitRate int, maxBitRate int, w io.Writer) error {
if maxBitRate > 0 && bitRate > maxBitRate {
cmdLine, args := createDownsamplingCommand(path, maxBitRate)
cmd := exec.Command(cmdLine, args...)
beego.Debug("Executing cmd:", cmdLine, args)
cmd.Stdout = w
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
beego.Error("Error executing", cmdLine, ":", err)
}
return err
} else {
f, err := os.Open(path)
if err != nil {
beego.Error("Error opening file", path, ":", err)
return err
}
_, err = io.Copy(w, f)
return err
}
}
func createDownsamplingCommand(path string, maxBitRate int) (string, []string) {
cmd := beego.AppConfig.String("downsampleCommand")
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:len(split)]
}
+29
View File
@@ -0,0 +1,29 @@
package stream
import (
. "github.com/deluan/gosonic/tests"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestDownsampling(t *testing.T) {
Init(t, false)
Convey("Subject: createDownsamplingCommand", t, func() {
Convey("It should create a valid command line", func() {
cmd, args := createDownsamplingCommand("/music library/file.mp3", 128)
So(cmd, ShouldEqual, "ffmpeg")
So(args[0], ShouldEqual, "-i")
So(args[1], ShouldEqual, "/music library/file.mp3")
So(args[2], ShouldEqual, "-b:a")
So(args[3], ShouldEqual, "128k")
So(args[4], ShouldEqual, "mp3")
So(args[5], ShouldEqual, "-")
})
})
}