pigallery2/backend/model/threading/VideoConverterWorker.ts
Patrik J. Braun 1be392e7da implementing video settings saving
refactoring settings
2019-12-10 09:36:14 +01:00

71 lines
1.8 KiB
TypeScript

import {Logger} from '../../Logger';
import {FfmpegCommand} from 'fluent-ffmpeg';
import {FFmpegFactory} from '../FFmpegFactory';
import {ServerConfig} from '../../../common/config/private/IPrivateConfig';
export interface VideoConverterInput {
videoPath: string;
output: {
path: string,
bitRate?: number,
resolution?: ServerConfig.resolutionType,
fps?: number,
codec: ServerConfig.codecType,
format: ServerConfig.formatType
};
}
export class VideoConverterWorker {
private static ffmpeg = FFmpegFactory.get();
public static convert(input: VideoConverterInput): Promise<void> {
if (this.ffmpeg == null) {
this.ffmpeg = FFmpegFactory.get();
}
return new Promise((resolve, reject) => {
Logger.silly('[FFmpeg] transcoding video: ' + input.videoPath);
const command: FfmpegCommand = this.ffmpeg(input.videoPath);
let executedCmd = '';
command
.on('start', (cmd: string) => {
Logger.silly('[FFmpeg] running:' + cmd);
executedCmd = cmd;
})
.on('end', () => {
resolve();
})
.on('error', (e: any) => {
reject('[FFmpeg] ' + e.toString() + ' executed: ' + executedCmd);
});
// set video bitrate
if (input.output.bitRate) {
command.videoBitrate((input.output.bitRate / 1024) + 'k');
}
// set target codec
command.videoCodec(input.output.codec);
if (input.output.resolution) {
command.size('?x' + input.output.resolution);
}
// set fps
if (input.output.fps) {
command.fps(input.output.fps);
}
// set output format to force
command.format(input.output.format)
// save to file
.save(input.output.path);
});
}
}