-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
336 lines (283 loc) · 8.13 KB
/
main.go
File metadata and controls
336 lines (283 loc) · 8.13 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"strings"
tea "github.com/charmbracelet/bubbletea"
)
const (
Version = "4.0.0"
)
// Config represents the user's saved configuration
type Config struct {
InputDir string `json:"input_dir"`
OutputDir string `json:"output_dir"`
Codec string `json:"codec"`
IPod bool `json:"ipod"`
NoLyrics bool `json:"no_lyrics"`
}
var (
// Command-line flags
inputFlag = flag.String("input", "", "Input directory containing audio files")
outputFlag = flag.String("output", "", "Output directory for converted files")
codecFlag = flag.String("codec", "", "Target codec: flac, alac, aac, wav, mp3, opus")
ipodFlag = flag.Bool("ipod", false, "Enable iPod optimizations")
noLyricsFlag = flag.Bool("no-lyrics", false, "Strip lyrics metadata")
dryRunFlag = flag.Bool("dry-run", false, "Show what would be done without converting")
interactiveFlag = flag.Bool("interactive", false, "Force interactive mode")
versionFlag = flag.Bool("version", false, "Show version information")
)
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("podhnologic v%s\n", Version)
os.Exit(0)
}
// Get config directory
configDir, err := getConfigDir()
if err != nil {
log.Fatalf("Failed to get config directory: %v", err)
}
// Ensure config directory exists
if err := os.MkdirAll(configDir, 0755); err != nil {
log.Fatalf("Failed to create config directory: %v", err)
}
// Load existing config
config := loadConfig(configDir)
// Determine if we should run in interactive mode
interactive := *interactiveFlag || (flag.NFlag() == 0 && len(os.Args) == 1)
if interactive {
// Interactive mode
if err := runInteractive(&config, configDir); err != nil {
log.Fatalf("Interactive mode failed: %v", err)
}
} else {
// Command-line mode: override config with flags
if *inputFlag != "" {
config.InputDir = expandPath(*inputFlag)
}
if *outputFlag != "" {
config.OutputDir = expandPath(*outputFlag)
}
if *codecFlag != "" {
config.Codec = *codecFlag
}
if *ipodFlag {
config.IPod = true
}
if *noLyricsFlag {
config.NoLyrics = true
}
// Validate required fields
if config.InputDir == "" || config.OutputDir == "" {
log.Fatal("--input and --output are required")
}
if config.Codec == "" && !config.IPod {
log.Fatal("--codec or --ipod is required")
}
// Save the config for future use
saveConfig(configDir, config)
}
// Ensure we have ffmpeg (uses embedded binaries)
ffmpegPath, err := ensureFFmpeg(configDir)
if err != nil {
log.Fatalf("Failed to ensure ffmpeg is available: %v", err)
}
// Set default codec for iPod mode
if config.Codec == "" && config.IPod {
config.Codec = "aac"
}
// Run the conversion
if err := runConversion(config, ffmpegPath, *dryRunFlag); err != nil {
log.Fatalf("Conversion failed: %v", err)
}
}
func getConfigDir() (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}
return filepath.Join(homeDir, ".podhnologic"), nil
}
func loadConfig(configDir string) Config {
configPath := filepath.Join(configDir, "config.json")
data, err := os.ReadFile(configPath)
if err != nil {
// Config doesn't exist yet, return empty config
return Config{}
}
var config Config
if err := json.Unmarshal(data, &config); err != nil {
log.Printf("Warning: Failed to parse config file: %v", err)
return Config{}
}
return config
}
func saveConfig(configDir string, config Config) error {
configPath := filepath.Join(configDir, "config.json")
data, err := json.MarshalIndent(config, "", " ")
if err != nil {
return err
}
return os.WriteFile(configPath, data, 0644)
}
func runInteractive(config *Config, configDir string) error {
// Create and run the interactive menu
menu := NewMenuModel(config, configDir)
p := tea.NewProgram(menu, tea.WithAltScreen())
finalModel, err := p.Run()
if err != nil {
return fmt.Errorf("error running menu: %w", err)
}
// Check if user wants to start conversion
if m, ok := finalModel.(menuModel); ok {
if m.shouldStart {
fmt.Println()
printSuccess("Starting conversion...")
return nil
}
}
// User quit without starting
return fmt.Errorf("cancelled by user")
}
func findIndex(items []string, target string) int {
for i, item := range items {
if item == target {
return i
}
}
return 0
}
func trimQuotes(s string) string {
// Remove surrounding single or double quotes
if len(s) >= 2 {
if (s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '\'' && s[len(s)-1] == '\'') {
return s[1 : len(s)-1]
}
}
return s
}
func shortenPath(path string) string {
homeDir, err := os.UserHomeDir()
if err != nil {
return path
}
if strings.HasPrefix(path, homeDir) {
return "~" + strings.TrimPrefix(path, homeDir)
}
return path
}
func expandPath(path string) string {
// Expand ~ to home directory
if strings.HasPrefix(path, "~/") || path == "~" {
homeDir, err := os.UserHomeDir()
if err != nil {
return path
}
if path == "~" {
return homeDir
}
return filepath.Join(homeDir, path[2:])
}
return path
}
func getFFmpegDownloadURL() (string, error) {
os := runtime.GOOS
arch := runtime.GOARCH
// Map of platform/arch to download URLs
// Using static builds from https://github.com/BtbN/FFmpeg-Builds (Linux/Windows)
// and https://evermeet.cx/ffmpeg/ (macOS)
switch os {
case "linux":
if arch == "amd64" {
return "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz", nil
} else if arch == "arm64" {
return "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linuxarm64-gpl.tar.xz", nil
}
case "darwin":
// For macOS, we'll use homebrew or prompt user to install
// For now, return an error suggesting manual installation
return "", fmt.Errorf("macOS: please install ffmpeg via 'brew install ffmpeg'")
case "windows":
if arch == "amd64" {
return "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-win64-gpl.zip", nil
}
}
return "", fmt.Errorf("unsupported platform: %s/%s", os, arch)
}
func ensureFFmpeg(configDir string) (string, error) {
binDir := filepath.Join(configDir, "bin")
localFFmpeg := filepath.Join(binDir, "ffmpeg")
if runtime.GOOS == "windows" {
localFFmpeg += ".exe"
}
// Priority 1: Check if we have embedded binaries and extract them
if hasEmbeddedBinaries() {
// Check if already extracted
if _, err := os.Stat(localFFmpeg); err == nil {
if testFFmpeg(localFFmpeg) == nil {
return localFFmpeg, nil
}
}
// Extract embedded binaries
printInfo("Extracting embedded ffmpeg binaries...")
if err := extractEmbeddedFFmpeg(binDir); err != nil {
fmt.Printf("Warning: Failed to extract embedded binaries: %v\n", err)
// Continue to other methods
} else {
if testFFmpeg(localFFmpeg) == nil {
return localFFmpeg, nil
}
}
}
// Priority 2: Try to find ffmpeg in PATH
if path, err := findInPath("ffmpeg"); err == nil {
if testFFmpeg(path) == nil {
fmt.Printf("Found ffmpeg in PATH: %s\n", path)
return path, nil
}
}
// Priority 3: Check if we already downloaded it
if _, err := os.Stat(localFFmpeg); err == nil {
if testFFmpeg(localFFmpeg) == nil {
return localFFmpeg, nil
}
}
// Priority 4: Download ffmpeg
fmt.Println("FFmpeg not found. Attempting to download...")
downloadURL, err := getFFmpegDownloadURL()
if err != nil {
return "", err
}
if err := downloadAndExtractFFmpeg(downloadURL, binDir); err != nil {
return "", fmt.Errorf("failed to download ffmpeg: %w", err)
}
return localFFmpeg, nil
}
func testFFmpeg(path string) error {
// Test if ffmpeg works by running -version
cmd := execCommand(path, "-version")
return cmd.Run()
}
func findInPath(binary string) (string, error) {
if runtime.GOOS == "windows" {
binary += ".exe"
}
pathEnv := os.Getenv("PATH")
separator := ":"
if runtime.GOOS == "windows" {
separator = ";"
}
for _, dir := range strings.Split(pathEnv, separator) {
fullPath := filepath.Join(dir, binary)
if _, err := os.Stat(fullPath); err == nil {
return fullPath, nil
}
}
return "", fmt.Errorf("%s not found in PATH", binary)
}