2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import FFmpeg from 'fluent-ffmpeg';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { ImageProcessingService } from '@/core/ImageProcessingService.js';
|
|
|
|
import type { IImage } from '@/core/ImageProcessingService.js';
|
|
|
|
import { createTempDir } from '@/misc/create-temp.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-02-12 09:13:47 +09:00
|
|
|
import { appendQuery, query } from '@/misc/prelude/url.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class VideoProcessingService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
private imageProcessingService: ImageProcessingService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09: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%'],
|
|
|
|
});
|
|
|
|
});
|
2022-12-25 00:15:48 +09:00
|
|
|
|
2022-12-25 01:03:05 +09:00
|
|
|
return await this.imageProcessingService.convertToWebp(`${dir}/out.png`, 498, 280);
|
2022-09-18 03:27:08 +09:00
|
|
|
} finally {
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 09:13:47 +09:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public getExternalVideoThumbnailUrl(url: string): string | null {
|
|
|
|
if (this.config.videoThumbnailGenerator == null) return null;
|
|
|
|
|
|
|
|
return appendQuery(
|
|
|
|
`${this.config.videoThumbnailGenerator}/thumbnail.webp`,
|
|
|
|
query({
|
|
|
|
thumbnail: '1',
|
|
|
|
url,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|