From f16cd25ea4a40f48b1744c67de09549756ec8505 Mon Sep 17 00:00:00 2001 From: Amelia Frost Date: Wed, 3 Apr 2024 16:09:33 +0200 Subject: [PATCH] Fix for some chapters being sent by CR as floats (#351) * Fix for some chapters being sent by CR as floats. See: https://github.com/crunchy-labs/crunchyroll-rs/commit/3f3a80f7f7205e8ecb67e15fcf82b988eb88d9b2 * Compile fix for error[E0277]: cannot multiply `f32` by `u32` * Format Co-authored-by: bytedream --- crunchy-cli-core/src/utils/download.rs | 42 ++++++++++++++------------ 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/crunchy-cli-core/src/utils/download.rs b/crunchy-cli-core/src/utils/download.rs index 8a1fe66..d7904f3 100644 --- a/crunchy-cli-core/src/utils/download.rs +++ b/crunchy-cli-core/src/utils/download.rs @@ -1214,41 +1214,45 @@ fn write_ffmpeg_chapters( ) -> Result<()> { let video_len = video_len .signed_duration_since(NaiveTime::MIN) - .num_seconds() as u32; - events.sort_by(|(_, event_a), (_, event_b)| event_a.start.cmp(&event_b.start)); + .num_milliseconds() as f32 + / 1000.0; + events.sort_by(|(_, event_a), (_, event_b)| event_a.start.total_cmp(&event_b.start)); writeln!(file, ";FFMETADATA1")?; - let mut last_end_time = 0; + let mut last_end_time = 0.0; for (name, event) in events { - // include an extra 'Episode' chapter if the start of the current chapter is more than 10 - // seconds later than the end of the last chapter. - // this is done before writing the actual chapter of this loop to keep the chapter - // chronologically in order - if event.start as i32 - last_end_time as i32 > 10 { + /* + - Convert from seconds to milliseconds for the correct timescale + - Include an extra 'Episode' chapter if the start of the current chapter is more than 10 + seconds later than the end of the last chapter. + This is done before writing the actual chapter of this loop to keep the chapter + chronologically in order + */ + if event.start - last_end_time > 10.0 { writeln!(file, "[CHAPTER]")?; - writeln!(file, "TIMEBASE=1/1")?; - writeln!(file, "START={}", last_end_time)?; - writeln!(file, "END={}", event.start)?; + writeln!(file, "TIMEBASE=1/1000")?; + writeln!(file, "START={}", (last_end_time * 1000.0) as u32)?; + writeln!(file, "END={}", (event.start * 1000.0) as u32)?; writeln!(file, "title=Episode")?; } writeln!(file, "[CHAPTER]")?; - writeln!(file, "TIMEBASE=1/1")?; - writeln!(file, "START={}", event.start)?; - writeln!(file, "END={}", event.end)?; + writeln!(file, "TIMEBASE=1/1000")?; + writeln!(file, "START={}", (event.start * 1000.0) as u32)?; + writeln!(file, "END={}", (event.end * 1000.0) as u32)?; writeln!(file, "title={}", name)?; last_end_time = event.end; } - // only add a traling chapter if the gab between the end of the last chapter and the total video + // only add a trailing chapter if the gap between the end of the last chapter and the total video // length is greater than 10 seconds - if video_len as i32 - last_end_time as i32 > 10 { + if video_len - last_end_time > 10.0 { writeln!(file, "[CHAPTER]")?; - writeln!(file, "TIMEBASE=1/1")?; - writeln!(file, "START={}", last_end_time)?; - writeln!(file, "END={}", video_len)?; + writeln!(file, "TIMEBASE=1/1000")?; + writeln!(file, "START={}", (last_end_time * 1000.0) as u32)?; + writeln!(file, "END={}", (video_len * 1000.0) as u32)?; writeln!(file, "title=Episode")?; }