build video support
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import sharp from 'sharp';
|
||||
import fs from 'fs';
|
||||
import { execFile } from 'child_process';
|
||||
import { promisify } from 'util';
|
||||
import { absolutePath, ensureDir } from './storage.js';
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
|
||||
export interface ImageMeta {
|
||||
width: number;
|
||||
height: number;
|
||||
@@ -29,6 +33,30 @@ export async function extractMeta(filePath: string): Promise<ImageMeta> {
|
||||
};
|
||||
}
|
||||
|
||||
export async function extractVideoMeta(filePath: string, mimeType: string): Promise<ImageMeta> {
|
||||
const abs = absolutePath(filePath);
|
||||
const stat = fs.statSync(abs);
|
||||
try {
|
||||
const { stdout } = await execFileAsync('ffprobe', [
|
||||
'-v', 'quiet',
|
||||
'-print_format', 'json',
|
||||
'-show_streams',
|
||||
abs,
|
||||
]);
|
||||
const data = JSON.parse(stdout);
|
||||
const video = (data.streams as any[])?.find((s) => s.codec_type === 'video');
|
||||
return {
|
||||
width: video?.width ?? 1280,
|
||||
height: video?.height ?? 720,
|
||||
mimeType,
|
||||
size: stat.size,
|
||||
};
|
||||
} catch {
|
||||
// ffprobe unavailable or failed — use safe defaults
|
||||
return { width: 1280, height: 720, mimeType, size: stat.size };
|
||||
}
|
||||
}
|
||||
|
||||
export async function saveBuffer(buffer: Buffer, destRelPath: string): Promise<void> {
|
||||
ensureDir(destRelPath);
|
||||
const abs = absolutePath(destRelPath);
|
||||
|
||||
Reference in New Issue
Block a user