import * as path from 'path' import * as fs from 'fs' import * as os from 'os' import { exec } from 'child_process' import { promisify } from 'util' const execAsync = promisify(exec) export async function convertGifToVideo(gifPath: string): Promise { // Create a temporary file path for the output video const tempDir = os.tmpdir() const outputPath = path.join( tempDir, `converted-${Date.now()}-${Math.random().toString(36).substring(2)}.mp4` ) // Use ffmpeg to convert the GIF to MP4 with optimized settings // -pix_fmt yuv420p: Ensures maximum compatibility // -vf scale=trunc(iw/2)*2:trunc(ih/2)*2: Ensures even dimensions // -movflags +faststart: Optimizes for streaming // -an: Removes audio (not needed for GIFs) // -crf 23: Good balance between quality and file size // -preset medium: Good balance between encoding speed and compression await execAsync( `ffmpeg -i "${gifPath}" -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" -movflags +faststart -an -crf 23 -preset medium "${outputPath}"` ) return outputPath }