Skip to content

Commit

Permalink
Merge pull request #1322 from houzin/ken
Browse files Browse the repository at this point in the history
解決: windows os 下,嘗試在當前目錄中查找ffmpeg失敗
  • Loading branch information
iawia002 committed Mar 14, 2024
2 parents bac9923 + 155ff4c commit cb63039
Showing 1 changed file with 21 additions and 2 deletions.
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

0 comments on commit cb63039

Please sign in to comment.