1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
|
/*
This file is part of jellything (https://codeberg.org/metamuffin/jellything)
which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
Copyright (C) 2023 metamuffin <metamuffin.org>
*/
use anyhow::{anyhow, bail, Context, Result};
use jellycommon::{Chapter, LocalTrack, SourceTrack, SourceTrackKind};
use jellymatroska::{
matroska::MatroskaTag,
read::EbmlReader,
unflatten::{IterWithPos, Unflat, Unflatten},
};
use log::{debug, error, info, warn};
use std::{path::PathBuf, time::Instant};
#[derive(Default, Debug)]
pub struct MatroskaMetadata {
pub title: Option<String>,
pub description: Option<String>,
pub tagline: Option<String>,
pub tracks: Vec<SourceTrack>,
pub track_sources: Vec<LocalTrack>,
pub cover: Option<(String, Vec<u8>)>,
pub infojson: Option<String>,
pub chapters: Vec<Chapter>,
pub duration: f64,
}
pub fn import_metadata(input: &mut EbmlReader) -> Result<MatroskaMetadata> {
while let Some(item) = input.next() {
let item = match item {
Ok(item) => item,
Err(e) => {
if !matches!(e, jellymatroska::error::Error::Io(_)) {
warn!("{e}");
}
break;
}
};
match item {
MatroskaTag::Ebml(_) => {
let mut iter = Unflatten::new_with_end(input, item);
while let Some(Ok(Unflat {
children: _, item, ..
})) = iter.n()
{
match item {
MatroskaTag::DocType(t) => {
if !matches!(t.as_str(), "matroska" | "webm") {
error!("file is neither matroska nor webm but {:?}", t)
}
}
_ => debug!("(re) tag ignored: {item:?}"),
}
}
}
MatroskaTag::Segment(_) => {
info!("extracting metadata...");
let mut children = Unflatten::new_with_end(input, item);
let t = Instant::now();
let r = import_read_segment(&mut children)?;
info!("done in {:?}", t.elapsed());
return Ok(r);
}
_ => debug!("(r) tag ignored: {item:?}"),
}
}
Err(anyhow!("no segment found"))
}
fn import_read_segment(segment: &mut Unflatten) -> Result<MatroskaMetadata> {
let (mut timestamp_scale, mut duration) = (None, None);
let mut m = MatroskaMetadata::default();
let (
mut info_found,
mut tags_found,
mut attachments_found,
mut tracks_found,
mut found_chapters,
) = (false, false, false, false, false);
while let Some(Ok(Unflat { children, item, .. })) = segment.n() {
match item {
MatroskaTag::SeekHead(_) => {}
MatroskaTag::Info(_) => {
info_found = true;
let mut children = children.unwrap();
while let Some(Ok(Unflat {
children: _, item, ..
})) = children.n()
{
match item {
MatroskaTag::Title(t) => m.title = Some(t),
MatroskaTag::TimestampScale(v) => timestamp_scale = Some(v),
MatroskaTag::Duration(v) => duration = Some(v),
_ => debug!("(rsi) tag ignored: {item:?}"),
}
}
}
MatroskaTag::Void(_) => {}
MatroskaTag::Tags(_) => {
tags_found = true;
let mut children = children.unwrap();
while let Some(Ok(Unflat { children, item, .. })) = children.n() {
match item {
MatroskaTag::Tag(_) => {
let mut children = children.unwrap();
while let Some(Ok(Unflat { children, item, .. })) = children.n() {
match item {
MatroskaTag::SimpleTag(_) => {
let (mut key, mut value) = (None, None);
let mut children = children.unwrap();
while let Some(Ok(Unflat {
children: _, item, ..
})) = children.n()
{
match item {
MatroskaTag::TagName(k) => key = Some(k),
MatroskaTag::TagString(v) => value = Some(v),
_ => debug!("(rstts) tag ignored: {item:?}"),
}
}
match (key, value) {
(Some(key), Some(value)) => match key.as_str() {
"DESCRIPTION" => m.description = Some(value),
"COMMENT" => m.tagline = Some(value),
_ => debug!("simple tag ignored: {key:?}"),
},
(None, None) => (),
_ => warn!("simple tag with only one of name/string"),
}
}
_ => debug!("(rstt) tag ignored: {item:?}"),
}
}
}
MatroskaTag::Crc32(_) => {}
_ => debug!("(rst) tag ignored: {item:?}"),
}
}
}
MatroskaTag::Attachments(_) => {
attachments_found = true;
let mut children = children.unwrap();
while let Some(Ok(Unflat { children, item, .. })) = children.n() {
match item {
MatroskaTag::AttachedFile(_) => {
let (mut name, mut data, mut mime) = Default::default();
let mut children = children.unwrap();
while let Some(Ok(Unflat {
children: _, item, ..
})) = children.n()
{
match item {
MatroskaTag::FileName(n) => name = Some(n),
MatroskaTag::FileData(d) => data = Some(d),
MatroskaTag::FileMimeType(m) => mime = Some(m),
_ => debug!("(rsaa) tag ignored: {item:?}"),
}
}
let (name, data, mime) = (
name.ok_or(anyhow!("attachment without name"))?,
data.ok_or(anyhow!("attachment without data"))?,
mime.ok_or(anyhow!("attachment without mime type"))?,
);
info!("attachment found: {name:?} type {mime:?}");
match (name.as_str(), mime.as_str()) {
("info.json", "application/json") => {
m.infojson =
Some(String::from_utf8(data).context("info.json invalid")?)
}
(_, "image/jpeg" | "image/png" | "image/webp") => {
m.cover = Some((mime, data))
}
_ => (),
}
}
_ => debug!("(rsa) tag ignored: {item:?}"),
}
}
}
MatroskaTag::Cues(_) => {}
MatroskaTag::Chapters(_) => {
found_chapters = true;
let mut children = children.unwrap();
while let Some(Ok(Unflat { children, item, .. })) = children.n() {
match item {
MatroskaTag::EditionEntry(_) => {
let mut children = children.unwrap();
while let Some(Ok(Unflat { children, item, .. })) = children.n() {
match item {
MatroskaTag::EditionUID(_)
| MatroskaTag::EditionFlagHidden(_)
| MatroskaTag::EditionFlagDefault(_) => {}
MatroskaTag::ChapterAtom(_) => {
let mut children = children.unwrap();
let mut chap = Chapter::default();
while let Some(Ok(Unflat { children, item, .. })) =
children.n()
{
match item {
MatroskaTag::ChapterFlagEnabled(_)
| MatroskaTag::ChapterFlagHidden(_)
| MatroskaTag::ChapterUID(_) => (),
MatroskaTag::ChapterTimeStart(t) => {
chap.time_start = Some(t as f64 * 1e-9)
}
MatroskaTag::ChapterTimeEnd(t) => {
chap.time_end = Some(t as f64 * 1e-9)
}
MatroskaTag::ChapterDisplay(_) => {
let mut string = String::new();
let mut lang = String::new();
let mut children = children.unwrap();
while let Some(Ok(Unflat { item, .. })) =
children.n()
{
match item {
MatroskaTag::ChapString(s) => {
string = s
}
MatroskaTag::ChapLanguage(l) => {
lang = l
}
_ => warn!(
"(rscead) tag ignored: {item:?}"
),
}
}
chap.labels.push((lang, string))
}
_ => warn!("(rscea) tag ignored: {item:?}"),
}
}
m.chapters.push(chap);
}
_ => warn!("(rsce) tag ignored: {item:?}"),
}
}
if !m.chapters.is_empty() {
info!("{} chapters added", m.chapters.len());
}
}
_ => warn!("(rsc) tag ignored: {item:?}"),
}
}
}
MatroskaTag::Tracks(_) => {
tracks_found = true;
let mut children = children.unwrap();
while let Some(Ok(Unflat { children, item, .. })) = children.n() {
match item {
MatroskaTag::TrackEntry(_) => {
let mut children = children.unwrap();
let (
mut index,
mut language,
mut codec,
mut kind,
mut sample_rate,
mut channels,
mut width,
mut height,
mut name,
mut fps,
mut bit_depth,
mut codec_private,
mut default_duration,
) = (
None, None, None, None, None, None, None, None, None, None, None,
None, None,
);
while let Some(Ok(Unflat { children, item, .. })) = children.n() {
match item {
MatroskaTag::CodecID(b) => codec = Some(b),
MatroskaTag::Language(v) => language = Some(v),
MatroskaTag::TrackNumber(v) => index = Some(v),
MatroskaTag::TrackType(v) => kind = Some(v),
MatroskaTag::Name(v) => name = Some(v),
MatroskaTag::CodecPrivate(v) => codec_private = Some(v),
MatroskaTag::DefaultDuration(v) => default_duration = Some(v),
MatroskaTag::Audio(_) => {
let mut children = children.unwrap();
while let Some(Ok(Unflat { item, .. })) = children.n() {
match item {
MatroskaTag::Channels(v) => {
channels = Some(v as usize)
}
MatroskaTag::SamplingFrequency(v) => {
sample_rate = Some(v)
}
MatroskaTag::BitDepth(v) => bit_depth = Some(v),
_ => (),
}
}
}
MatroskaTag::Video(_) => {
let mut children = children.unwrap();
while let Some(Ok(Unflat { item, .. })) = children.n() {
match item {
MatroskaTag::PixelWidth(v) => width = Some(v),
MatroskaTag::PixelHeight(v) => height = Some(v),
MatroskaTag::FrameRate(v) => fps = Some(v),
_ => (),
}
}
}
_ => (),
}
}
let track_index = index.unwrap();
let kind = match kind.ok_or(anyhow!("track type required"))? {
1 => SourceTrackKind::Video {
fps: fps.unwrap_or(0.0), // TODO
width: width.unwrap(),
height: height.unwrap(),
},
2 => SourceTrackKind::Audio {
bit_depth: bit_depth.unwrap_or(0) as usize, // TODO
channels: channels.unwrap_or(1), // TODO
sample_rate: sample_rate.unwrap_or(41_100.0), // TODO
},
17 => SourceTrackKind::Subtitles,
_ => bail!("invalid track type"),
};
m.tracks.push(SourceTrack {
default_duration,
name: name.unwrap_or_else(|| "unnamed".to_string()),
codec: codec.unwrap(),
language: language.unwrap_or_else(|| "none".to_string()),
kind,
});
m.track_sources.push(LocalTrack {
track: track_index as usize,
path: PathBuf::new(),
codec_private,
})
}
MatroskaTag::Crc32(_) => {}
_ => warn!("(rst) tag ignored: {item:?}"),
}
}
}
MatroskaTag::Cluster(_) => {}
_ => warn!("(rs) tag ignored: {item:?}"),
};
if info_found && tracks_found && attachments_found && tags_found && found_chapters {
debug!("we found all we need, stopping read early");
break;
}
}
segment.exit_dirty();
if let Some(duration) = duration {
m.duration = (duration * timestamp_scale.unwrap_or(1_000_000) as f64) / 1_000_000_000_f64;
}
Ok(m)
}
|