From e633517d3a461cfabfd28c3d7e8c3ffec69b8459 Mon Sep 17 00:00:00 2001 From: Tangent 128 Date: Thu, 7 Sep 2017 01:42:14 -0400 Subject: [PATCH] Rewrite timestamps on clusters to allow splicing --- src/bin/resynth.rs | 4 ++++ src/timecode_fixer.rs | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/bin/resynth.rs b/src/bin/resynth.rs index 70526f3..acaef39 100644 --- a/src/bin/resynth.rs +++ b/src/bin/resynth.rs @@ -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(); diff --git a/src/timecode_fixer.rs b/src/timecode_fixer.rs index 17aecab..b379dd5 100644 --- a/src/timecode_fixer.rs +++ b/src/timecode_fixer.rs @@ -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 } }