From 155ff4cc050e3c1b9ae00cf8fb62ba474d0f6312 Mon Sep 17 00:00:00 2001 From: "ken.hou" Date: Wed, 13 Mar 2024 18:32:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E6=B1=BA:=20windows=20os=20=E4=B8=8B?= =?UTF-8?q?=EF=BC=8C=E5=98=97=E8=A9=A6=E5=9C=A8=E7=95=B6=E5=89=8D=E7=9B=AE?= =?UTF-8?q?=E9=8C=84=E4=B8=AD=E6=9F=A5=E6=89=BEffmpeg=E5=A4=B1=E6=95=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/ffmpeg.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/utils/ffmpeg.go b/utils/ffmpeg.go index 0bca2940b..66f731c52 100644 --- a/utils/ffmpeg.go +++ b/utils/ffmpeg.go @@ -5,10 +5,28 @@ import ( "fmt" "os" "os/exec" + "path/filepath" + "runtime" "github.com/pkg/errors" ) +func findFFmpegExecutable() string { + ffmpegFileName := "ffmpeg" + if runtime.GOOS == "windows" { + ffmpegFileName = "ffmpeg.exe" // 在Windows上添加.exe擴展名 + } + // 嘗試在當前目錄中查找ffmpeg + matches, err := filepath.Glob("./" + ffmpegFileName) + if err == nil && len(matches) > 0 { + // 如果在當前目錄找到了ffmpeg,直接返回這個路徑 + return "./" + ffmpegFileName + } + + // 返回從PATH中找到的ffmpeg路徑 + return ffmpegFileName +} + func runMergeCmd(cmd *exec.Cmd, paths []string, mergeFilePath string) error { var stderr bytes.Buffer cmd.Stderr = &stderr @@ -37,7 +55,8 @@ func MergeFilesWithSameExtension(paths []string, mergedFilePath string) error { cmds = append(cmds, "-i", path) } cmds = append(cmds, "-c:v", "copy", "-c:a", "copy", mergedFilePath) - return runMergeCmd(exec.Command("ffmpeg", cmds...), paths, "") + + return runMergeCmd(exec.Command(findFFmpegExecutable(), cmds...), paths, "") } // MergeToMP4 merges video parts to an MP4 file. @@ -52,7 +71,7 @@ func MergeToMP4(paths []string, mergedFilePath string, filename string) error { mergeFile.Close() // nolint cmd := exec.Command( - "ffmpeg", "-y", "-f", "concat", "-safe", "0", + findFFmpegExecutable(), "-y", "-f", "concat", "-safe", "0", "-i", mergeFilePath, "-c", "copy", "-bsf:a", "aac_adtstoasc", mergedFilePath, ) return runMergeCmd(cmd, paths, mergeFilePath)