mirror of
https://codeberg.org/yeentown/barkey.git
synced 2025-07-07 20:44:34 +00:00
Add web optimization for video files during processing
This commit is contained in:
parent
6d4860bb78
commit
7cba9c11d4
2 changed files with 67 additions and 1 deletions
|
@ -159,6 +159,10 @@ export class DriveService {
|
|||
// thunbnail, webpublic を必要なら生成
|
||||
const alts = await this.generateAlts(path, type, !file.uri);
|
||||
|
||||
if (type && type.startsWith('video/')) {
|
||||
await this.videoProcessingService.webOptimizeVideo(path, type);
|
||||
}
|
||||
|
||||
if (this.meta.useObjectStorage) {
|
||||
//#region ObjectStorage params
|
||||
let [ext] = (name.match(/\.([a-zA-Z0-9_-]+)$/) ?? ['']);
|
||||
|
|
|
@ -9,18 +9,25 @@ import { DI } from '@/di-symbols.js';
|
|||
import type { Config } from '@/config.js';
|
||||
import { ImageProcessingService } from '@/core/ImageProcessingService.js';
|
||||
import type { IImage } from '@/core/ImageProcessingService.js';
|
||||
import { createTempDir } from '@/misc/create-temp.js';
|
||||
import {createTemp, createTempDir } from '@/misc/create-temp.js';
|
||||
import { bindThis } from '@/decorators.js';
|
||||
import { appendQuery, query } from '@/misc/prelude/url.js';
|
||||
import {LoggerService} from "@/core/LoggerService.js";
|
||||
import type Logger from "@/logger.js";
|
||||
|
||||
@Injectable()
|
||||
export class VideoProcessingService {
|
||||
private logger: Logger;
|
||||
|
||||
constructor(
|
||||
@Inject(DI.config)
|
||||
private config: Config,
|
||||
|
||||
private imageProcessingService: ImageProcessingService,
|
||||
|
||||
private loggerService: LoggerService,
|
||||
) {
|
||||
this.logger = this.loggerService.getLogger("video-processing");
|
||||
}
|
||||
|
||||
@bindThis
|
||||
|
@ -60,5 +67,60 @@ export class VideoProcessingService {
|
|||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Optimize video for web playback by adding faststart flag.
|
||||
* This allows the video to start playing before it is fully downloaded.
|
||||
* The original file is modified in-place.
|
||||
* @param source Path to the video file
|
||||
* @param mimeType The MIME type of the video
|
||||
* @returns Promise that resolves when optimization is complete
|
||||
*/
|
||||
@bindThis
|
||||
public async webOptimizeVideo(source: string, mimeType: string): Promise<void> {
|
||||
// faststart is only supported for MP4, M4A, M4W and MOV files (the MOV family).
|
||||
// WebM (and Matroska) files always support faststart-like behavior.
|
||||
const supportedMimeTypes = new Map([
|
||||
['video/mp4', 'mp4'],
|
||||
['video/m4a', 'mp4'],
|
||||
['video/m4v', 'mp4'],
|
||||
['video/quicktime', 'mov']
|
||||
]);
|
||||
|
||||
const outputFormat = supportedMimeTypes.get(mimeType);
|
||||
if (!outputFormat) {
|
||||
this.logger.debug(`Skipping web optimization for unsupported MIME type: ${mimeType}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const [tempPath, cleanup] = await createTemp();
|
||||
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
FFmpeg(source)
|
||||
.format(outputFormat) // Specify output format
|
||||
.addOutputOptions('-c copy') // Copy streams without re-encoding
|
||||
.addOutputOptions('-movflags +faststart')
|
||||
.on('error', reject)
|
||||
.on('end', async () => {
|
||||
try {
|
||||
// Replace original file with optimized version
|
||||
const fs = await import('node:fs/promises');
|
||||
await fs.copyFile(tempPath, source);
|
||||
this.logger.info(`Web-optimized video: ${source}`);
|
||||
resolve();
|
||||
} catch (copyError) {
|
||||
reject(copyError);
|
||||
}
|
||||
})
|
||||
.save(tempPath);
|
||||
});
|
||||
} catch (error) {
|
||||
this.logger.warn(`Failed to web-optimize video: ${source}`, { error });
|
||||
throw error;
|
||||
} finally {
|
||||
cleanup();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue