diff --git a/crunchy-cli-core/src/utils/download.rs b/crunchy-cli-core/src/utils/download.rs index eb280c31..bd7bf3df 100644 --- a/crunchy-cli-core/src/utils/download.rs +++ b/crunchy-cli-core/src/utils/download.rs @@ -1,5 +1,6 @@ use crate::utils::ffmpeg::FFmpegPreset; use crate::utils::filter::real_dedup_vec; +use crate::utils::fmt::format_time_delta; use crate::utils::log::progress; use crate::utils::os::{ cache_dir, is_special_file, temp_directory, temp_named_pipe, tempdir, tempfile, @@ -595,7 +596,7 @@ impl Downloader { for (i, meta) in videos.iter().enumerate() { if let Some(start_time) = meta.start_time { - input.extend(["-ss".to_string(), format_time_delta(start_time)]) + input.extend(["-ss".to_string(), format_time_delta(&start_time)]) } input.extend(["-i".to_string(), meta.path.to_string_lossy().to_string()]); maps.extend(["-map".to_string(), i.to_string()]); @@ -616,7 +617,7 @@ impl Downloader { } for (i, meta) in audios.iter().enumerate() { if let Some(start_time) = meta.start_time { - input.extend(["-ss".to_string(), format_time_delta(start_time)]) + input.extend(["-ss".to_string(), format_time_delta(&start_time)]) } input.extend(["-i".to_string(), meta.path.to_string_lossy().to_string()]); maps.extend(["-map".to_string(), (i + videos.len()).to_string()]); @@ -663,7 +664,7 @@ impl Downloader { if container_supports_softsubs { for (i, meta) in subtitles.iter().enumerate() { if let Some(start_time) = meta.start_time { - input.extend(["-ss".to_string(), format_time_delta(start_time)]) + input.extend(["-ss".to_string(), format_time_delta(&start_time)]) } input.extend(["-i".to_string(), meta.path.to_string_lossy().to_string()]); maps.extend([ @@ -1390,8 +1391,8 @@ fn fix_subtitles(raw: &mut Vec, max_length: TimeDelta) { format!( "Dialogue: {},{},{},", layer, - format_time_delta(start), - format_time_delta(end) + format_time_delta(&start), + format_time_delta(&end) ), ) .to_string() @@ -1665,18 +1666,6 @@ fn check_frame_windows(base_hashes: &[ImageHash], check_hashes: &[ImageHash]) -> results } -fn format_time_delta(time_delta: TimeDelta) -> String { - let hours = time_delta.num_hours(); - let minutes = time_delta.num_minutes() - time_delta.num_hours() * 60; - let seconds = time_delta.num_seconds() - time_delta.num_minutes() * 60; - let milliseconds = time_delta.num_milliseconds() - time_delta.num_seconds() * 1000; - - format!( - "{}:{:0>2}:{:0>2}.{:0>3}", - hours, minutes, seconds, milliseconds - ) -} - fn len_from_segments(segments: &[StreamSegment]) -> TimeDelta { TimeDelta::milliseconds(segments.iter().map(|s| s.length.as_millis()).sum::() as i64) } diff --git a/crunchy-cli-core/src/utils/fmt.rs b/crunchy-cli-core/src/utils/fmt.rs new file mode 100644 index 00000000..cd7d81a1 --- /dev/null +++ b/crunchy-cli-core/src/utils/fmt.rs @@ -0,0 +1,19 @@ +use chrono::TimeDelta; + +pub fn format_time_delta(time_delta: &TimeDelta) -> String { + let negative = *time_delta < TimeDelta::zero(); + let time_delta = time_delta.abs(); + let hours = time_delta.num_hours(); + let minutes = time_delta.num_minutes() - time_delta.num_hours() * 60; + let seconds = time_delta.num_seconds() - time_delta.num_minutes() * 60; + let milliseconds = time_delta.num_milliseconds() - time_delta.num_seconds() * 1000; + + format!( + "{}{}:{:0>2}:{:0>2}.{:0>3}", + if negative { "-" } else { "" }, + hours, + minutes, + seconds, + milliseconds + ) +} diff --git a/crunchy-cli-core/src/utils/mod.rs b/crunchy-cli-core/src/utils/mod.rs index e5c48942..72a0908e 100644 --- a/crunchy-cli-core/src/utils/mod.rs +++ b/crunchy-cli-core/src/utils/mod.rs @@ -3,6 +3,7 @@ pub mod context; pub mod download; pub mod ffmpeg; pub mod filter; +pub mod fmt; pub mod format; pub mod interactive_select; pub mod locale;