import { mkdir, readFile, writeFile, readdir, copyFile } from 'node:fs/promises'; import path from 'node:path'; import { createHash } from 'node:crypto'; import { verifyMediaDistribution } from './verify-media-distribution.mjs'; const destination = 'apps/web/public/vendor/ffmpeg-0.12.10'; await mkdir(destination, { recursive: true }); for (const name of await readdir('node_modules/@ffmpeg/ffmpeg/dist/esm')) { if (name.endsWith('.js')) await copyFile(path.join('node_modules/@ffmpeg/ffmpeg/dist/esm', name), path.join(destination, name)); } await copyFile('node_modules/@ffmpeg/core/dist/esm/ffmpeg-core.js', path.join(destination, 'ffmpeg-core.js')); const wasm = await readFile('node_modules/@ffmpeg/core/dist/esm/ffmpeg-core.wasm'); const parts = []; for (let offset = 0, index = 0; offset < wasm.length; offset += 12 * 1024 * 1024, index++) { const bytes = wasm.subarray(offset, offset + 12 * 1024 * 1024); const file = `core-${index}.bin`; await writeFile(path.join(destination, file), bytes); parts.push({ file, bytes: bytes.length, sha256: createHash('sha256').update(bytes).digest('hex') }); } await writeFile(path.join(destination, 'manifest.json'), JSON.stringify({ bytes: wasm.length, parts })); console.log(`Prepared FFmpeg: ${wasm.length} bytes in ${parts.length} parts; largest ${Math.max(...parts.map(p => p.bytes))} bytes.`); for (const file of ['prepare-media-assets.mjs', 'prepare-media-sources.mjs', 'verify-media-distribution.mjs']) await copyFile(`scripts/${file}`, path.join(destination, `${file}.txt`)); if (process.env.APP_ENV === 'production') await verifyMediaDistribution(destination);