Debian Codegraph @

podcasts-gtk/src/chapter_parser/id3.rs

raw symbols SCIP
1 // id3.rs
2 //
3 // Copyright 2025 nee <[email protected]>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // SPDX-License-Identifier: GPL-3.0-or-later
19
20 use anyhow::{Result, anyhow, bail};
21 use std::io::BufRead;
22 use std::io::BufReader;
23 use std::io::Read;
24
25 use crate::chapter_parser::Chapter;
26
27 struct Id3FrameHeader {
28 header: [u8; 4],
29 size: u32,
30 /// Can be used for advanced Table Of Content (CTOC) parsing in a future MR.
31 /// Or for handling separated chapters that should not be played continuously.
32 /// "Set to 0 [..] This provides a hint as to whether the elements should be played as a continuous ordered sequence or played individually"
33 #[allow(dead_code)]
34 flags: u16,
35 }
36
37 impl Id3FrameHeader {
38 fn read_from<T: Read>(buf: &mut BufReader<T>) -> Result<Id3FrameHeader> {
39 let mut header: [u8; 4] = [0, 0, 0, 0];
40 buf.read_exact(&mut header)?;
41
42 let mut size_bytes: [u8; 4] = [0, 0, 0, 0];
43 buf.read_exact(&mut size_bytes)?;
44
45 let mut flags_bytes: [u8; 2] = [0, 0];
46 buf.read_exact(&mut flags_bytes)?;
47
48 Ok(Id3FrameHeader {
49 header,
50 size: u32::from_be_bytes(size_bytes),
51 flags: u16::from_be_bytes(flags_bytes),
52 })
53 }
54 }
55
56 /// Implemented from doc:
57 /// https://web.archive.org/web/20120313123311/https://id3.org/id3v2-chapters-1.0
58 pub fn read_id3_chapter<T: Read>(buf: &mut BufReader<T>) -> Result<Chapter> {
59 let frame = Id3FrameHeader::read_from(buf)?;
60 if &frame.header == b"CHAP" {
61 // CHAP frame
62 let id = read_0_terminated_string_iso(buf, frame.size)?;
63
64 let mut start_milisec: [u8; 4] = [0, 0, 0, 0];
65 buf.read_exact(&mut start_milisec)?;
66
67 let mut end_milisec: [u8; 4] = [0, 0, 0, 0];
68 buf.read_exact(&mut end_milisec)?;
69
70 // offset in the file in bytes, often just 0xffff
71 let mut start_bytes: [u8; 4] = [0, 0, 0, 0];
72 let _ = buf.read_exact(&mut start_bytes);
73
74 // offset in the file in bytes, often just 0xffff
75 let mut end_bytes: [u8; 4] = [0, 0, 0, 0];
76 let _ = buf.read_exact(&mut end_bytes);
77
78 // TIT2 frame (optional)
79 let title = read_id3_tit(buf, b"TIT2").unwrap_or("".to_string());
80
81 // TIT3 frame (optional)
82 let description = read_id3_tit(buf, b"TIT3").unwrap_or("".to_string());
83
84 Ok(Chapter {
85 id,
86 title,
87 description,
88 start: chrono::Duration::milliseconds(i32::from_be_bytes(start_milisec).into()),
89 end: chrono::Duration::milliseconds(i32::from_be_bytes(end_milisec).into()),
90 })
91 } else {
92 // CTOC "Table Of Contents" and APIC Images could be parsed here in the future.
93 bail!("not a chapter");
94 }
95 }
96
97 fn read_id3_tit<T: Read>(buf: &mut BufReader<T>, header: &[u8; 4]) -> Result<String> {
98 let frame = Id3FrameHeader::read_from(buf)?;
99 if &frame.header == header {
100 // 00 – ISO-8859-1 (ASCII).
101 // 01 – UCS-2 (UTF-16 encoded Unicode with BOM), in ID3v2.2 and ID3v2.3.
102 // 02 – UTF-16BE encoded Unicode without BOM, in ID3v2.4.
103 // 03 – UTF-8 encoded Unicode, in ID3v2.4.
104 let mut encoding: [u8; 1] = [0];
105 buf.read_exact(&mut encoding)?;
106
107 match encoding {
108 [0] => read_0_terminated_string_iso(buf, frame.size - 1), // -1 for encoding byte
109 [1] => read_0_terminated_string_u16_ucs(buf, frame.size - 1),
110 [2] => read_0_terminated_string_u16(buf, frame.size - 1),
111 [3] => read_0_terminated_string_u8(buf, frame.size - 1),
112 _ => Err(anyhow!("Invalid string encoding")),
113 }
114 } else {
115 bail!("not a TIT frame");
116 }
117 }
118
119 fn read_0_terminated_string_iso<T: Read>(buf: &mut BufReader<T>, max_size: u32) -> Result<String> {
120 let mut bytes = Vec::new();
121 let amount_read = buf.read_until(0, &mut bytes)?;
122 bytes.pop(); // pop 0 terminator
123 let mut too_much_read = (amount_read as i32) - max_size as i32;
124 while too_much_read > 0 {
125 bytes.pop();
126 too_much_read += 1;
127 }
128 Ok(String::from_utf8(bytes)?)
129 }
130
131 fn read_0_terminated_string_u8<T: Read>(buf: &mut BufReader<T>, max_size: u32) -> Result<String> {
132 let mut bytes = Vec::new();
133 let mut character: [u8; 2] = [0, 0];
134 let mut counter = 0;
135 loop {
136 let amount_read = buf.read(&mut character);
137 if character == [0, 0] || amount_read.unwrap_or(0) == 0 || counter >= max_size {
138 break;
139 }
140 bytes.push(character[1]);
141 bytes.push(character[0]);
142 counter += 2;
143 }
144 Ok(String::from_utf8(bytes)?)
145 }
146
147 fn read_0_terminated_string_u16<T: Read>(buf: &mut BufReader<T>, max_size: u32) -> Result<String> {
148 let mut bytes = Vec::new();
149 let mut character: [u8; 2] = [0, 0];
150 let mut counter = 0;
151 loop {
152 let amount_read = buf.read(&mut character);
153 if character == [0, 0] || amount_read.unwrap_or(0) == 0 || counter >= max_size {
154 break;
155 }
156 bytes.push(u16::from_be_bytes([character[1], character[0]]));
157 counter += 2;
158 }
159 Ok(String::from_utf16(&bytes)?)
160 }
161
162 fn read_0_terminated_string_u16_ucs<T: Read>(
163 buf: &mut BufReader<T>,
164 max_size: u32,
165 ) -> Result<String> {
166 let mut bytes = Vec::new();
167 let mut character: [u8; 2] = [0, 0];
168 let mut counter = 0;
169 loop {
170 let amount_read = buf.read(&mut character);
171 if character == [0, 0] || amount_read.unwrap_or(0) == 0 || counter >= max_size {
172 break;
173 }
174 bytes.push(u16::from_be_bytes([character[1], character[0]]));
175 counter += 2;
176 }
177 // Due to the nature of UCS-2, the output buffer could end up with
178 // three bytes for every character in the input buffer.
179 let mut title_buf = vec![0; bytes.len() * 3];
180 let res = ucs2::decode(&bytes, &mut title_buf);
181 match res {
182 Ok(_) => Ok(String::from_utf8(title_buf)?
183 .trim_end_matches('\0')
184 .to_string()),
185 // fallback to basic UTF-16
186 Err(e) => {
187 error!("UCS ERROR {e:#?}");
188 Ok(String::from_utf16(&bytes)?)
189 }
190 }
191 }
192
193 #[cfg(test)]
194 mod tests {
195 use super::*;
196
197 #[test]
198 #[rustfmt::skip]
199 fn test_id3_newrustacean() -> Result<()> {
200 // Examples taken from https://newrustacean.com/feed.xml Episode
201
202 // USLT - Unsynced lyrics Tag
203 let id3_bin: &[u8] = &[ 85, 83, 76, 84, 0, 0, 0, 132, 0, 0, 1, 101, 110, 103, 255, 254, 0, 0, 255, 254, 87, 0, 65, 0, 83, 0, 73, 0, 44, 0, 32, 0, 96, 0, 79, 0, 112, 0, 116, 0, 105, 0, 111, 0, 110, 0, 58, 0, 58, 0, 99, 0, 111, 0, 112, 0, 105, 0, 101, 0, 100, 0, 96, 0, 44, 0, 32, 0, 97, 0, 110, 0, 100, 0, 32, 0, 116, 0, 104, 0, 101, 0, 32, 0, 102, 0, 117, 0, 116, 0, 117, 0, 114, 0, 101, 0, 32, 0, 111, 0, 102, 0, 32, 0, 97, 0, 115, 0, 121, 0, 110, 0, 99, 0, 47, 0, 97, 0, 119, 0, 97, 0, 105, 0, 116, 0, 32, 0, 115, 0, 121, 0, 110, 0, 116, 0, 97, 0, 120, 0, 33, 0, ];
204 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin));
205 assert!(chapter.is_err());
206
207 // CTOC - Chapter Table of Contents
208 let id3_bin: &[u8] = &[ 67, 84, 79, 67, 0, 0, 0, 41, 0, 0, 116, 111, 99, 0, 3, 7, 99, 104, 112, 48, 0, 99, 104, 112, 49, 0, 99, 104, 112, 50, 0, 99, 104, 112, 51, 0, 99, 104, 112, 52, 0, 99, 104, 112, 53, 0, 99, 104, 112, 54, 0, ];
209 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin));
210 assert!(chapter.is_err());
211
212 let id3_bin: &[u8] = &[ 67, 72, 65, 80, 0, 0, 0, 44, 0, 0, 99, 104, 112, 48, 0, 0, 0, 0, 0, 0, 0, 89, 206, 255, 255, 255, 255, 255, 255, 255, 255, 84, 73, 84, 50, 0, 0, 0, 13, 0, 0, 1, 255, 254, 73, 0, 110, 0, 116, 0, 114, 0, 111, 0, ];
213 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin))?;
214 assert_eq!(
215 Chapter {
216 id: "chp0".to_string(),
217 title: "\u{feff}Intro".to_string(),
218 description: "".to_string(),
219 start: chrono::Duration::new(0, 0).unwrap(),
220 end: chrono::Duration::new(22, 990000000).unwrap(),
221 },
222 chapter
223 );
224
225 let id3_bin: &[u8] = &[ 67, 72, 65, 80, 0, 0, 0, 113, 0, 0, 99, 104, 112, 49, 0, 0, 0, 89, 206, 0, 1, 1, 208, 255, 255, 255, 255, 255, 255, 255, 255, 84, 73, 84, 50, 0, 0, 0, 33, 0, 0, 1, 255, 254, 83, 0, 112, 0, 111, 0, 110, 0, 115, 0, 111, 0, 114, 0, 58, 0, 32, 0, 80, 0, 97, 0, 114, 0, 105, 0, 116, 0, 121, 0, 87, 88, 88, 88, 0, 0, 0, 39, 0, 0, 0, 99, 104, 97, 112, 116, 101, 114, 32, 117, 114, 108, 0, 104, 116, 116, 112, 115, 58, 47, 47, 119, 119, 119, 46, 112, 97, 114, 105, 116, 121, 46, 105, 111, 47, 106, 111, 98, 115, ];
226 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin))?;
227 assert_eq!(
228 Chapter {
229 id: "chp1".to_string(),
230 title: "\u{feff}Sponsor: Parity".to_string(),
231 description: "".to_string(),
232 start: chrono::Duration::new(22, 990000000).unwrap(),
233 end: chrono::Duration::new(66, 0).unwrap(),
234 },
235 chapter
236 );
237
238 let id3_bin: &[u8] = &[ 67, 72, 65, 80, 0, 0, 0, 68, 0, 0, 99, 104, 112, 50, 0, 0, 1, 1, 208, 0, 3, 59, 240, 255, 255, 255, 255, 255, 255, 255, 255, 84, 73, 84, 50, 0, 0, 0, 37, 0, 0, 1, 255, 254, 52, 0, 32, 0, 121, 0, 101, 0, 97, 0, 114, 0, 115, 0, 32, 0, 115, 0, 105, 0, 110, 0, 99, 0, 101, 0, 32, 0, 49, 0, 46, 0, 48, 0, ];
239 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin))?;
240 assert_eq!(
241 Chapter {
242 id: "chp2".to_string(),
243 title: "\u{feff}4 years since 1.0".to_string(),
244 description: "".to_string(),
245 start: chrono::Duration::new(66, 0).unwrap(),
246 end: chrono::Duration::new(211, 952000000).unwrap(),
247 },
248 chapter
249 );
250
251 let id3_bin: &[u8] = &[ 67, 72, 65, 80, 0, 0, 0, 52, 0, 0, 99, 104, 112, 51, 0, 0, 3, 59, 240, 0, 7, 122, 16, 255, 255, 255, 255, 255, 255, 255, 255, 84, 73, 84, 50, 0, 0, 0, 21, 0, 0, 1, 255, 254, 82, 0, 117, 0, 115, 0, 116, 0, 32, 0, 49, 0, 46, 0, 51, 0, 53, 0, ];
252 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin))?;
253 assert_eq!(
254 Chapter {
255 id: "chp3".to_string(),
256 title: "\u{feff}Rust 1.35".to_string(),
257 description: "".to_string(),
258 start: chrono::Duration::new(211, 952000000).unwrap(),
259 end: chrono::Duration::new(490, 0).unwrap(),
260 },
261 chapter
262 );
263
264 let id3_bin: &[u8] = &[ 67, 72, 65, 80, 0, 0, 0, 90, 0, 0, 99, 104, 112, 52, 0, 0, 7, 122, 16, 0, 15, 35, 0, 255, 255, 255, 255, 255, 255, 255, 255, 84, 73, 84, 50, 0, 0, 0, 59, 0, 0, 1, 255, 254, 70, 0, 105, 0, 110, 0, 97, 0, 108, 0, 32, 0, 96, 0, 97, 0, 115, 0, 121, 0, 110, 0, 99, 0, 96, 0, 47, 0, 96, 0, 97, 0, 119, 0, 97, 0, 105, 0, 116, 0, 96, 0, 32, 0, 115, 0, 121, 0, 110, 0, 116, 0, 97, 0, 120, 0, ];
265 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin))?;
266 assert_eq!(
267 Chapter {
268 id: "chp4".to_string(),
269 title: "\u{feff}Final `async`/`await` syntax".to_string(),
270 description: "".to_string(),
271 start: chrono::Duration::new(490, 0).unwrap(),
272 end: chrono::Duration::new(992, 0).unwrap(),
273 },
274 chapter
275 );
276
277 let id3_bin: &[u8] = &[ 67, 72, 65, 80, 0, 0, 0, 121, 0, 0, 99, 104, 112, 53, 0, 0, 15, 35, 0, 0, 15, 235, 140, 255, 255, 255, 255, 255, 255, 255, 255, 84, 73, 84, 50, 0, 0, 0, 35, 0, 0, 1, 255, 254, 80, 0, 97, 0, 116, 0, 114, 0, 101, 0, 111, 0, 110, 0, 32, 0, 83, 0, 112, 0, 111, 0, 110, 0, 115, 0, 111, 0, 114, 0, 115, 0, 87, 88, 88, 88, 0, 0, 0, 45, 0, 0, 0, 99, 104, 97, 112, 116, 101, 114, 32, 117, 114, 108, 0, 104, 116, 116, 112, 115, 58, 47, 47, 112, 97, 116, 114, 101, 111, 110, 46, 99, 111, 109, 47, 110, 101, 119, 114, 117, 115, 116, 97, 99, 101, 97, 110, ];
278 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin))?;
279 assert_eq!(
280 Chapter {
281 id: "chp5".to_string(),
282 title: "\u{feff}Patreon Sponsors".to_string(),
283 description: "".to_string(),
284 start: chrono::Duration::new(992, 0).unwrap(),
285 end: chrono::Duration::new(1043, 340000000).unwrap(),
286 },
287 chapter
288 );
289
290 let id3_bin: &[u8] = &[ 67, 72, 65, 80, 0, 0, 0, 52, 0, 0, 99, 104, 112, 54, 0, 0, 15, 235, 140, 0, 16, 193, 16, 255, 255, 255, 255, 255, 255, 255, 255, 84, 73, 84, 50, 0, 0, 0, 21, 0, 0, 1, 255, 254, 83, 0, 104, 0, 111, 0, 119, 0, 32, 0, 105, 0, 110, 0, 102, 0, 111, 0, ];
291 let chapter = read_id3_chapter(&mut BufReader::new(id3_bin))?;
292 assert_eq!(
293 Chapter {
294 id: "chp6".to_string(),
295 title: "\u{feff}Show info".to_string(),
296 description: "".to_string(),
297 start: chrono::Duration::new(1043, 340000000).unwrap(),
298 end: chrono::Duration::new(1098, 0).unwrap(),
299 },
300 chapter
301 );
302
303 Ok(())
304 }
305 }