fix(server): FFmpegPath can contain spaces

This commit is contained in:
Deluan 2024-10-20 13:58:39 -04:00
parent 97c06aba1a
commit 28668782c6
2 changed files with 23 additions and 13 deletions

View File

@ -153,31 +153,27 @@ func (j *ffCmd) wait() {
// Path will always be an absolute path // Path will always be an absolute path
func createFFmpegCommand(cmd, path string, maxBitRate, offset int) []string { func createFFmpegCommand(cmd, path string, maxBitRate, offset int) []string {
split := strings.Split(fixCmd(cmd), " ") var args []string
var parts []string for _, s := range fixCmd(cmd) {
for _, s := range split {
if strings.Contains(s, "%s") { if strings.Contains(s, "%s") {
s = strings.ReplaceAll(s, "%s", path) s = strings.ReplaceAll(s, "%s", path)
parts = append(parts, s) args = append(args, s)
if offset > 0 && !strings.Contains(cmd, "%t") { if offset > 0 && !strings.Contains(cmd, "%t") {
parts = append(parts, "-ss", strconv.Itoa(offset)) args = append(args, "-ss", strconv.Itoa(offset))
} }
} else { } else {
s = strings.ReplaceAll(s, "%t", strconv.Itoa(offset)) s = strings.ReplaceAll(s, "%t", strconv.Itoa(offset))
s = strings.ReplaceAll(s, "%b", strconv.Itoa(maxBitRate)) s = strings.ReplaceAll(s, "%b", strconv.Itoa(maxBitRate))
parts = append(parts, s) args = append(args, s)
} }
} }
return parts return args
} }
func createProbeCommand(cmd string, inputs []string) []string { func createProbeCommand(cmd string, inputs []string) []string {
split := strings.Split(fixCmd(cmd), " ")
var args []string var args []string
for _, s := range fixCmd(cmd) {
for _, s := range split {
if s == "%s" { if s == "%s" {
for _, inp := range inputs { for _, inp := range inputs {
args = append(args, "-i", inp) args = append(args, "-i", inp)
@ -189,7 +185,7 @@ func createProbeCommand(cmd string, inputs []string) []string {
return args return args
} }
func fixCmd(cmd string) string { func fixCmd(cmd string) []string {
split := strings.Split(cmd, " ") split := strings.Split(cmd, " ")
var result []string var result []string
cmdPath, _ := ffmpegCmd() cmdPath, _ := ffmpegCmd()
@ -200,7 +196,7 @@ func fixCmd(cmd string) string {
result = append(result, s) result = append(result, s)
} }
} }
return strings.Join(result, " ") return result
} }
func ffmpegCmd() (string, error) { func ffmpegCmd() (string, error) {
@ -223,6 +219,7 @@ func ffmpegCmd() (string, error) {
return ffmpegPath, ffmpegErr return ffmpegPath, ffmpegErr
} }
// These variables are accessible here for tests. Do not use them directly in production code. Use ffmpegCmd() instead.
var ( var (
ffOnce sync.Once ffOnce sync.Once
ffmpegPath string ffmpegPath string

View File

@ -48,4 +48,17 @@ var _ = Describe("ffmpeg", func() {
Expect(args).To(Equal([]string{"ffmpeg", "-i", "/music library/one.mp3", "-i", "/music library/two.mp3", "-f", "ffmetadata"})) Expect(args).To(Equal([]string{"ffmpeg", "-i", "/music library/one.mp3", "-i", "/music library/two.mp3", "-f", "ffmetadata"}))
}) })
}) })
When("ffmpegPath is set", func() {
It("returns the correct ffmpeg path", func() {
ffmpegPath = "/usr/bin/ffmpeg"
args := createProbeCommand(probeCmd, []string{"one.mp3"})
Expect(args).To(Equal([]string{"/usr/bin/ffmpeg", "-i", "one.mp3", "-f", "ffmetadata"}))
})
It("returns the correct ffmpeg path with spaces", func() {
ffmpegPath = "/usr/bin/with spaces/ffmpeg.exe"
args := createProbeCommand(probeCmd, []string{"one.mp3"})
Expect(args).To(Equal([]string{"/usr/bin/with spaces/ffmpeg.exe", "-i", "one.mp3", "-f", "ffmetadata"}))
})
})
}) })