Skip to content

Commit

Permalink
Include numberOfSamples for CBR encoded MP3
Browse files Browse the repository at this point in the history
  • Loading branch information
Borewit committed Nov 10, 2024
1 parent af33a24 commit 434a0ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
5 changes: 3 additions & 2 deletions lib/mpeg/MpegParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,13 @@ export class MpegParser extends AbstractID3Parser {
if (format.codecProfile && format.codecProfile[0] === 'V') {
this.metadata.setFormat('bitrate', mpegSize * 8 / format.duration);
}
} else if (this.tokenizer.fileInfo.size && format.codecProfile === 'CBR') {
}
if (this.tokenizer.fileInfo.size && format.codecProfile === 'CBR') {
const mpegSize = this.tokenizer.fileInfo.size - this.mpegOffset - (hasID3v1 ? 128 : 0);
if (this.frame_size !== null && this.samplesPerFrame !== null) {
const numberOfSamples = Math.round(mpegSize / this.frame_size) * this.samplesPerFrame;
this.metadata.setFormat('numberOfSamples', numberOfSamples);
if (format.sampleRate) {
if (format.sampleRate && !format.duration) {
const duration = numberOfSamples / format.sampleRate;
debug("Calculate CBR duration based on file size: %s", duration);
this.metadata.setFormat('duration', duration);
Expand Down
24 changes: 19 additions & 5 deletions test/test-file-mp3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ describe('Parse MP3 files', () => {

describe('duration=false', () => {

Parsers
Parsers.slice(0, 1)
.forEach(parser => {
it(parser.description, async function(){
const metadata = await parser.initParser(() => this.skip(), filePath, 'audio/mpeg', {duration: false});
assert.isUndefined(metadata.format.duration, 'Don\'t expect a duration');
const { format } = await parser.initParser(() => this.skip(), filePath, 'audio/mpeg', {duration: false});
assert.isUndefined(format.duration, 'Don\'t expect a duration');
});
});
});
Expand All @@ -269,8 +269,9 @@ describe('Parse MP3 files', () => {
Parsers
.forEach(parser => {
it(parser.description, async function(){
const metadata = await parser.initParser(() => this.skip(), filePath, 'audio/mpeg', {duration: true});
assert.approximately(metadata.format.duration, durationSleepAwayMp3, 1 / 10, 'Expect a duration');
const { format } = await parser.initParser(() => this.skip(), filePath, 'audio/mpeg', {duration: true});
assert.approximately(format.duration, durationSleepAwayMp3, 1 / 10, 'Expect a duration');
assert.strictEqual(format.numberOfSamples, 8831232, 'format.numberOfSamples');
});
});
});
Expand Down Expand Up @@ -368,4 +369,17 @@ describe('Parse MP3 files', () => {

});

describe('Expect format.numberOfSamples', async () => {
[false, true].forEach(durationFlag => {
it('duration=' + durationFlag, async () => {
const filePath = path.join(mp3SamplePath, 'lame-peak.mp3');
const {format} = await mm.parseFile(filePath, {duration: durationFlag});

assert.strictEqual(format.container, 'MPEG');
assert.strictEqual(format.codec, 'MPEG 1 Layer 3', '(format.codec');
assert.strictEqual(format.numberOfSamples, 5760,'format.numberOfSamples');
});
});
});

});

0 comments on commit 434a0ed

Please sign in to comment.