Skip to content
This repository has been archived by the owner on Jul 4, 2024. It is now read-only.

Commit

Permalink
move format_time_delta to own file (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
Frooastside committed Apr 28, 2024
1 parent bf28dbf commit f237033
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
23 changes: 6 additions & 17 deletions crunchy-cli-core/src/utils/download.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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()]);
Expand All @@ -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()]);
Expand Down Expand Up @@ -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([
Expand Down Expand Up @@ -1390,8 +1391,8 @@ fn fix_subtitles(raw: &mut Vec<u8>, max_length: TimeDelta) {
format!(
"Dialogue: {},{},{},",
layer,
format_time_delta(start),
format_time_delta(end)
format_time_delta(&start),
format_time_delta(&end)
),
)
.to_string()
Expand Down Expand Up @@ -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::<u128>() as i64)
}
19 changes: 19 additions & 0 deletions crunchy-cli-core/src/utils/fmt.rs
Original file line number Diff line number Diff line change
@@ -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
)
}
1 change: 1 addition & 0 deletions crunchy-cli-core/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f237033

Please sign in to comment.