remotion-dev/remotion · error · Error
Invalid MPEG layer
Error message
Invalid MPEG layer
What it means
Thrown by getSamplesPerMpegFrame() (samples-per-mpeg-file.ts) when the (mpegVersion, layer) pair does not match any of the supported branches. Valid MPEG audio layers are 1, 2, 3 for MPEG-1 and MPEG-2; any other combination (e.g. layer === 0, which is 'reserved' in the spec) falls through to the throw.
Source
Thrown at packages/media-parser/src/containers/mp3/samples-per-mpeg-file.ts:32
return 1152;
}
}
if (mpegVersion === 2) {
if (layer === 1) {
return 384;
}
if (layer === 2) {
return 1152;
}
if (layer === 3) {
return 576;
}
}
throw new Error('Invalid MPEG layer');
};
View on GitHub (pinned to 78fe4bb3fd)
Solutions
- Validate the frame header's layer bits before computing samples-per-frame: layer must be 1, 2, or 3.
- Re-encode the source audio to a clean MP3: ffmpeg -i in.mp3 -c:a libmp3lame -b:a 192k out.mp3
- If parsing untrusted input, wrap the parse call in try/catch and skip the offending frame.
Example fix
// before
const spf = getSamplesPerMpegFrame({layer, mpegVersion});
// after
if (layer !== 1 && layer !== 2 && layer !== 3) {
throw new Error(`Skipping frame with reserved MPEG layer ${layer}`);
}
const spf = getSamplesPerMpegFrame({layer, mpegVersion}); Defensive patterns
Strategy: validation
Validate before calling
// Validate MPEG layer before computing samples-per-frame.
function isValidMpegLayer(layer: number): boolean {
return layer === 1 || layer === 2 || layer === 3;
}
if (!isValidMpegLayer(layer)) {
throw new Error(`Reserved/invalid MPEG layer ${layer} — corrupt frame`);
} Type guard
function isValidMpegLayer(layer: number): layer is 1 | 2 | 3 {
return layer === 1 || layer === 2 || layer === 3;
} Try / catch
try {
const spf = getSamplesPerMpegFrame({layer, mpegVersion});
} catch (err) {
if (err instanceof Error && err.message === 'Invalid MPEG layer') {
// skip this frame; do not abort the whole parse
} else throw err;
} Prevention
- Decode and validate the MPEG header's layer field before any layer-dependent call.
- Treat layer === 0 as corrupt and skip the frame.
- Reject files whose first frame has an invalid layer.
When it happens
Trigger: An MP3 frame whose layer field is the reserved value 0b00, or an mpegVersion outside the handled 1/2 set. Usually a corrupt frame header or non-MPEG data being parsed as MPEG.
Common situations: Corrupt/truncated MP3; random bytes misidentified as MP3; a frame header where bit-flips changed the layer field to the reserved value.
Related errors
- Expected Layer I, II or III
- Free bitrate not supported
- Invalid Xing header
- xing header was parsed wrong: read beyond available data
- No MP3 info
AI-assisted analysis of remotion-dev/remotion@78fe4bb3fd (2026-08-12).
Data as JSON: /api/errors/c41a34a975d63991.
Report an issue: GitHub.