mirror of
https://github.com/xuthus83/pigallery2.git
synced 2024-11-03 21:04:03 +08:00
70 lines
1.7 KiB
TypeScript
70 lines
1.7 KiB
TypeScript
|
import {Logger} from '../../Logger';
|
||
|
import {FfmpegCommand} from 'fluent-ffmpeg';
|
||
|
import {FFmpegFactory} from '../FFmpegFactory';
|
||
|
|
||
|
|
||
|
export interface VideoConverterInput {
|
||
|
videoPath: string;
|
||
|
output: {
|
||
|
path: string,
|
||
|
bitRate?: number,
|
||
|
resolution?: 240 | 360 | 480 | 720 | 1080 | 1440 | 2160 | 4320,
|
||
|
fps?: number,
|
||
|
codec: string,
|
||
|
format: string
|
||
|
};
|
||
|
}
|
||
|
|
||
|
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);
|
||
|
|
||
|
});
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|