2024-04-16 23:30:31 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2024-03-20 19:32:04 +01:00
|
|
|
import ffmpeg, { FfprobeData } from 'fluent-ffmpeg';
|
|
|
|
import fs from 'node:fs/promises';
|
|
|
|
import { Writable } from 'node:stream';
|
|
|
|
import { promisify } from 'node:util';
|
|
|
|
import sharp from 'sharp';
|
2024-03-20 22:02:51 +01:00
|
|
|
import { Colorspace } from 'src/entities/system-config.entity';
|
2024-04-16 23:30:31 +02:00
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
2024-03-05 23:23:06 +01:00
|
|
|
import {
|
|
|
|
CropOptions,
|
|
|
|
IMediaRepository,
|
|
|
|
ResizeOptions,
|
|
|
|
TranscodeOptions,
|
|
|
|
VideoInfo,
|
2024-03-21 12:59:49 +01:00
|
|
|
} from 'src/interfaces/media.interface';
|
2024-03-21 04:15:09 +01:00
|
|
|
import { Instrumentation } from 'src/utils/instrumentation';
|
|
|
|
import { handlePromiseError } from 'src/utils/misc';
|
2023-04-04 16:48:02 +02:00
|
|
|
|
|
|
|
const probe = promisify<string, FfprobeData>(ffmpeg.ffprobe);
|
2023-08-02 03:56:10 +02:00
|
|
|
sharp.concurrency(0);
|
2024-01-21 05:10:14 +01:00
|
|
|
sharp.cache({ files: 0 });
|
2023-02-25 15:12:03 +01:00
|
|
|
|
2024-03-12 06:19:12 +01:00
|
|
|
@Instrumentation()
|
2024-04-16 23:30:31 +02:00
|
|
|
@Injectable()
|
2023-02-25 15:12:03 +01:00
|
|
|
export class MediaRepository implements IMediaRepository {
|
2024-04-16 23:30:31 +02:00
|
|
|
constructor(@Inject(ILoggerRepository) private logger: ILoggerRepository) {
|
|
|
|
this.logger.setContext(MediaRepository.name);
|
|
|
|
}
|
2023-09-03 08:21:51 +02:00
|
|
|
crop(input: string | Buffer, options: CropOptions): Promise<Buffer> {
|
2023-08-08 16:39:51 +02:00
|
|
|
return sharp(input, { failOn: 'none' })
|
2023-09-05 01:24:55 +02:00
|
|
|
.pipelineColorspace('rgb16')
|
2023-05-17 19:07:17 +02:00
|
|
|
.extract({
|
|
|
|
left: options.left,
|
|
|
|
top: options.top,
|
|
|
|
width: options.width,
|
|
|
|
height: options.height,
|
|
|
|
})
|
|
|
|
.toBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
async resize(input: string | Buffer, output: string, options: ResizeOptions): Promise<void> {
|
2023-09-05 01:24:55 +02:00
|
|
|
await sharp(input, { failOn: 'none' })
|
2023-09-26 01:18:47 +02:00
|
|
|
.pipelineColorspace(options.colorspace === Colorspace.SRGB ? 'srgb' : 'rgb16')
|
2023-08-07 22:35:25 +02:00
|
|
|
.resize(options.size, options.size, { fit: 'outside', withoutEnlargement: true })
|
|
|
|
.rotate()
|
2023-12-26 22:27:51 +01:00
|
|
|
.withIccProfile(options.colorspace)
|
|
|
|
.toFormat(options.format, {
|
|
|
|
quality: options.quality,
|
|
|
|
// this is default in libvips (except the threshold is 90), but we need to set it manually in sharp
|
|
|
|
chromaSubsampling: options.quality >= 80 ? '4:4:4' : '4:2:0',
|
|
|
|
})
|
2023-08-07 22:35:25 +02:00
|
|
|
.toFile(output);
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|
2023-04-04 16:48:02 +02:00
|
|
|
|
|
|
|
async probe(input: string): Promise<VideoInfo> {
|
|
|
|
const results = await probe(input);
|
|
|
|
return {
|
2023-04-06 05:32:59 +02:00
|
|
|
format: {
|
|
|
|
formatName: results.format.format_name,
|
|
|
|
formatLongName: results.format.format_long_name,
|
|
|
|
duration: results.format.duration || 0,
|
2024-01-31 02:25:07 +01:00
|
|
|
bitrate: results.format.bit_rate ?? 0,
|
2023-04-06 05:32:59 +02:00
|
|
|
},
|
|
|
|
videoStreams: results.streams
|
|
|
|
.filter((stream) => stream.codec_type === 'video')
|
|
|
|
.map((stream) => ({
|
2023-08-29 11:01:42 +02:00
|
|
|
index: stream.index,
|
2023-04-06 05:32:59 +02:00
|
|
|
height: stream.height || 0,
|
|
|
|
width: stream.width || 0,
|
2023-08-02 03:56:10 +02:00
|
|
|
codecName: stream.codec_name === 'h265' ? 'hevc' : stream.codec_name,
|
2023-04-06 05:32:59 +02:00
|
|
|
codecType: stream.codec_type,
|
|
|
|
frameCount: Number.parseInt(stream.nb_frames ?? '0'),
|
|
|
|
rotation: Number.parseInt(`${stream.rotation ?? 0}`),
|
2023-08-07 22:35:25 +02:00
|
|
|
isHDR: stream.color_transfer === 'smpte2084' || stream.color_transfer === 'arib-std-b67',
|
2024-02-14 17:24:39 +01:00
|
|
|
bitrate: Number.parseInt(stream.bit_rate ?? '0'),
|
2023-04-06 05:32:59 +02:00
|
|
|
})),
|
|
|
|
audioStreams: results.streams
|
|
|
|
.filter((stream) => stream.codec_type === 'audio')
|
|
|
|
.map((stream) => ({
|
2023-08-29 11:01:42 +02:00
|
|
|
index: stream.index,
|
2023-04-06 05:32:59 +02:00
|
|
|
codecType: stream.codec_type,
|
|
|
|
codecName: stream.codec_name,
|
2023-08-29 11:01:42 +02:00
|
|
|
frameCount: Number.parseInt(stream.nb_frames ?? '0'),
|
2023-04-06 05:32:59 +02:00
|
|
|
})),
|
2023-04-04 16:48:02 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-09-03 08:21:51 +02:00
|
|
|
transcode(input: string, output: string | Writable, options: TranscodeOptions): Promise<void> {
|
2023-05-22 20:07:43 +02:00
|
|
|
if (!options.twoPass) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2024-02-27 16:47:04 +01:00
|
|
|
this.configureFfmpegCall(input, output, options).on('error', reject).on('end', resolve).run();
|
2023-05-22 20:07:43 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-09-03 08:21:51 +02:00
|
|
|
if (typeof output !== 'string') {
|
2024-02-02 04:18:00 +01:00
|
|
|
throw new TypeError('Two-pass transcoding does not support writing to a stream');
|
2023-09-03 08:21:51 +02:00
|
|
|
}
|
|
|
|
|
2023-05-22 20:07:43 +02:00
|
|
|
// two-pass allows for precise control of bitrate at the cost of running twice
|
|
|
|
// recommended for vp9 for better quality and compression
|
2023-04-04 16:48:02 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2023-10-30 15:39:37 +01:00
|
|
|
// first pass output is not saved as only the .log file is needed
|
|
|
|
this.configureFfmpegCall(input, '/dev/null', options)
|
2023-05-22 20:07:43 +02:00
|
|
|
.addOptions('-pass', '1')
|
|
|
|
.addOptions('-passlogfile', output)
|
|
|
|
.addOptions('-f null')
|
2023-10-30 15:39:37 +01:00
|
|
|
.on('error', reject)
|
2023-05-22 20:07:43 +02:00
|
|
|
.on('end', () => {
|
|
|
|
// second pass
|
2023-10-30 15:39:37 +01:00
|
|
|
this.configureFfmpegCall(input, output, options)
|
2023-05-22 20:07:43 +02:00
|
|
|
.addOptions('-pass', '2')
|
|
|
|
.addOptions('-passlogfile', output)
|
2023-10-30 15:39:37 +01:00
|
|
|
.on('error', reject)
|
2024-03-05 23:23:06 +01:00
|
|
|
.on('end', () => handlePromiseError(fs.unlink(`${output}-0.log`), this.logger))
|
|
|
|
.on('end', () => handlePromiseError(fs.rm(`${output}-0.log.mbtree`, { force: true }), this.logger))
|
2023-05-22 20:07:43 +02:00
|
|
|
.on('end', resolve)
|
|
|
|
.run();
|
|
|
|
})
|
2023-04-04 16:48:02 +02:00
|
|
|
.run();
|
|
|
|
});
|
|
|
|
}
|
2023-06-18 05:22:31 +02:00
|
|
|
|
|
|
|
async generateThumbhash(imagePath: string): Promise<Buffer> {
|
|
|
|
const maxSize = 100;
|
|
|
|
|
|
|
|
const { data, info } = await sharp(imagePath)
|
|
|
|
.resize(maxSize, maxSize, { fit: 'inside', withoutEnlargement: true })
|
|
|
|
.raw()
|
|
|
|
.ensureAlpha()
|
|
|
|
.toBuffer({ resolveWithObject: true });
|
|
|
|
|
|
|
|
const thumbhash = await import('thumbhash');
|
|
|
|
return Buffer.from(thumbhash.rgbaToThumbHash(info.width, info.height, data));
|
|
|
|
}
|
2024-03-12 06:19:12 +01:00
|
|
|
|
|
|
|
private configureFfmpegCall(input: string, output: string | Writable, options: TranscodeOptions) {
|
|
|
|
return ffmpeg(input, { niceness: 10 })
|
|
|
|
.inputOptions(options.inputOptions)
|
|
|
|
.outputOptions(options.outputOptions)
|
|
|
|
.output(output)
|
|
|
|
.on('error', (error, stdout, stderr) => this.logger.error(stderr || error));
|
|
|
|
}
|
|
|
|
|
|
|
|
private chainPath(existing: string, path: string) {
|
|
|
|
const separator = existing.endsWith(':') ? '' : ':';
|
|
|
|
return `${existing}${separator}${path}`;
|
|
|
|
}
|
2023-02-25 15:12:03 +01:00
|
|
|
}
|