Debian Codegraph @

podcasts-gtk/src/chapter_parser/mod.rs

raw symbols SCIP
1 // mod.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 mod extended_comment;
21 mod id3;
22
23 use anyhow::Result;
24 use gst::ClockTime;
25 use gst_pbutils::Discoverer;
26 use gst_pbutils::prelude::*;
27 use std::collections::HashMap;
28 use std::io::BufReader;
29
30 use crate::chapter_parser::extended_comment::parse_extended_comment;
31 use crate::chapter_parser::id3::read_id3_chapter;
32
33 #[derive(Clone, Debug, Default, PartialEq, Eq)]
34 pub struct Chapter {
35 pub id: String,
36 pub title: String,
37 pub description: String,
38 pub start: chrono::Duration,
39 /// Currently unused, but might be useful for id3 Flag b,
40 /// where chapters should be viewed as separated.
41 #[allow(dead_code)]
42 pub end: chrono::Duration,
43 }
44
45 /// Probes gst metadata for the uri and extracts Chapter tags.
46 pub fn load_chapters(uri: &str) -> Result<Vec<Chapter>> {
47 let mut results = Vec::new();
48 let timeout = ClockTime::try_from_seconds_f32(30.0)?;
49 let discoverer = Discoverer::new(timeout)?;
50 let info = discoverer.discover_uri(uri)?;
51
52 for container in info.stream_list() {
53 if let Some(tags) = container.tags() {
54 for (name, tag) in tags.iter_generic() {
55 results.append(&mut tag_generic(name, tag));
56 }
57 }
58 }
59 results.sort_by_key(|c| c.start);
60 Ok(results)
61 }
62
63 /// Iterates over gst tags and turns them into Chapters.
64 fn tag_generic(name: &str, tags: gst::tags::GenericTagIter<'_>) -> Vec<Chapter> {
65 let mut results = Vec::new();
66 let mut extended_comment_chapters: HashMap<String, Chapter> = HashMap::new();
67 for value in tags {
68 if let Ok(s) = value.get::<String>() {
69 if "extended-comment" == name {
70 parse_extended_comment(&mut extended_comment_chapters, &s);
71 }
72 } else if "private-id3v2-frame" == name {
73 if let Ok(sample) = value.get::<gst::sample::Sample>() {
74 let readable = sample.buffer().map(|buffer| buffer.as_cursor_readable());
75 if let Some(buf) = readable {
76 if let Ok(chapter) = read_id3_chapter(&mut BufReader::new(buf)) {
77 results.push(chapter);
78 }
79 } else {
80 error!("cannot get buffer from gstSample");
81 }
82 } else {
83 error!("cannot convert to gstSample");
84 }
85 }
86 }
87 let mut values: Vec<_> = extended_comment_chapters.into_values().collect();
88 results.append(&mut values);
89 results
90 }