-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContentView.swift
379 lines (302 loc) · 15.5 KB
/
ContentView.swift
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
//
// ContentView.swift
// download yt
//
// Created by user on 06.01.24.
//
import AppKit // output location folder picker
import Foundation // execute commands in zsh
import SwiftUI
/* TODO:
- add Cancel download button that kills background thread (Button must not be affected by UI disabled)
- add ProgressBar which parses output of yt-dlp to get % completion and shows it next to download button
*/
// set folder of this program (lives under Documents/YTDL and contains a folder called bin that stores ffmpeg and yt-dlp
let ytdlRoot = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("YTDL")
let ytdlBin = ytdlRoot.appendingPathComponent("bin")
struct ContentView: View {
// target URL
@State private var url: String = ""
// filetype of output (default mkv)
@State private var filetype = UserDefaults.standard.string(forKey: "filetype") ?? "mkv"
// output folder URL (default ~/Documents/YTDL)
@State private var outputFolderPath: URL? = UserDefaults.standard.url(forKey: "outputFolderPath") ?? ytdlRoot
// keep track of whether download is currently active or not
@State private var isDownloading : Bool = false
// download success popup shown
@State private var isDownloadSuccessAlertPresented = false
// at startup create folders ~/Documents/YTDL and ~/Documents/YTDL/bin if they do not exist already
init() {
createYTDLFolderIfNeeded()
}
var body: some View {
VStack {
// url
HStack {
Spacer().frame(width: 4) // icon sizes vary, improves overall alignment
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("URL:\t\t\t\t")
TextField("Paste your URL here", text: $url)
// paste button
Button(action: {
// set url to clipboard content
if let clipboardContent = NSPasteboard.general.string(forType: .string) {
url = clipboardContent
}
}) {
Image(systemName: "doc.on.clipboard")
.imageScale(.medium)
.foregroundStyle(.tint) // Optional: Set the image color
}
}
Spacer().frame(height: 24) // evenly spaced vertical space between the rows
// output format
HStack {
Spacer().frame(width: 3)
Image(systemName: "gearshape")
.imageScale(.large)
.foregroundStyle(.tint)
Picker("Output Format:\t\t", selection: $filetype) {
ForEach(["mp3", "opus", "mkv"], id: \.self) {
Text($0)
}
}
// horizontal picker style
.pickerStyle(SegmentedPickerStyle())
// when user selects different filetype, store it in userdefaults
.onChange(of: filetype) { newValue in
UserDefaults.standard.set(newValue, forKey: "filetype")
}
// when app is started, set last use filetype from userdefaults (default is mkv)
.onAppear {
if let storedFileType = UserDefaults.standard.string(forKey: "filetype") {
filetype = storedFileType
}
}
}
// output folder
HStack {
Image(systemName: "folder")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Output Folder:\t ")
Button("Choose Folder") {
// open folder picker (no files allowed)
let panel = NSOpenPanel()
panel.canChooseFiles = false
panel.canChooseDirectories = true
panel.allowsMultipleSelection = false
// display the folder picker panel
panel.begin { response in
if response == NSApplication.ModalResponse.OK {
outputFolderPath = panel.urls.first
// also store URL in userdefaults
if let outputFolderPath = outputFolderPath {
UserDefaults.standard.set(outputFolderPath, forKey: "outputFolderPath")
}
}
}
}
.padding()
.onAppear {
// load last URL from UserDefaults at app startup
if let storedOutputFolderPath = UserDefaults.standard.url(forKey: "outputFolderPath") {
outputFolderPath = storedOutputFolderPath
}
}
if let outputFolderPath = outputFolderPath {
Text("\(outputFolderPath.path)")
.padding()
}
}
.frame(maxWidth: .infinity, alignment: .leading) // fixes swiftui bug? where its not aligned left otherwise
// start download button
HStack {
Button("Start Download") {
// if URL is empty dont do anything
if url == "" {
return
}
// TODO: check if folder is valid choice
// every background task runs on background thread so that UI is not blocked
DispatchQueue.global(qos: .background).async {
// disable UI
isDownloading = true
// download ffmpeg, unpack it and make it executable
downloadBinary(url: "https://evermeet.cx/ffmpeg/ffmpeg-113169-ge1c1dc8347.zip"){
executeZshCommand("chmod +x ffmpeg")
}
// download yt-dlp, make it executable and execute rest of commands
downloadBinary(url: "https://github.com/yt-dlp/yt-dlp/releases/download/2023.12.30/yt-dlp_macos"){
executeZshCommand("chmod +x yt-dlp")
}
// update yt-dlp (every few months this will trigger and fix broken downloads)
executeZshCommand("./yt-dlp --update")
// if URL field not-empty, determine download command (depends on filetype chosen) and start download
if url.count > 0 {
var downloadCommand = ""
switch filetype {
case "mp3":
downloadCommand = "./yt-dlp -f bestaudio --extract-audio --audio-format mp3 --audio-quality 0 --ignore-errors --yes-playlist -o '\(outputFolderPath!.path)/%(channel)s - %(title)s.%(ext)s' \"\(url)\" --ffmpeg-location \(ytdlBin.path)"
case "opus":
downloadCommand = "./yt-dlp -f bestaudio --extract-audio --audio-format opus --remux-video opus --audio-quality 0 --ignore-errors --yes-playlist -o '\(outputFolderPath!.path)/%(channel)s - %(title)s.%(ext)s' \"\(url)\" --ffmpeg-location \(ytdlBin.path)"
case "mkv":
downloadCommand = "./yt-dlp -f 'bestvideo[ext=mkv]+bestaudio[ext=m4a]/best[ext=mkv]/best' --ignore-errors --yes-playlist -o '\(outputFolderPath!.path)/%(channel)s - %(title)s.%(ext)s' \"\(url)\" --ffmpeg-location \(ytdlBin.path) --recode-video mkv"
default:
print("This can't possibly happen.")
}
// start download
executeZshCommand(downloadCommand)
// show success dialog when download completes
isDownloadSuccessAlertPresented.toggle()
}
// enable UI again
isDownloading = false
} // end of Background Thread
} // end of Button Download action
.alert(isPresented: $isDownloadSuccessAlertPresented) {
// notify user that all downloads have been completed
Alert(
title: Text("Download Success"),
message: Text("All downloads completed successfully."),
dismissButton: .default(Text("Ok"))
)
}
} // end of Button Download HStack
.frame(maxWidth: .infinity, alignment: .leading)
// open download location button
Button("Open Folder") { // Open Output Folder
// open folder in Finder
if NSWorkspace.shared.open(outputFolderPath!) {
print("Output folder opened successfully")
} else {
print("Failed to open output folder")
}
}
.padding(.top, 14) // add slightly more vertical space between the buttons
.frame(maxWidth: .infinity, alignment: .leading)
HStack {
// show "Downloading, please wait..." while download is active
if isDownloading {
// show animated loading icon
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .blue))
.scaleEffect(0.7)
.padding()
Text("Downloading, please wait...")
}
}
} // end of VStack
.padding()
.disabled(isDownloading) // disable UI elements while download is active
// note: no exceptions are possible, but it is cleaner than copypasting this line to each element that should be disabled..
/*
// Cancel Download Button
if isDownloading {
HStack {
Button("Cancel Download") {
// kill background thread (seems to require lots of work to get this function...)
}
}
}
*/
}
// unzips file at specified location (required for ffmpeg.zip)
func unzipFile(at url: URL) {
let task = Process()
task.launchPath = "/usr/bin/unzip"
task.arguments = ["-o", url.path, "-d", ytdlBin.path] // -o makes it unzip without confirmation, -d specifies output destination
task.launch()
task.waitUntilExit()
print("File unzipped successfully at: \(ytdlBin)ffmpeg")
}
// executeZshCommand expects a zsh command as input string and then executes that command
func executeZshCommand(_ command: String) {
let task = Process()
task.launchPath = "/bin/zsh"
task.arguments = ["-c", command]
task.currentDirectoryPath = ytdlBin.path
print("----\nStarting to execute zsh command: \(command)\n")
task.launch()
task.waitUntilExit()
print("Zsh successfully executed.\n----")
}
// downloads binaries (ffmpeg or yt-dlp), but only if it does not exist already
func downloadBinary(url: String, completion: @escaping () -> Void) {
// bool value that determines whether we are currently downloading ffmpeg or yt-dlp
// true: downloading ffmpeg, false: downloading yt-dlp
let downloadingFfmpeg = url.hasPrefix("https://evermeet.cx/ffmpeg/")
// signal that notifies us when download is finished
let semaphore = DispatchSemaphore(value: 0)
// set output name of file that was downloaded (will be .zip for ffmpeg, but no file ending for yt-dlp)
let destinationURL = downloadingFfmpeg ? ytdlBin.appendingPathComponent("ffmpeg.zip") : ytdlBin.appendingPathComponent("yt-dlp")
let binaryURL = downloadingFfmpeg ? ytdlBin.appendingPathComponent("ffmpeg") : ytdlBin.appendingPathComponent("yt-dlp")
// check if the binaries exists already
let fileManager = FileManager.default
let binaryExistsAlready = fileManager.fileExists(atPath: binaryURL.path)
if binaryExistsAlready {
if downloadingFfmpeg {
print("Ffmpeg exists already, skipping download..")
return
} else {
print("Yt-dlp exists already, skipping download..")
return
}
}
// prepare download
let urlToDownload = URL(string: url)!
let task = URLSession.shared.downloadTask(with: urlToDownload) { localURL, _, error in
if let localURL = localURL {
do {
try FileManager.default.moveItem(at: localURL, to: destinationURL)
print("File downloaded successfully at: \(destinationURL)")
// ffmpeg is only offered in zipped form, so unzip it
if downloadingFfmpeg {
unzipFile(at: destinationURL)
}
// signal that download etc is now complete
semaphore.signal()
} catch {
print("Error moving file: \(error)")
}
} else if let error = error {
print("Download failed with error: \(error)")
// download failed but avoid deadlock but signaling completion
semaphore.signal()
}
}
// start download
task.resume()
// wait for download to finish
semaphore.wait()
// ok now continue with other code
completion()
}
// create YTDL folder in Documents if it does not exist already
private func createYTDLFolderIfNeeded() {
// check if the YTDL folder exists, and create it if not
if !FileManager.default.fileExists(atPath: ytdlRoot.path) {
// create root folder YTDL
do {
try FileManager.default.createDirectory(at: ytdlRoot, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Error creating YTDL root folder: \(error)")
exit(0) // cancel program execution, nothing will work anyways if this fails
}
// then create bin folder inside
do {
try FileManager.default.createDirectory(at: ytdlBin, withIntermediateDirectories: true, attributes: nil)
} catch {
print("Error creating YTDL bin folder: \(error)")
exit(0)
}
}
}
}
/*
#Preview {
ContentView()
}
*/