{"record":{"id":"0b32df7a04e07112","repo":"remotion-dev/remotion","slug":"expected-remaining-bytes-to-be-0-got-remaining","errorCode":null,"errorMessage":"Expected remaining bytes to be 0, got ${remaining}","messagePattern":"Expected remaining bytes to be 0, got (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/media-parser/src/containers/iso-base-media/mdhd.ts","lineNumber":46,"sourceCode":"\t// creation time\n\tconst creationTime =\n\t\tversion === 1 ? Number(data.getUint64()) : data.getUint32();\n\n\t// modification time\n\tconst modificationTime =\n\t\tversion === 1 ? Number(data.getUint64()) : data.getUint32();\n\n\tconst timescale = data.getUint32();\n\tconst duration = version === 1 ? data.getUint64() : data.getUint32();\n\n\tconst language = data.getUint16();\n\n\t// quality\n\tconst quality = data.getUint16();\n\n\tconst remaining = size - (data.counter.getOffset() - fileOffset);\n\tif (remaining !== 0) {\n\t\tthrow new Error(`Expected remaining bytes to be 0, got ${remaining}`);\n\t}\n\n\treturn {\n\t\ttype: 'mdhd-box',\n\t\tduration: Number(duration),\n\t\ttimescale,\n\t\tversion,\n\t\tlanguage,\n\t\tquality,\n\t\tcreationTime,\n\t\tmodificationTime,\n\t};\n};\n","sourceCodeStart":28,"sourceCodeEnd":60,"githubUrl":"https://github.com/remotion-dev/remotion/blob/78fe4bb3fdb5a2cd68724393d63cb223db333fa7/packages/media-parser/src/containers/iso-base-media/mdhd.ts#L28-L60","documentation":"Thrown at the end of parseMdhd() (media header box). After reading all defined mdhd fields for the box's version, leftover bytes remain: size - (bytesConsumed) !== 0. The box's declared size does not match the byte count the spec mandates, so the parser treats the box as malformed and aborts.","triggerScenarios":"parseMedia({src}) on an MP4/MOV/M4A whose mdhd box size is inconsistent with its version-0 or version-1 field layout — e.g. an mdhd with extra trailing bytes, a version this parser did not account for, or a size field that was corrupted/truncated.","commonSituations":"Files written by non-compliant muxers that pad or truncate boxes; edited/patched MP4s; corrupted downloads where the mdhd size field is wrong.","solutions":["Re-mux with a compliant tool: ffmpeg -i in.mp4 -c copy out.mp4 (this rebuilds boxes with correct sizes).","Validate with MP4Box -info in.mp4 or ffprobe; both flag box-size inconsistencies.","Migrate to @remotion/mediabunny (parseMedia is deprecated) which may tolerate the mismatch.","Obtain a fresh copy of the source file if you suspect download corruption."],"exampleFix":"// before\nconst {durationInSeconds} = await parseMedia({src, reader: nodeReader});\n\n// after\ntry {\n  const {durationInSeconds} = await parseMedia({src, reader: nodeReader});\n} catch (err) {\n  if (err instanceof Error && /Expected remaining bytes to be 0/.test(err.message)) {\n    // mdhd box size mismatch — re-mux with ffmpeg to rebuild boxes\n  } else throw err;\n}","handlingStrategy":"try-catch","validationCode":"// Have ffprobe/MP4Box sanity-check the file before parsing; box-size\n// inconsistencies surface there as warnings/errors:\n//   MP4Box -info in.mp4\n//   ffprobe -v warning in.mp4\nimport {execFileSync} from 'node:child_process';\nfunction fileLooksIntact(file: string): boolean {\n  try {\n    execFileSync('ffprobe', ['-v','warning','-hide_banner', file], {encoding: 'utf8', stdio:'pipe'});\n    return true;\n  } catch { return false; }\n}","typeGuard":"// mdhd box correctness is a deep binary property; not type-guardable by the caller.\n// => typeGuard: null","tryCatchPattern":"try {\n  await parseMedia({src, reader: nodeReader});\n} catch (err) {\n  if (err instanceof Error && /Expected remaining bytes to be 0/.test(err.message)) {\n    // mdhd size mismatch — re-mux with ffmpeg\n  } else {\n    throw err;\n  }\n}","preventionTips":["Re-mux suspect files with ffmpeg to rebuild correctly-sized boxes.","Validate with MP4Box -info / ffprobe before parsing.","Migrate to @remotion/mediabunny (parseMedia is deprecated).","Keep a try/catch around parseMedia() for untrusted inputs."],"tags":["mp4","iso-base-media","media-parser","mdhd","corrupt-file","box-size"],"backgroundTag":null,"analyzedSha":"78fe4bb3fdb5a2cd68724393d63cb223db333fa7","analyzedAt":"2026-08-12T17:18:50.444Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}