-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.ts
398 lines (354 loc) · 15.2 KB
/
main.ts
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import {app, BrowserWindow, dialog, globalShortcut, ipcMain, shell} from "electron"
import {autoUpdater} from "electron-updater"
import Store from "electron-store"
import * as localShortcut from "electron-shortcuts"
import util from "util"
import child_process from "child_process"
import path from "path"
import ffmpeg from "fluent-ffmpeg"
import process from "process"
import "./dev-app-update.yml"
import pack from "./package.json"
import fs from "fs"
import functions from "./structures/functions"
import Youtube from "youtube.ts"
const exec = util.promisify(child_process.exec)
require("@electron/remote/main").initialize()
ipcMain.handle("paste-loop", async (event) => {
window?.webContents.send("paste-loop")
})
ipcMain.handle("copy-loop", async (event) => {
window?.webContents.send("copy-loop")
})
ipcMain.handle("trigger-paste", async (event) => {
window?.webContents.send("trigger-paste")
})
process.setMaxListeners(0)
let window: Electron.BrowserWindow | null
let ffmpegPath = undefined as any
if (process.platform === "darwin") ffmpegPath = path.join(app.getAppPath(), "../../ffmpeg/ffmpeg.app")
if (process.platform === "win32") ffmpegPath = path.join(app.getAppPath(), "../../ffmpeg/ffmpeg.exe")
if (process.platform === "linux") ffmpegPath = path.join(app.getAppPath(), "../../ffmpeg/ffmpeg")
if (!fs.existsSync(ffmpegPath)) ffmpegPath = undefined
if (ffmpegPath) ffmpeg.setFfmpegPath(ffmpegPath)
autoUpdater.autoDownload = false
const store = new Store()
const youtube = new Youtube()
let filePath = ""
const parseResolution = async (file: string, ffmpegPath?: string) => {
let command = `"${ffmpegPath ? ffmpegPath : "ffmpeg"}" -i "${functions.escapeQuotes(file)}"`
const str = await exec(command).then((s: any) => s.stdout).catch((e: any) => e.stderr)
const dim = str.match(/(?<= )\d+x\d+(?= |,)/)[0].split("x")
return {width: Number(dim[0]), height: Number(dim[1])}
}
ipcMain.handle("resize-window", async (event, videoFile: string) => {
const dim = await parseResolution(videoFile, ffmpegPath)
const {width, height} = functions.constrainDimensions(dim.width, dim.height)
// window?.setSize(width, height, true)
})
ipcMain.handle("mov-to-mp4", async (event, videoFile: string) => {
const baseFlags = ["-pix_fmt", "yuv420p", "-movflags", "+faststart"]
const ext = path.extname(videoFile)
const name = path.basename(videoFile, ext)
const savePath = path.join(app.getAppPath(), `../assets/videos/${name}.mp4`)
if (!fs.existsSync(path.dirname(savePath))) fs.mkdirSync(path.dirname(savePath), {recursive: true})
if (fs.existsSync(savePath)) return savePath
await new Promise<void>((resolve) => {
ffmpeg(path.normalize(videoFile).replaceAll("\\", "/"))
.outputOptions([...baseFlags, "-vcodec", "libx264", "-preset", "ultrafast", "-crf", "16", "-acodec", "copy"])
.save(savePath)
.on("end", () => {
resolve()
})
})
return savePath
})
ipcMain.handle("export-video", async (event, videoFile: string, savePath: string, options: any) => {
let {reverse, speed, preservesPitch, abloop, loopStart, loopEnd, duration} = options
if (videoFile.startsWith("file:///")) videoFile = videoFile.replace("file:///", "")
const baseFlags = ["-pix_fmt", "yuv420p", "-movflags", "+faststart"]
let audioSpeed = preservesPitch ? `atempo=${speed}` : `asetrate=44100*${speed},aresample=44100`
let filter = ["-filter_complex", `[0:v]setpts=${1.0/speed}*PTS${reverse ? ",reverse": ""}[v];[0:a]${audioSpeed}${reverse ? ",areverse" : ""}[a]`, "-map", "[v]", "-map", "[a]"]
let segment = [] as string[]
duration /= speed
if (abloop) {
const start = reverse ? (duration / 100) * (100 - loopStart) : (duration / 100) * loopStart
const end = reverse ? (duration / 100) * (100 - loopEnd) : (duration / 100) * loopEnd
segment = ["-ss", `${reverse ? functions.formatSeconds(end) : functions.formatSeconds(start)}`, "-to", `${reverse ? functions.formatSeconds(start) : functions.formatSeconds(end)}`]
duration = reverse ? start - end : end - start
}
const tempDir = `${path.dirname(savePath)}/temp`
if (!fs.existsSync(tempDir)) fs.mkdirSync(tempDir, {recursive: true})
const tempDest = `${tempDir}/temp${path.extname(savePath)}`
await new Promise<void>((resolve) => {
ffmpeg(path.normalize(videoFile).replaceAll("\\", "/"))
.outputOptions([...baseFlags, ...segment, ...filter])
.save(tempDest)
.on("end", () => {
resolve()
})
.on("progress", (progress) => {
window?.webContents.send("export-progress", {...progress, duration})
})
})
fs.renameSync(tempDest, savePath)
functions.removeDirectory(tempDir)
shell.showItemInFolder(savePath)
})
ipcMain.handle("save-dialog", async (event, defaultPath: string) => {
if (!window) return
const save = await dialog.showSaveDialog(window, {
defaultPath,
filters: [
{name: "All Files", extensions: ["*"]},
{name: "MP4", extensions: ["mp4"]},
{name: "MKV", extensions: ["mkv"]},
{name: "MOV", extensions: ["mov"]},
{name: "AVI", extensions: ["avi"]},
{name: "WEBM", extensions: ["webm"]}
],
properties: ["createDirectory"]
})
return save.filePath ? save.filePath : null
})
ipcMain.handle("trigger-download", async (event, link: string) => {
window?.webContents.send("trigger-download", link)
})
ipcMain.handle("download-yt-video", async (event, link: string) => {
return youtube.util.downloadVideo(link, path.join(app.getAppPath(), "../assets/videos"), {format: "mp4"})
})
ipcMain.handle("open-link", async (event, link: string) => {
window?.webContents.send("open-link", link)
})
ipcMain.handle("show-fx-dialog", async (event) => {
window?.webContents.send("close-all-dialogs", "fx")
window?.webContents.send("show-fx-dialog")
})
ipcMain.handle("show-link-dialog", async (event) => {
window?.webContents.send("close-all-dialogs", "link")
window?.webContents.send("show-link-dialog")
})
ipcMain.handle("next", async (event, videoFile: string) => {
if (videoFile.startsWith("http")) return
if (videoFile.startsWith("file:///")) videoFile = videoFile.replace("file:///", "")
const directory = path.dirname(videoFile)
const files = await functions.getSortedFiles(directory)
const index = files.findIndex((f) => f === path.basename(videoFile))
if (index !== -1) {
if (files[index + 1]) return `file:///${directory}/${files[index + 1]}`
}
return null
})
ipcMain.handle("previous", async (event, videoFile: string) => {
if (videoFile.startsWith("http")) return
if (videoFile.startsWith("file:///")) videoFile = videoFile.replace("file:///", "")
const directory = path.dirname(videoFile)
const files = await functions.getSortedFiles(directory)
const index = files.findIndex((f) => f === path.basename(videoFile))
if (index !== -1) {
if (files[index - 1]) return `file:///${directory}/${files[index - 1]}`
}
return null
})
ipcMain.handle("export-dialog", async (event, visible: boolean) => {
window?.webContents.send("close-all-dialogs", "export")
window?.webContents.send("show-export-dialog", visible)
})
ipcMain.handle("reverse-dialog", async (event, visible: boolean) => {
window?.webContents.send("close-all-dialogs", "reverse")
window?.webContents.send("show-reverse-dialog", visible)
})
ipcMain.handle("get-state", () => {
return store.get("state", {})
})
ipcMain.handle("save-state", (event, newState: any) => {
let state = store.get("state", {}) as object
state = {...state, ...newState}
store.set("state", state)
})
ipcMain.handle("upload-file", () => {
window?.webContents.send("upload-file")
})
ipcMain.handle("extract-subtitles", async (event, videoFile) => {
if (videoFile.startsWith("file:///")) videoFile = videoFile.replace("file:///", "")
const name = path.basename(videoFile, path.extname(videoFile))
const vidDest = path.join(app.getAppPath(), `../assets/subtitles`)
if (!fs.existsSync(vidDest)) fs.mkdirSync(vidDest, {recursive: true})
const newDest = path.join(vidDest, `./${name}.vtt`)
return new Promise<string>((resolve, reject) => {
ffmpeg(path.normalize(videoFile).replaceAll("\\", "/"))
.save(newDest)
.on("end", () => {
resolve(newDest)
})
.on("error", () => reject())
}).catch(() => "")
})
const splitVideo = async (videoFile: string, savePath: string) => {
const baseFlags = ["-pix_fmt", "yuv420p", "-movflags", "+faststart"]
await new Promise<void>((resolve) => {
ffmpeg(path.normalize(videoFile).replaceAll("\\", "/")).outputOptions([...baseFlags, "-acodec", "copy", "-vcodec", "copy", "-f", "segment", "-segment_time", "10", "-reset_timestamps", "1", "-map", "0"])
.save(savePath)
.on("end", () => {
resolve()
})
})
return fs.readdirSync(path.dirname(savePath), {withFileTypes: true}).filter((p) => p.isFile()).map((p) => `${path.dirname(savePath)}/${p.name}`)
}
const reverseSegments = async (segments: string[], savePath: string) => {
const baseFlags = ["-pix_fmt", "yuv420p", "-movflags", "+faststart"]
let queue: string[][] = []
const total = segments.length
while (segments.length) queue.push(segments.splice(0, 2))
let counter = 0
for (let i = 0; i < queue.length; i++) {
window?.webContents.send("reverse-progress", {current: counter, total})
await Promise.all(queue[i].map(async (f) => {
counter++
return new Promise<void>((resolve) => {
ffmpeg(path.normalize(f).replaceAll("\\", "/")).outputOptions([...baseFlags, "-vf", "reverse", "-af", "areverse"])
.save(`${savePath}/${path.basename(f)}`)
.on("end", () => {
resolve()
})
})
}))
}
return fs.readdirSync(savePath, {withFileTypes: true}).filter((p) => p.isFile()).map((p) => `${savePath}/${p.name}`)
}
const concatSegments = async (segments: string[], savePath: string) => {
const baseFlags = ["-pix_fmt", "yuv420p", "-movflags", "+faststart"]
const sorted = segments.sort(new Intl.Collator(undefined, {numeric: true, sensitivity: "base"}).compare).reverse()
const text = sorted.map((s) => `file '${process.platform === "win32" ? "file:" : ""}${s}'`).join("\n")
const textPath = `${path.dirname(savePath)}/list.txt`
fs.writeFileSync(textPath, text)
await new Promise<void>((resolve) => {
ffmpeg(path.normalize(textPath).replaceAll("\\", "/"))
.inputOptions(["-f", "concat", "-safe", "0"])
.outputOptions([...baseFlags, "-c", "copy"])
.save(savePath)
.on("end", () => {
resolve()
})
})
return savePath
}
ipcMain.handle("get-reverse-src", async (event, videoFile: string) => {
const ext = path.extname(videoFile)
const name = path.basename(videoFile, ext)
const vidDest = path.join(app.getAppPath(), `../assets/videos/`)
const newDest = path.join(vidDest, `./${name}_reverse${ext}`)
if (fs.existsSync(newDest)) return newDest
return null
})
ipcMain.handle("reverse-video", async (event, videoFile: string) => {
if (videoFile.startsWith("file:///")) videoFile = videoFile.replace("file:///", "")
const ext = path.extname(videoFile)
const name = path.basename(videoFile, ext)
const vidDest = path.join(app.getAppPath(), `../assets/videos/`)
const newDest = path.join(vidDest, `./${name}_reverse${ext}`)
if (!fs.existsSync(`${vidDest}/segments/reversed`)) fs.mkdirSync(`${vidDest}/segments/reversed`, {recursive: true})
const segments = await splitVideo(videoFile, `${vidDest}/segments/seg%d${ext}`)
const reversedSegments = await reverseSegments(segments, `${vidDest}/segments/reversed`)
const reverseFile = await concatSegments(reversedSegments, newDest)
functions.removeDirectory(`${vidDest}/segments`)
return reverseFile
})
ipcMain.handle("select-file", async () => {
if (!window) return
const files = await dialog.showOpenDialog(window, {
filters: [
{name: "All Files", extensions: ["*"]},
{name: "Video", extensions: ["mp4", "webm", "mkv", "mov", "avi", "m4v"]},
{name: "Audio", extensions: ["mp3", "wav", "ogg"]}
],
properties: ["openFile"]
})
return files.filePaths[0] ? files.filePaths[0] : null
})
ipcMain.handle("get-transparency", () => {
return store.get("transparency", false)
})
ipcMain.handle("save-transparency", (event, transparency: string) => {
store.set("transparency", transparency)
})
ipcMain.handle("get-theme", () => {
return store.get("theme", "light")
})
ipcMain.handle("save-theme", (event, theme: string) => {
store.set("theme", theme)
})
ipcMain.handle("install-update", async (event) => {
if (process.platform === "darwin") {
const update = await autoUpdater.checkForUpdates()
const url = `${pack.repository.url}/releases/download/v${update.updateInfo.version}/${update.updateInfo.files[0].url}`
await shell.openExternal(url)
app.quit()
} else {
await autoUpdater.downloadUpdate()
autoUpdater.quitAndInstall()
}
})
ipcMain.handle("check-for-updates", async (event, startup: boolean) => {
window?.webContents.send("close-all-dialogs", "version")
const update = await autoUpdater.checkForUpdates()
const newVersion = update.updateInfo.version
if (pack.version === newVersion) {
if (!startup) window?.webContents.send("show-version-dialog", null)
} else {
window?.webContents.send("show-version-dialog", newVersion)
}
})
ipcMain.handle("get-opened-file", () => {
if (process.platform !== "darwin") {
return process.argv[1]
} else {
return filePath
}
})
const openFile = (argv?: any) => {
if (process.platform !== "darwin") {
let file = argv ? argv[2] : process.argv[1]
window?.webContents.send("open-file", file)
}
}
app.on("open-file", (event, file) => {
filePath = file
event.preventDefault()
window?.webContents.send("open-file", file)
})
const singleLock = app.requestSingleInstanceLock()
if (!singleLock) {
app.quit()
} else {
app.on("second-instance", (event, argv) => {
if (window) {
if (window.isMinimized()) window.restore()
window.focus()
}
openFile(argv)
})
app.on("ready", () => {
window = new BrowserWindow({width: 900, height: 650, minWidth: 520, minHeight: 250, roundedCorners: false, transparent: true, hasShadow: false, frame: false, backgroundColor: "#00000000", center: true, webPreferences: {nodeIntegration: true, contextIsolation: false, enableRemoteModule: true, webSecurity: false}})
window.loadFile(path.join(__dirname, "index.html"))
window.removeMenu()
openFile()
require("@electron/remote/main").enable(window.webContents)
if (ffmpegPath && process.platform !== "win32") fs.chmodSync(ffmpegPath, "777")
window.on("closed", () => {
window = null
})
localShortcut.register("Ctrl+S", () => {
window?.webContents.send("trigger-download")
}, window, {strict: true})
localShortcut.register("Ctrl+O", () => {
window?.webContents.send("upload-file")
}, window, {strict: true})
if (process.env.DEVELOPMENT === "true") {
globalShortcut.register("Control+Shift+I", () => {
window?.webContents.toggleDevTools()
})
}
})
}
app.allowRendererProcessReuse = false