Skip to content

Commit

Permalink
feat: add encoding option to ID3v1
Browse files Browse the repository at this point in the history
  • Loading branch information
eidoriantan committed Mar 22, 2024
1 parent 2a4547b commit 39a541c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 6 additions & 6 deletions src/id3v1/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ export function validate (tags, strict) {
return true
}

export function encode (tags) {
export function encode (tags, encoding = 'utf-8') {
let { title, artist, album, year, comment, track, genre } = tags

title = encodeString(title, 'utf-8')
artist = encodeString(artist, 'utf-8')
album = encodeString(album, 'utf-8')
year = encodeString(year, 'utf-8')
comment = encodeString(comment, 'utf-8')
title = encodeString(title, encoding)
artist = encodeString(artist, encoding)
album = encodeString(album, encoding)
year = encodeString(year, encoding)
comment = encodeString(comment, encoding)
genre = GENRES.indexOf(genre)

while (title.length < 30) title.push(0)
Expand Down
7 changes: 5 additions & 2 deletions src/mp3tag.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ export default class MP3Tag {

options = overwriteDefault(options, {
strict: false,
id3v1: { include: false },
id3v1: {
include: false,
encoding: 'utf-8'
},
id3v2: {
include: true,
unsynch: false,
Expand All @@ -193,7 +196,7 @@ export default class MP3Tag {
ID3v1.validate(tags.v1, options.strict)

if (verbose) console.log('Writing ID3v1...')
const encoded = ID3v1.encode(tags.v1)
const encoded = ID3v1.encode(tags.v1, options.encoding)
const tagBytes = new Uint8Array(encoded)
audio = mergeBytes(audio, tagBytes)
}
Expand Down
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export type MP3Buffer = Buffer | ArrayBuffer;

export type MP3TagV2Versions = 2 | 3 | 4;

export type MP3TagEncodings =
'utf8' | 'utf-8' | 'utf16' | 'utf16be' | 'utf-16' | 'utf-16be' |
'windows1251' | '';

export interface MP3TagDefaultReadOptions {
id3v1: boolean;
id3v2: boolean;
Expand All @@ -14,6 +18,7 @@ export interface MP3TagDefaultWriteOptions {
strict: boolean;
id3v1: {
include: boolean;
encoding: MP3TagEncodings;
};
id3v2: {
include: boolean;
Expand Down

0 comments on commit 39a541c

Please sign in to comment.