mirror of
https://github.com/arcodange-org/mediabunny.git
synced 2026-10-03 05:43:50 +02:00
Fix WAV files with embedded ID3 metadata returning empty tags (#110)
* Read ID3v2 tags from WAV file * remove js import Co-authored-by: David P. <[email protected]> * revert slicing in wave-demuxer.ts and add id3.ts for further sharing * Add `writeId3Tag` to the `WavOutputFormat` constructor options * add more context in private and public APIs that it's non-standard * compare actual values in the test with real file * remove unnecesarry double slice requesting * trim test audio file (i tried my best to preserve ID3, including artwork) * Couple of code cleanups * const -> using * Fix linting errors --------- Co-authored-by: David P. <[email protected]>
This commit is contained in:
co-authored by
David P.
parent
cab8319051
commit
6aee4e6a6a
+34
-2
@@ -16,6 +16,7 @@ import { EncodedPacket } from '../packet';
|
||||
import { WavOutputFormat } from '../output-format';
|
||||
import { assert, assertNever, isIso88591Compatible, keyValueIterator } from '../misc';
|
||||
import { MetadataTags, metadataTagsAreEmpty } from '../tags';
|
||||
import { Id3V2Writer } from '../id3';
|
||||
|
||||
export class WaveMuxer extends Muxer {
|
||||
private format: WavOutputFormat;
|
||||
@@ -159,9 +160,17 @@ export class WaveMuxer extends Muxer {
|
||||
this.riffWriter.writeU16(blockSize);
|
||||
this.riffWriter.writeU16(8 * pcmInfo.sampleSize);
|
||||
|
||||
// Metadata tags
|
||||
if (!metadataTagsAreEmpty(this.output._metadataTags)) {
|
||||
// Metadata exists, let's write an INFO chunk
|
||||
this.writeInfoChunk(this.output._metadataTags);
|
||||
const metadataFormat = this.format._options.metadataFormat ?? 'info';
|
||||
|
||||
if (metadataFormat === 'info') {
|
||||
this.writeInfoChunk(this.output._metadataTags);
|
||||
} else if (metadataFormat === 'id3') {
|
||||
this.writeId3Chunk(this.output._metadataTags);
|
||||
} else {
|
||||
assertNever(metadataFormat);
|
||||
}
|
||||
}
|
||||
|
||||
// data chunk
|
||||
@@ -300,6 +309,29 @@ export class WaveMuxer extends Muxer {
|
||||
}
|
||||
}
|
||||
|
||||
private writeId3Chunk(metadata: MetadataTags) {
|
||||
const startPos = this.writer.getPos();
|
||||
|
||||
// Write RIFF chunk header
|
||||
this.riffWriter.writeAscii('ID3 ');
|
||||
this.riffWriter.writeU32(0); // Size placeholder
|
||||
|
||||
const id3Writer = new Id3V2Writer(this.writer);
|
||||
const id3TagSize = id3Writer.writeId3V2Tag(metadata);
|
||||
|
||||
const endPos = this.writer.getPos();
|
||||
|
||||
// Update RIFF chunk size
|
||||
this.writer.seek(startPos + 4);
|
||||
this.riffWriter.writeU32(id3TagSize);
|
||||
this.writer.seek(endPos);
|
||||
|
||||
// Add padding byte if chunk size is odd
|
||||
if (id3TagSize & 1) {
|
||||
this.writer.write(new Uint8Array(1));
|
||||
}
|
||||
}
|
||||
|
||||
async finalize() {
|
||||
const release = await this.mutex.acquire();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user