import {EncoderListeners, EncoderOptions, Settings, User, Video} from "../../shared/schema"; import * as fs from "fs"; import axios, {AxiosInstance} from "axios"; const ffmpeg = require('fluent-ffmpeg') export class Encoder { private readonly id: string; private readonly input: string; private readonly output: string; private readonly options: EncoderOptions; private readonly listeners: EncoderListeners; private readonly settings: Settings; private api: AxiosInstance; private s3: AxiosInstance; constructor( id: string, input: string, options: EncoderOptions, listeners: EncoderListeners, settings: Settings ) { this.id = id; this.input = input; this.output = this.input.replace(/\.mp4$/, '.flv') this.options = options; this.listeners = listeners; this.settings = settings; this.api = axios.create({ baseURL: settings.endpoint, headers: { Authorization: `${settings.credentials.token_type} ${settings.credentials.access_token}` } }) this.s3 = axios.create({}); } async encode(): Promise { this.listeners.onStart(this.id) const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; ffmpeg.setFfmpegPath(ffmpegPath) ffmpeg(this.input) .outputOptions(this.getOutputOptions()) .output(this.output) .on('start', () => { console.log('start') }) .on('progress', (progress) => { console.log('progress', progress) this.listeners.onProgress(this.id, progress.percent) }) .on('end', async () => { console.log('end') // @ts-ignore try { const video = await this.requestUploadUrl(this.settings.credentials.user) await this.upload(video) } catch (error) { console.log('error', error) this.listeners.onError(this.id, error.message) } }) .on('error', (error) => { console.log('error', error) this.listeners.onError(this.id, error.message) }) .run() } private getOutputOptions() { return [ '-c:v libx264', '-preset veryfast', '-crf 23', '-maxrate 6000k', '-bufsize 6000k', '-c:a aac', '-b:a 128k', '-ac 2', '-f flv', '-movflags +faststart', '-y' ] } private async requestUploadUrl(user: User): Promise