/
index.js
106 lines (93 loc) · 2.59 KB
/
index.js
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
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const windowStateKeeper = require('electron-window-state')
let mainWindow
let player
const spawn = require('child_process').spawn
const ffmpeg = require('fluent-ffmpeg')
const fs = require('fs')
const userData = app.getPath('userData')
var extract = require('extract-zip')
const createWindow = () => {
let mainWindowState = windowStateKeeper({
defaultWidth: 1200,
defaultHeight: 800
})
mainWindow = new BrowserWindow({
x: mainWindowState.x,
y: mainWindowState.y,
width: mainWindowState.width,
height: mainWindowState.height,
titleBarStyle: 'hidden',
darkTheme: true,
transparent: true,
webPreferences: {
backgroundThrottling: false
}
})
mainWindowState.manage(mainWindow)
mainWindow.loadURL('file://' + __dirname + '/components/app/index.html')
mainWindow.on('closed', () => {
app.stop()
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.stop()
app.quit()
}
})
app.on('activate', () => {
if (mainWindow === null) {
createWindow()
}
})
app.playTrack = file => {
app.stop()
player = spawn(`${userData}/ffmpeg/ffplay`, [file.path, '-nodisp', '-autoexit'], {stdio: 'ignore'})
ffmpeg.setFfprobePath(`${userData}/ffmpeg/ffprobe`)
ffmpeg.ffprobe(file.path, (error, metadata) => {
let duration
if (error) throw error
else if (metadata.format) {
duration = metadata.format.duration
} else duration = false
mainWindow.webContents.executeJavaScript(`window.player.setDuration(${duration})`)
})
player.on('exit', sss)
this.playing = this.cnt = true
this.track = file
}
app.currentTrack = () => this.track
const sss = () => {
if (this.cnt) next()
else app.stop()
}
let next = () => mainWindow.webContents.executeJavaScript('window.player.next()')
app.stop = () => {
if (player) player.removeListener('exit', sss)
if (this.playing) player.kill()
player = this.playing = this.cnt = this.track = null
}
app.pause = () => {
this.playing = false
mainWindow.webContents.executeJavaScript('window.player.pause()')
player.kill('SIGSTOP')
}
app.resume = () => {
this.playing = true
mainWindow.webContents.executeJavaScript('window.player.resume()')
player.kill('SIGCONT')
}
app.toggle = () => {
if (this.playing) app.pause()
else app.resume()
}
if (!fs.existsSync(`${userData}/ff.txt`)) {
extract(__dirname + '/ffmpeg.zip', {dir: userData}, function (err) {
fs.writeFile(`${userData}/ff.txt`, '🍑🍆')
})
}