65 lines
1.4 KiB
JavaScript
65 lines
1.4 KiB
JavaScript
|
import os from 'os'
|
||
|
import { EventEmitter } from 'events'
|
||
|
import { spawn, execSync } from 'child_process'
|
||
|
|
||
|
export default class Encoder extends EventEmitter {
|
||
|
constructor(opts = {}) {
|
||
|
super()
|
||
|
Object.assign(this, {
|
||
|
encoding: false,
|
||
|
program: 'ffmpeg.exe',
|
||
|
options: ['--help'],
|
||
|
processor: null,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
start() {
|
||
|
if (this.processor) return
|
||
|
|
||
|
this.processor = spawn(this.program, this.options, {
|
||
|
shell: true,
|
||
|
})
|
||
|
|
||
|
this.encoding = true
|
||
|
|
||
|
this.emit('status', this.status())
|
||
|
|
||
|
this.processor.stdout.on('data', (data) => {
|
||
|
this.emit('stdout', data.toString())
|
||
|
})
|
||
|
this.processor.stderr.on('data', (data) => {
|
||
|
this.emit('stderr', data.toString())
|
||
|
})
|
||
|
|
||
|
this.processor.on('error', (err) => {
|
||
|
this.processor = null
|
||
|
this.encoding = false
|
||
|
this.emit('error', err)
|
||
|
this.emit('status', this.status())
|
||
|
})
|
||
|
this.processor.on('exit', (code) => {
|
||
|
this.processor = null
|
||
|
this.encoding = false
|
||
|
this.emit('exit', { code: code })
|
||
|
this.emit('status', this.status())
|
||
|
})
|
||
|
}
|
||
|
|
||
|
stop() {
|
||
|
if(os.platform() === 'win32'){
|
||
|
try {
|
||
|
execSync('taskkill /pid ' + this.processor.pid + ' /T /F')
|
||
|
} catch {}
|
||
|
} else {
|
||
|
this.processor.kill();
|
||
|
}
|
||
|
this.encoding = false
|
||
|
this.processor = null
|
||
|
}
|
||
|
|
||
|
status() {
|
||
|
return {
|
||
|
encoding: this.encoding,
|
||
|
}
|
||
|
}
|
||
|
}
|