DEV Community

Uchit Chakma
Uchit Chakma

Posted on Originally published at ucdreams.com

Building a Zero-Upload In-Browser Universal File Converter: Magic-Byte Detection, WebCodecs, Canvas & OOXML Pipelines

Building a Zero-Upload In-Browser Universal File Converter: Magic-Byte Detection, WebCodecs, Canvas & OOXML Pipelines

Most "universal" online file converters follow the same expensive, privacy-hostile architecture: upload your raw MP4, HEIC photo, WAV recording, or .docx contract to a remote S3 bucket, spin up a cloud container running ImageMagick/FFmpeg/LibreOffice, and force users behind a login, file-size cap, or watermarked paywall.

At UCDREAMS, we engineered Universal File Converter to run 100% locally inside browser memory with zero server uploads, zero watermarks, and zero file-size quotas.

Here is how our client-side conversion router detects formats reliably via magic bytes, dispatches workloads across WebCodecs, OffscreenCanvas, FFmpeg.wasm, and OOXML parsers, and cleans up memory safely.


1. Why File Extensions Lie (Magic-Byte Container Sniffing)

Users routinely rename .webp images to .jpg, save .m4a AAC streams with .mp3 extensions, or upload .heic photos from iOS Safari with ambiguous MIME headers. Relying on file.name.split('.').pop() causes silent decoder failures.

Instead, we inspect the first 16–32 bytes (Uint8Array) of the file header before dispatching a conversion worker:

export type DetectedKind =
  | 'png'
  | 'jpeg'
  | 'webp'
  | 'heic'
  | 'mp4'
  | 'wav'
  | 'pdf'
  | 'zip_ooxml'
  | 'unknown';

export async function detectMagicKind(file: File): Promise<DetectedKind> {
  const head = new Uint8Array(await file.slice(0, 32).arrayBuffer());
  const ascii = (start: number, len: number) =>
    String.fromCharCode(...head.slice(start, start + len));

  if (head[0] === 0x89 && head[1] === 0x50 && head[2] === 0x4e && head[3] === 0x47) {
    return 'png';
  }
  if (head[0] === 0xff && head[1] === 0xd8 && head[2] === 0xff) {
    return 'jpeg';
  }
  if (ascii(0, 4) === 'RIFF' && ascii(8, 4) === 'WEBP') return 'webp';
  if (ascii(0, 4) === 'RIFF' && ascii(8, 4) === 'WAVE') return 'wav';
  if (ascii(0, 4) === '%PDF') return 'pdf';
  if (head[0] === 0x50 && head[1] === 0x4b && head[2] === 0x03 && head[3] === 0x04) {
    return 'zip_ooxml';
  }
  if (ascii(4, 4) === 'ftyp') {
    const brand = ascii(8, 4);
    if (['heic', 'heix', 'mif1', 'msf1'].includes(brand)) return 'heic';
    return 'mp4';
  }
  return 'unknown';
}
Enter fullscreen mode Exit fullscreen mode

Inspecting only the leading 32 bytes takes less than a millisecond even on multi-gigabyte media exports because File.slice() reads lazily from disk without loading the full asset into RAM.


2. Routing Transformations Across Native Browser Engines

Once the true binary signature is known, the converter routes the job to the fastest zero-upload pipeline available on the client device:

  1. Images (PNG, JPG, WebP, AVIF, HEIC): Decoded via createImageBitmap() (or WASM HEVC decoder for iOS HEIC) and re-encoded inside an OffscreenCanvas Web Worker via convertToBlob({ type, quality }). Dedicated imaging workflows also live in our Bulk Image Converter and Image Compressor.
  2. Audio & Video (MP4, MOV, WebM, MP3, WAV): Demuxed and transcoded locally via FFmpeg.wasm or hardware-accelerated WebCodecs without round-tripping gigabytes over HTTPS. Pair this with our Video to Audio Extractor and MP4 Video Compressor.
  3. Documents & Spreadsheets (DOCX, XLSX, CSV, Markdown, HTML, PDF): ZIP-based ECMA-376 OOXML archives are parsed in browser RAM using JSZip and transformed into clean HTML, Markdown, CSV, or printable PDFs. For deep editing, check out our DOCX Editor Online, Online Spreadsheet Editor, and Document Converter.

3. Preventing Browser Tab OOM Crashes on Batch Conversions

When converting dozens of assets in one session, leaking Blob object URLs or retaining decoded ImageBitmap frames quickly exhausts mobile Safari memory. We enforce deterministic lifecycle cleanup after every export:

export async function convertImageInWorker(
  bitmap: ImageBitmap,
  targetMime: 'image/webp' | 'image/jpeg' | 'image/png',
  quality = 0.92
): Promise<Blob> {
  try {
    const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
    const ctx = canvas.getContext('2d');
    if (!ctx) throw new Error('2D OffscreenCanvas unavailable');
    ctx.drawImage(bitmap, 0, 0);
    return await canvas.convertToBlob({ type: targetMime, quality });
  } finally {
    bitmap.close(); // Immediately release GPU/RAM texture backing
  }
}
Enter fullscreen mode Exit fullscreen mode

Every generated URL.createObjectURL(blob) download handle is revoked immediately after triggering the anchor download or after a bounded timeout, keeping peak heap flat across large batches.


Try Universal File Converter Live (100% Free, Zero Sign-Up)


Custom Web Apps, WebAssembly Tools & 95+ PageSpeed Engineering

Built by UCDREAMS Senior Engineers (UCDREAMS TECHNOLOGIES LLP). Need a custom client-side media tool, React/Next.js + Django SaaS portal, AI automation workflow, or a 95+ Mobile PageSpeed WordPress site?

Top comments (0)