Rewrite timestamps on clusters to allow splicing
This commit is contained in:
parent
13e75a6415
commit
e633517d3a
2 changed files with 29 additions and 0 deletions
|
@ -45,6 +45,10 @@ pub fn main() {
|
|||
encode_webm_element(&fixer.process(element), &mut cursor).unwrap();
|
||||
}
|
||||
|
||||
for element in &body {
|
||||
encode_webm_element(&fixer.process(element), &mut cursor).unwrap();
|
||||
}
|
||||
|
||||
output = cursor.into_inner();
|
||||
stdout().write_all(&output).unwrap();
|
||||
output.clear();
|
||||
|
|
|
@ -2,16 +2,41 @@
|
|||
use webm::WebmElement;
|
||||
|
||||
pub struct TimecodeFixer {
|
||||
pub current_offset: u64,
|
||||
pub last_cluster_base: u64,
|
||||
pub last_observed_timecode: u64,
|
||||
pub assumed_duration: u64
|
||||
}
|
||||
|
||||
impl TimecodeFixer {
|
||||
pub fn new() -> TimecodeFixer {
|
||||
TimecodeFixer {
|
||||
current_offset: 0,
|
||||
last_cluster_base: 0,
|
||||
last_observed_timecode: 0,
|
||||
assumed_duration: 33
|
||||
}
|
||||
}
|
||||
|
||||
pub fn process<'b>(&mut self, element: &WebmElement<'b>) -> WebmElement<'b> {
|
||||
match element {
|
||||
&WebmElement::Timecode(timecode) => {
|
||||
// detect a jump backwards in the source, meaning we need to recalculate our offset
|
||||
if timecode < self.last_cluster_base {
|
||||
let next_timecode = self.last_observed_timecode + self.assumed_duration;
|
||||
self.current_offset = next_timecode - timecode;
|
||||
}
|
||||
|
||||
// remember the source timecode to detect future jumps
|
||||
self.last_cluster_base = timecode;
|
||||
|
||||
// return adjusted timecode
|
||||
WebmElement::Timecode(timecode + self.current_offset)
|
||||
},
|
||||
&WebmElement::SimpleBlock(block) => {
|
||||
self.last_observed_timecode = self.last_cluster_base + (block.timecode as u64);
|
||||
*element
|
||||
},
|
||||
_ => *element
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue