{"record":{"id":"3548899354b47043","repo":"remotion-dev/remotion","slug":"invalid-adts-header","errorCode":null,"errorMessage":"Invalid ADTS header ","messagePattern":"Invalid ADTS header ","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/media-parser/src/containers/transport-stream/adts-header.ts","lineNumber":21,"sourceCode":"\tgetSampleRateFromSampleFrequencyIndex,\n} from '../../aac-codecprivate';\nimport {getArrayBufferIterator} from '../../iterator/buffer-iterator';\n\nexport const readAdtsHeader = (buffer: Uint8Array) => {\n\tif (buffer.byteLength < 9) {\n\t\treturn null;\n\t}\n\n\tconst iterator = getArrayBufferIterator({\n\t\tinitialData: buffer,\n\t\tmaxBytes: buffer.byteLength,\n\t\tlogLevel: 'error',\n\t});\n\n\titerator.startReadingBits();\n\tconst bits = iterator.getBits(12);\n\tif (bits !== 0xfff) {\n\t\tthrow new Error('Invalid ADTS header ');\n\t}\n\n\t// MPEG Version, set to 0 for MPEG-4 and 1 for MPEG-2.\n\tconst id = iterator.getBits(1);\n\tif (id !== 0) {\n\t\tthrow new Error('Only supporting MPEG-4 for .ts');\n\t}\n\n\tconst layer = iterator.getBits(2);\n\tif (layer !== 0) {\n\t\tthrow new Error('Only supporting layer 0 for .ts');\n\t}\n\n\tconst protectionAbsent = iterator.getBits(1); // protection absent\n\tconst audioObjectType = iterator.getBits(2); // 1 = 'AAC-LC'\n\n\tconst samplingFrequencyIndex = iterator.getBits(4);\n\tconst sampleRate = getSampleRateFromSampleFrequencyIndex(","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/remotion-dev/remotion/blob/78fe4bb3fdb5a2cd68724393d63cb223db333fa7/packages/media-parser/src/containers/transport-stream/adts-header.ts#L3-L39","documentation":"Thrown by readAdtsHeader() when the first 12 bits of an AAC ADTS frame are not the 0xFFF sync word. ADTS frames always begin with the 12-bit sync word; a mismatch means the buffer does not start on an ADTS frame boundary or the data is corrupt/not AAC.","triggerScenarios":"Feeding a buffer to readAdtsHeader() that is misaligned (not starting at an ADTS sync point), truncated, or not AAC audio at all. Common when a TS packet's payload pointer is wrong or when concatenating partial PES payloads incorrectly.","commonSituations":"Corrupt MPEG-TS streams; buggy TS demuxing that loses ADTS alignment; non-AAC audio misrouted through the ADTS reader; bit-flips in transport.","solutions":["Verify the stream is MPEG-TS with AAC audio (ffprobe -show_streams).","If the TS is corrupt, re-mux/re-encode it: ffmpeg -i in.ts -c copy -muxpreload 0 -muxdelay 0 out.ts (or re-encode AAC).","Resync before parsing by scanning for the 0xFFF sync word at expected ADTS frame boundaries."],"exampleFix":"// before\nconst hdr = readAdtsHeader(maybeMisalignedBuffer);\n\n// after: align to the ADTS sync word first\nlet i = 0;\nwhile (i < buffer.length - 1 && !(buffer[i] === 0xff && (buffer[i + 1] & 0xf0) === 0xf0)) i++;\nconst hdr = readAdtsHeader(buffer.subarray(i));","handlingStrategy":"validation","validationCode":"// Check the ADTS 12-bit sync word (0xFFF) before fully parsing the header.\nfunction hasAdtsSync(buf: Uint8Array): boolean {\n  return buf.length >= 2 && buf[0] === 0xff && (buf[1] & 0xf0) === 0xf0;\n}\nif (!hasAdtsSync(buffer)) {\n  // resync or reject before calling readAdtsHeader\n}","typeGuard":"function isAdtsSync(buf: Uint8Array): boolean {\n  return buf.length >= 2 && buf[0] === 0xff && (buf[1] & 0xf0) === 0xf0;\n}","tryCatchPattern":"try {\n  const hdr = readAdtsHeader(buffer);\n} catch (err) {\n  if (err instanceof Error && err.message === 'Invalid ADTS header ') {\n    // scan to next 0xFFF sync and retry\n  } else throw err;\n}","preventionTips":["Resync to 0xFFF before parsing ADTS.","Verify the stream is TS/AAC with ffprobe.","Re-mux corrupt TS files before parsing."],"tags":["transport-stream","adts","aac","sync","media-parser"],"backgroundTag":null,"analyzedSha":"78fe4bb3fdb5a2cd68724393d63cb223db333fa7","analyzedAt":"2026-08-12T17:18:50.444Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}