Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions utils/ffmpeg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down