misskey/packages/backend/src/core/VideoProcessingService.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2022-09-17 20:27:08 +02:00
import { Inject, Injectable } from '@nestjs/common';
import FFmpeg from 'fluent-ffmpeg';
import { DI } from '@/di-symbols.js';
2022-09-20 22:33:11 +02:00
import type { Config } from '@/config.js';
2022-09-17 20:27:08 +02:00
import { ImageProcessingService } from '@/core/ImageProcessingService.js';
import type { IImage } from '@/core/ImageProcessingService.js';
import { createTempDir } from '@/misc/create-temp.js';
import { bindThis } from '@/decorators.js';
2022-09-17 20:27:08 +02:00
@Injectable()
export class VideoProcessingService {
constructor(
@Inject(DI.config)
private config: Config,
private imageProcessingService: ImageProcessingService,
) {
}
@bindThis
2022-09-17 20:27:08 +02:00
public async generateVideoThumbnail(source: string): Promise<IImage> {
const [dir, cleanup] = await createTempDir();
try {
await new Promise((res, rej) => {
FFmpeg({
source,
})
.on('end', res)
.on('error', rej)
.screenshot({
folder: dir,
filename: 'out.png', // must have .png extension
count: 1,
timestamps: ['5%'],
});
});
// JPEGに変換 (Webpでもいいが、MastodonはWebpをサポートせず表示できなくなる)
return await this.imageProcessingService.convertToJpeg(`${dir}/out.png`, 498, 280);
} finally {
cleanup();
}
}
}