Skip to content

Commit 3040351

Browse files
committed
VorbisComments: Support current/total TRACKNUMBER fields
Closes: #499
1 parent af0ce91 commit 3040351

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

lofty/src/ogg/read.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::config::{ParseOptions, ParsingMode};
44
use crate::error::{ErrorKind, LoftyError, Result};
55
use crate::macros::{decode_err, err, parse_mode_choice};
66
use crate::picture::{MimeType, Picture, PictureInformation, PictureType};
7+
use crate::tag::Accessor;
78
use crate::util::text::{utf16_decode, utf8_decode, utf8_decode_str};
89

910
use std::borrow::Cow;
@@ -167,6 +168,32 @@ where
167168
},
168169
}
169170
},
171+
// Support the case of TRACKNUMBER being equal to current/total
172+
k if k.eq_ignore_ascii_case(b"TRACKNUMBER") => {
173+
match utf8_decode_str(value) {
174+
Ok(value) => {
175+
let mut value_split = value.splitn(2, '/');
176+
let track_number: Option<u32> =
177+
value_split.next().and_then(|b| b.parse().ok());
178+
let track_total: Option<u32> =
179+
value_split.next().and_then(|b| b.parse().ok());
180+
if let Some(n) = track_number {
181+
tag.set_track(n);
182+
}
183+
if let Some(n) = track_total {
184+
tag.set_track_total(n);
185+
}
186+
},
187+
Err(e) => {
188+
if parse_mode == ParsingMode::Strict {
189+
return Err(e);
190+
}
191+
192+
log::warn!("Non UTF-8 value found, discarding field {key:?}");
193+
continue;
194+
},
195+
}
196+
},
170197
// The valid range is 0x20..=0x7D not including 0x3D
171198
k if k.iter().all(|c| (b' '..=b'}').contains(c) && *c != b'=') => {
172199
// SAFETY: We just verified that all of the bytes fall within the subset of ASCII
1.19 KB
Binary file not shown.

lofty/tests/files/ogg.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ fn flac_with_id3v2() {
167167
assert!(flac_file.vorbis_comments().is_some());
168168
}
169169

170+
// case TRACKNUMBER=11/22 (current/total)
171+
#[test_log::test]
172+
fn opus_issue_499() {
173+
use lofty::ogg::OpusFile;
174+
175+
let file = std::fs::read("tests/files/assets/issue_499.opus").unwrap();
176+
let opus_file =
177+
OpusFile::read_from(&mut std::io::Cursor::new(file), ParseOptions::new()).unwrap();
178+
179+
let comments = opus_file.vorbis_comments();
180+
assert_eq!(comments.track(), Some(11));
181+
assert_eq!(comments.track_total(), Some(22));
182+
}
183+
170184
#[test_log::test]
171185
fn flac_remove_id3v2() {
172186
crate::remove_tag!("tests/files/assets/flac_with_id3v2.flac", TagType::Id3v2);

0 commit comments

Comments
 (0)