Repository navigation
Expand file tree
/
Copy pathffmpeg.go
More file actions
132 lines (109 loc) · 3.46 KB
/
Copy pathffmpeg.go
File metadata and controls
132 lines (109 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"os"
"os/exec"
"path/filepath"
goruntime "runtime"
)
// GetBundledFFmpegPath returns the path to bundled ffmpeg binary
// If bundled binary is not found or not executable, returns "ffmpeg" (system PATH)
func GetBundledFFmpegPath() string {
// Get executable path
exePath, err := os.Executable()
if err != nil {
return "ffmpeg" // Fallback to system PATH
}
// Get the directory containing the executable
exeDir := filepath.Dir(exePath)
var ffmpegPath string
switch goruntime.GOOS {
case "darwin":
// On macOS, binaries are in Contents/Resources/ffmpeg/
// The app structure is: FFForge.app/Contents/MacOS/ffforge-desktop
// We want: FFForge.app/Contents/Resources/ffmpeg/ffmpeg
resourceDir := filepath.Join(filepath.Dir(exeDir), "Resources", "ffmpeg")
ffmpegPath = filepath.Join(resourceDir, "ffmpeg")
case "windows":
// On Windows, binaries are in the same directory as the .exe
// or in a ffmpeg subdirectory
ffmpegPath = filepath.Join(exeDir, "ffmpeg", "ffmpeg.exe")
default:
return "ffmpeg" // Unsupported OS, use system PATH
}
// Check if bundled ffmpeg exists and is executable
if _, err := os.Stat(ffmpegPath); err == nil {
if isExecutable(ffmpegPath) {
return ffmpegPath
}
}
// Fallback to system PATH
return "ffmpeg"
}
// GetBundledFFprobePath returns the path to bundled ffprobe binary
// If bundled binary is not found or not executable, returns "ffprobe" (system PATH)
func GetBundledFFprobePath() string {
// Get executable path
exePath, err := os.Executable()
if err != nil {
return "ffprobe" // Fallback to system PATH
}
// Get the directory containing the executable
exeDir := filepath.Dir(exePath)
var ffprobePath string
switch goruntime.GOOS {
case "darwin":
// On macOS, binaries are in Contents/Resources/ffmpeg/
resourceDir := filepath.Join(filepath.Dir(exeDir), "Resources", "ffmpeg")
ffprobePath = filepath.Join(resourceDir, "ffprobe")
case "windows":
// On Windows, binaries are in the same directory as the .exe
// or in a ffmpeg subdirectory
ffprobePath = filepath.Join(exeDir, "ffmpeg", "ffprobe.exe")
default:
return "ffprobe" // Unsupported OS, use system PATH
}
// Check if bundled ffprobe exists and is executable
if _, err := os.Stat(ffprobePath); err == nil {
if isExecutable(ffprobePath) {
return ffprobePath
}
}
// Fallback to system PATH
return "ffprobe"
}
// isExecutable checks if a file exists and is executable
func isExecutable(path string) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
// On Windows, we just check if file exists
if goruntime.GOOS == "windows" {
return !info.IsDir()
}
// On Unix-like systems, check executable permission
return !info.IsDir() && (info.Mode()&0111 != 0)
}
// VerifyFFmpegAvailability checks if ffmpeg and ffprobe are available
// Returns the paths found and any error
func VerifyFFmpegAvailability(ffmpegPath, ffprobePath string) (string, string, error) {
// Try ffmpeg
if _, err := exec.LookPath(ffmpegPath); err != nil {
// If bundled path failed, try system PATH
if systemFFmpeg, err := exec.LookPath("ffmpeg"); err == nil {
ffmpegPath = systemFFmpeg
} else {
return "", "", err
}
}
// Try ffprobe
if _, err := exec.LookPath(ffprobePath); err != nil {
// If bundled path failed, try system PATH
if systemFFprobe, err := exec.LookPath("ffprobe"); err == nil {
ffprobePath = systemFFprobe
} else {
return ffmpegPath, "", err
}
}
return ffmpegPath, ffprobePath, nil
}