Skip to content

Commit 3f4989f

Browse files
committed
[feat]: batch changes
1 parent e09d44d commit 3f4989f

File tree

2 files changed

+59
-18
lines changed

2 files changed

+59
-18
lines changed

Sources/ScriptToolkit/FileExtension.swift

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,57 +146,86 @@ public extension File {
146146
// MARK: - Audio Processing
147147

148148
@discardableResult public func slowDownAudio(newName: String, percent: Float, overwrite: Bool = true) throws -> File {
149-
if !overwrite && FileManager.default.fileExists(atPath: newName) { return try File(path: newName) }
149+
if FileManager.default.fileExists(atPath: newName) {
150+
if !overwrite { return try File(path: newName) }
151+
try FileManager.default.removeItem(atPath: newName)
152+
}
153+
150154
run(ScriptToolkit.soxPath, path, newName, "tempo", "-s", String(percent))
151155
return try File(path: newName)
152156
}
153157

154158
@discardableResult public func convertToWav(newName: String, overwrite: Bool = true) throws -> File {
155-
if !overwrite && FileManager.default.fileExists(atPath: newName) { return try File(path: newName) }
159+
if FileManager.default.fileExists(atPath: newName) {
160+
if !overwrite { return try File(path: newName) }
161+
try FileManager.default.removeItem(atPath: newName)
162+
}
163+
156164
run(ScriptToolkit.ffmpegPath, "-i", path, "-sample_rate", "44100", newName.deletingPathExtension + ".wav")
157165
return try File(path: newName)
158166
}
159167

160168
@discardableResult public func convertToM4A(newName: String, overwrite: Bool = true) throws -> File {
161-
if !overwrite && FileManager.default.fileExists(atPath: newName) { return try File(path: newName) }
169+
if FileManager.default.fileExists(atPath: newName) {
170+
if !overwrite { return try File(path: newName) }
171+
try FileManager.default.removeItem(atPath: newName)
172+
}
173+
162174
run(ScriptToolkit.ffmpegPath, "-i", path, "-sample_rate", "44100", newName.deletingPathExtension + ".m4a")
163175
return try File(path: newName)
164176
}
165177

166178
@discardableResult public func addSilence(newName: String, overwrite: Bool = true) throws -> File {
167-
if !overwrite && FileManager.default.fileExists(atPath: newName) { return try File(path: newName) }
179+
if FileManager.default.fileExists(atPath: newName) {
180+
if !overwrite { return try File(path: newName) }
181+
try FileManager.default.removeItem(atPath: newName)
182+
}
183+
168184
run(ScriptToolkit.soxPath, ScriptToolkit.silenceFilePath, path, newName)
169185
return try File(path: newName)
170186
}
171187

172-
func prepareSongForPractise(outputFolder: Folder) throws {
188+
func prepareSongForPractise(outputFolder: Folder, overwrite: Bool = true) throws {
189+
let inputFolder = parent!
190+
191+
let fileName75 = nameExcludingExtension + "@75.m4a"
192+
let fileName90 = nameExcludingExtension + "@90.m4a"
193+
let fileName100 = nameExcludingExtension + "@100.m4a"
194+
195+
if !overwrite {
196+
let outputPath = outputFolder.path
197+
if FileManager.default.fileExists(atPath: outputPath.appendingPathComponent(path: fileName75))
198+
&& FileManager.default.fileExists(atPath: outputPath.appendingPathComponent(path: fileName90))
199+
&& FileManager.default.fileExists(atPath: outputPath.appendingPathComponent(path: fileName100)) { return }
200+
}
201+
173202
print(name + ":")
174203

175204
let originalWavFile: File
176205
if let ext = `extension`, ext.lowercased() != "wav" {
177206
print(" Converting to wav")
178-
originalWavFile = try convertToWav(newName: "wav-file.wav")
207+
originalWavFile = try convertToWav(newName: inputFolder.path.appendingPathComponent(path: "wav-file.wav"))
179208
}
180209
else {
181210
originalWavFile = self
182211
}
183212

184213
print(" Converting to 75% speed")
185-
let file75 = try originalWavFile.slowDownAudio(newName: "tempo-75.wav", percent: 0.75)
214+
let file75 = try originalWavFile.slowDownAudio(newName: inputFolder.path.appendingPathComponent(path: "tempo-75.wav"), percent: 0.75)
186215
print(" Converting to 90% speed")
187-
let file90 = try originalWavFile.slowDownAudio(newName: "tempo-90.wav", percent: 0.9)
216+
let file90 = try originalWavFile.slowDownAudio(newName: inputFolder.path.appendingPathComponent(path: "tempo-90.wav"), percent: 0.9)
188217

189218
print(" Adding initial silence to 75% speed file")
190-
let silencedFile75 = try file75.addSilence(newName: "silence-75.wav")
219+
let silencedFile75 = try file75.addSilence(newName: inputFolder.path.appendingPathComponent(path: inputFolder.path.appendingPathComponent(path: "silence-75.wav")))
191220
print(" Adding initial silence to 90% speed file")
192-
let silencedFile90 = try file90.addSilence(newName: "silence-90.wav")
221+
let silencedFile90 = try file90.addSilence(newName: inputFolder.path.appendingPathComponent(path: inputFolder.path.appendingPathComponent(path: "silence-90.wav")))
193222
print(" Adding initial silence to 100% speed file")
194-
let silencedFile100 = try originalWavFile.addSilence(newName: "silence-100.wav")
223+
let silencedFile100 = try originalWavFile.addSilence(newName: inputFolder.path.appendingPathComponent(path: inputFolder.path.appendingPathComponent(path: "silence-100.wav")))
195224

196225
print(" Converting to M4A")
197-
let silencedM4AFile75 = try silencedFile75.convertToM4A(newName: nameExcludingExtension + "@75.m4a")
198-
let silencedM4AFile90 = try silencedFile90.convertToM4A(newName: nameExcludingExtension + "@90.m4a")
199-
let silencedM4AFile100 = try silencedFile100.convertToM4A(newName: nameExcludingExtension + "@100.m4a")
226+
let silencedM4AFile75 = try silencedFile75.convertToM4A(newName: inputFolder.path.appendingPathComponent(path: fileName75))
227+
let silencedM4AFile90 = try silencedFile90.convertToM4A(newName: inputFolder.path.appendingPathComponent(path: fileName90))
228+
let silencedM4AFile100 = try silencedFile100.convertToM4A(newName: inputFolder.path.appendingPathComponent(path: fileName100))
200229

201230
// Move result to output dir
202231
print(" Moving to destination folder")
@@ -217,7 +246,10 @@ public extension File {
217246
// MARK: - Video Processing
218247

219248
@discardableResult public func reduceVideo(newName: String, overwrite: Bool = true) throws -> File {
220-
if !overwrite && FileManager.default.fileExists(atPath: newName) { return try File(path: newName) }
249+
if FileManager.default.fileExists(atPath: newName) {
250+
if !overwrite { return try File(path: newName) }
251+
try FileManager.default.removeItem(atPath: newName)
252+
}
221253

222254
run(ScriptToolkit.ffmpegPath, "-i", path, "-vf", "scale=iw/2:ih/2", newName)
223255
return try File(path: newName)
@@ -227,13 +259,22 @@ public extension File {
227259
// MARK: - PDF
228260

229261
@discardableResult public func cropPDF(newName: String, insets: NSEdgeInsets, overwrite: Bool = true) throws -> File {
230-
if !overwrite && FileManager.default.fileExists(atPath: newName) { return try File(path: newName) }
262+
if FileManager.default.fileExists(atPath: newName) {
263+
if !overwrite { return try File(path: newName) }
264+
try FileManager.default.removeItem(atPath: newName)
265+
}
266+
231267
let left = Int(insets.left)
232268
let top = Int(insets.top)
233269
let bottom = Int(insets.bottom)
234270
let right = Int(insets.right)
235271

236-
run(ScriptToolkit.pdfCropPath, "--margins", "\(left) \(top) \(right) \(bottom)", path, newName)
272+
main.currentdirectory = parent!.path
273+
let result = run(bash: "\(ScriptToolkit.pdfCropPath) --margins '5 5 5 5' \"\(name)\" \"\(newName)\"")
274+
275+
// let result = run(ScriptToolkit.pdfCropPath, "--margins", "\(left) \(top) \(right) \(bottom)", path, newName)
276+
print(result)
277+
//runAndDebug(ScriptToolkit.pdfCropPath, "--margins", "\(left) \(top) \(right) \(bottom)", path, newName)
237278
return try File(path: newName)
238279
}
239280

Sources/ScriptToolkit/ScriptToolkit.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public struct ScriptToolkit {
7373

7474
@discardableResult public func runAndDebug(_ executable: String, _ args: Any ..., combineOutput: Bool = false) -> RunOutput {
7575
let stringargs = args.map(String.init(describing:))
76-
print(executable, String(describing: stringargs.joined(separator: " ")))
76+
print(executable, String(describing: stringargs.joined(separator: " ")))
7777
return run(executable, args, combineOutput: combineOutput)
7878
}
7979

0 commit comments

Comments
 (0)