-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
168 lines (146 loc) · 7.83 KB
/
server.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
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
const path = require('path')
const fs = require('fs-extra')
const chalk = require('chalk')
const express = require('express')
const config = require('./config.json')
const { spawn, exec } = require('child_process')
const ui = express()
ui.use(express.json())
ui.use(express.urlencoded({ extended: true }))
ui.use('/static', express.static('resources'))
// Checks for FFMPEG and GI-Cutscenes
exec(`${config.prerequisites.ffmpeg == 'path' ? 'ffmpeg': config.prerequisites.ffmpeg} -version`,
{ shell: 'powershell.exe' }, async (stdout, stderr, error) => {
const output = (stderr != 'Error: Command failed: ffmpeg -version' ? stderr : null)
if (output) {
const version = output.match(/[0-9]{4}-[0-9]{2}-[0-9]{2}(-git-.{10})?/i)[0]
console.log('[' + chalk.greenBright('Success') + '] Found FFMPEG binary in ' + chalk.grey('%path%') + ', version ' + chalk.grey(version))
}
else {
console.log('[' + chalk.redBright('Error') + '] Couldn\'t find FFMPEG binary in ' + chalk.grey('%path%'))
process.exit(1)
}
}
)
exec(`${config.prerequisites.gicutscenes ? config.prerequisites.gicutscenes : '.\GICutscenes.exe'} --version`,
{ shell: 'powershell.exe' }, async (stdout, stderr, error) => {
const output = (stderr ? stderr : null)
if (output) {
console.log('[' + chalk.greenBright('Success') + '] Found GI-Cutscenes binary, version ' + chalk.grey(output))
initUI()
}
else {
console.log('[' + chalk.redBright('Error') + '] Couldn\'t find GI-Cutscenes binary.')
process.exit(1)
}
}
)
// Init variables that will be used later
const filesList = []
let filesToDemux = []
let optAudioTrack = ''
let optSubtitlesLang = ''
let optFFmpegArgs = ''
function initUI() {
ui.listen(2809, async () => {
console.log(chalk.greenBright('UI started!'))
console.log(('Access ' + chalk.grey('http://localhost:2809') + ' on your browser to get started.\n'))
})
ui.get('/', async (request, response) => {
response.send(
fs.readFileSync('ui/index.html')
.toString()
.replace('$outdir', config.output)
.replace('$ffmpegflags', config.defaultFFMPEGFlags)
.replace(/\$usminpdir/g, config.optionUSMFilesDir)
)
})
ui.get('/getinputfiles', async (request, response) => {
const inputFilesDirectory = fs.readdirSync(config.optionUSMFilesDir).filter(f => f.endsWith('.usm'))
inputFilesDirectory.forEach(f => { filesList.push(`${path.join(config.optionUSMFilesDir, f)}`) })
response.send({ status: 'success', data: inputFilesDirectory.length, files: inputFilesDirectory })
})
ui.post('/start', async (request, response) => {
optAudioTrack = request.body['audioTrack']
optSubtitlesLang = request.body['subtitles']
optFFmpegArgs = request.body['ffmpegArgs']
response.sendStatus(200)
filesToDemux = fs.readdirSync(config.optionUSMFilesDir).filter(f => f.endsWith('.usm'))
await _callbackDemuxer(0, filesToDemux)
})
}
async function demuxIndividualFile(file, indexCounter) {
const cutsceneName = file.replace('.usm', '')
exec(`cd ${config.prerequisites.gicutscenes.replace('\\GICutscenes.exe', '')};${config.prerequisites.gicutscenes} demuxUsm "${path.join(config.optionUSMFilesDir, file)}" -s -m -nc -o "${config.output}"`, { shell: 'powershell.exe' }, async function (stdout, stderr, error) {
if(error && error != '') {
console.log('[' + chalk.redBright('Demuxer') + '] Failed to extract ' + chalk.grey(file) + '\n ' + chalk.grey(error))
const id = indexCounter + 1
return await _callbackDemuxer(id, filesToDemux)
}
// fs.existsSync(path.join(config.output, '/Subs')) ? fs.readdirSync(path.join(config.output, '/Subs')).filter(f => f.endsWith('.ass')) : null
const hasSubtitles = fs.existsSync(path.join(config.output, `/Subs/${cutsceneName}_EN.ass`)) ? true : false
fs.readdirSync(config.output).filter(f => f.endsWith('.hca') || f.endsWith('.mkv')).forEach(async f => {
fs.unlinkSync(path.join(config.output, f))
})
console.log('[' + chalk.greenBright('Demuxer') + '] Finished extracting ' + chalk.grey(file))
const audioFile = `${cutsceneName}_${optAudioTrack}.wav`
const videoFile = `${cutsceneName}.ivf`
const subFile = hasSubtitles ? `${cutsceneName}_${optSubtitlesLang}.ass` : 'none'
fs.moveSync(
path.join(config.output, audioFile),
path.join(config.output, `./temp/${audioFile}`)
)
fs.readdirSync(config.output).filter(f => f.endsWith('.wav') || f.endsWith('.mkv')).forEach(async f => {
fs.unlinkSync(path.join(config.output, f))
})
if(hasSubtitles) {
fs.moveSync(
path.join(config.output, `./Subs/${subFile}`),
path.join(config.output, `./temp/${subFile}`)
)
fs.readdirSync(path.join(config.output, '/Subs')).forEach(f => {
fs.unlinkSync(path.join(config.output, `/Subs/${f}`))
})
}
fs.moveSync(
path.join(config.output, `${videoFile}`),
path.join(config.output, `./temp/${videoFile}`)
)
fs.moveSync(path.join(config.output, `./temp/${audioFile}`), path.join(config.output, `${audioFile}`))
fs.moveSync(path.join(config.output, `./temp/${videoFile}`), path.join(config.output, `${videoFile}`))
if(hasSubtitles) {
fs.moveSync(path.join(config.output, `./temp/${subFile}`), path.join(config.output, `${subFile}`))
}
const ffmpegCommand = `cd ${config.output}; ffmpeg -i "${audioFile}" -i "${videoFile}"${optFFmpegArgs ? ` ${optFFmpegArgs}` : ''}${hasSubtitles ? ` -vf subtitles="${subFile}":fontsdir="./fonts":force_style="FontName=SDK_JP_Web"` : ''} "${cutsceneName}.mp4"`
console.log('[' + chalk.greenBright('FFMPEG') + '] Started ffmpeg process' + `\n └─ Audio: ${chalk.grey(audioFile)}\n └─ Video: ${chalk.grey(videoFile)}\n └─ Subtitles: ${chalk.grey(subFile)}\n └─ Output: ${chalk.grey(cutsceneName + '.mp4')}\n └─ Flags: ${chalk.grey(optFFmpegArgs ? optFFmpegArgs : '(none)')}`)
const startDate = Date.now()
exec(
ffmpegCommand,
{ shell: 'powershell.exe' }, async function(stdout, stderr, error) {
// prototype error detection - migrating to fluent-ffmpeg soon:tm:
let success = true
if(error.trim().split('\n')[error.trim().split('\n').length - 1].includes('Error')) success = false
if(!success) {
console.log('\n[' + chalk.redBright('FFMPEG') + '] An exception occured. Detailed logs below.')
console.log(error)
}
else {
console.log('\n[' + chalk.greenBright('FFMPEG') + '] Finished task after ' + chalk.italic(chalk.grey(`${(Date.now() - startDate) / 1000}s\n`)))
}
fs.unlinkSync(path.join(config.output, audioFile))
fs.unlinkSync(path.join(config.output, videoFile))
if(hasSubtitles) fs.unlinkSync(path.join(config.output, subFile))
const id = indexCounter + 1
await _callbackDemuxer(id, filesToDemux)
}
)
})
}
async function _callbackDemuxer(index, list) {
if(index >= list.length) {
console.log('[' + chalk.greenBright('Demuxer') + '] Finished!')
return exec(`explorer ${config.output}`)
}
console.log('[' + chalk.greenBright('Demuxer') + '] Attempting to extract ' + chalk.grey(list[index]))
await demuxIndividualFile(list[index], index)
}