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
|
use jellycommon::ItemInfo;
use log::{debug, info};
use std::{fs::File, io::Write, path::PathBuf, sync::Arc};
use webm_iterable::{
matroska_spec::{Block, Master, MatroskaSpec},
WebmIterator, WebmWriter,
};
pub struct RemuxerContext {}
impl RemuxerContext {
pub fn new() -> Arc<Self> {
Arc::new(Self {})
}
pub fn generate_into(
&self,
writer: impl Write,
offset: usize,
path_base: PathBuf,
item: ItemInfo,
selection: Vec<u64>,
) -> anyhow::Result<()> {
let source_path = path_base.join(item.source.path);
info!("remuxing {source_path:?} to have tracks {selection:?}");
let mut input = File::open(source_path)?;
let tags = WebmIterator::new(&mut input, &[MatroskaSpec::TrackEntry(Master::Start)]);
let mut output = WebmWriter::new(writer);
let mut tscale = None;
let mut duration = None;
let mut ignore = false;
for tag in tags {
let tag = tag.unwrap();
match tag {
MatroskaSpec::SeekHead(Master::Start) | MatroskaSpec::Cues(Master::Start) => {
ignore = true
}
MatroskaSpec::SeekHead(Master::End) | MatroskaSpec::Cues(Master::End) => {
ignore = false
}
MatroskaSpec::TrackEntry(master) => {
let children = master.get_children();
let mut number = None;
for c in &children {
if let MatroskaSpec::TrackNumber(n) = c {
number = Some(*n)
}
}
let number = number.unwrap();
if selection.contains(&number) {
output.write(&MatroskaSpec::TrackEntry(Master::Full(children)))?;
}
}
MatroskaSpec::Block(ref data) => {
let data: &[u8] = &data;
let block: Block = data.try_into()?;
if selection.contains(&block.track) {
output.write(&tag)?;
}
}
MatroskaSpec::SimpleBlock(ref data) => {
let data: &[u8] = &data;
let block: Block = data.try_into()?;
if selection.contains(&block.track) {
output.write(&tag)?;
}
}
MatroskaSpec::Info(Master::Start) => (),
MatroskaSpec::TimestampScale(n) => tscale = Some(n),
MatroskaSpec::Duration(n) => duration = Some(n),
MatroskaSpec::Info(Master::End) => {
output.write(&MatroskaSpec::Info(Master::Full(vec![
MatroskaSpec::TimestampScale(tscale.unwrap()),
MatroskaSpec::Title(item.title.clone()),
MatroskaSpec::Duration(duration.unwrap()),
MatroskaSpec::MuxingApp("jellyremux".to_string()),
MatroskaSpec::WritingApp("jellything".to_string()),
])))?;
}
x => {
if !ignore {
debug!("{x:?}");
output.write(&x)?;
}
}
}
}
Ok(())
}
}
// pub struct SendWriter(pub Sender<Vec<u8>>);
// impl Write for SendWriter {
// fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
// self.0.blocking_send(buf.to_owned()).unwrap();
// Ok(buf.len())
// }
// fn flush(&mut self) -> std::io::Result<()> {
// Ok(()) // TODO should we actually do something here?
// }
// }
|