diff --git a/Cargo.lock b/Cargo.lock index 402bf3a..b910304 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1220,7 +1220,7 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "screencap-bot" -version = "1.5.0" +version = "1.5.1" dependencies = [ "anyhow", "dotenvy", diff --git a/Cargo.toml b/Cargo.toml index 25962c7..508eac1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "screencap-bot" -version = "1.5.0" +version = "1.5.1" edition = "2021" authors = ["xenofem "] license = "MIT" diff --git a/src/main.rs b/src/main.rs index 0fbf291..685b107 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,7 +97,7 @@ async fn post_random_capture( .with_context(|| format!("Failed to get info for media file {}", file.display()))?; debug!( "Media duration: {}", - format_timestamp(media_info.duration_secs, None) + format_timestamp(media_info.duration_secs, None, true) ); debug!( "Subtitle stream index: {:?}", @@ -115,10 +115,13 @@ async fn post_random_capture( )); } let timestamp = max_timestamp * rng.sample::(Standard); - let formatted_timestamp = format_timestamp(timestamp, Some(media_info.duration_secs)); - info!("Taking capture at {}", formatted_timestamp); + info!( + "Taking capture at {}", + format_timestamp(timestamp, Some(media_info.duration_secs), true) + ); let mut attachments = Vec::new(); + let display_timestamp = format_timestamp(timestamp, Some(media_info.duration_secs), false); if conf.capture_images { let image_data = media::take_screencap(file, timestamp, media_info.subtitle_stream_index) @@ -130,7 +133,7 @@ async fn post_random_capture( let image_attachment = eggbug::Attachment::new( image_data, - format!("{} @ {}.png", descriptor, formatted_timestamp), + format!("{} @ {}.png", descriptor, display_timestamp), String::from("image/png"), eggbug::MediaMetadata::Image { width: Some(image_size.width as u32), @@ -139,7 +142,7 @@ async fn post_random_capture( ) .with_alt_text(format!( "Screencap of {} at {}", - descriptor, formatted_timestamp + descriptor, display_timestamp )); attachments.push(image_attachment); @@ -153,11 +156,11 @@ async fn post_random_capture( let audio_attachment = eggbug::Attachment::new( audio_data, - format!("{} @ {}.mp3", descriptor, formatted_timestamp), + format!("{} @ {}.mp3", descriptor, display_timestamp), String::from("audio/mpeg"), eggbug::MediaMetadata::Audio { artist: show.title.clone(), - title: format!("{} @ {}", descriptor, formatted_timestamp), + title: format!("{} @ {}", descriptor, display_timestamp), }, ); @@ -193,16 +196,28 @@ async fn post_random_capture( Ok(()) } -fn format_timestamp(timestamp: f64, total_duration: Option) -> String { +fn format_timestamp( + timestamp: f64, + total_duration: Option, + fractional_seconds: bool, +) -> String { let total_duration = total_duration.unwrap_or(timestamp); - format!( - "{}{:02}:{:05.2}", - if total_duration >= 3600.0 { - format!("{}:", (timestamp / 3600.0) as u32) - } else { - String::new() - }, - ((timestamp % 3600.0) / 60.0) as u32, - timestamp % 60.0 - ) + + let truncated_timestamp = timestamp as u32; + + let optional_hours = if total_duration >= 3600.0 { + format!("{}:", truncated_timestamp / 3600) + } else { + String::new() + }; + + let minutes = format!("{:02}", (truncated_timestamp % 3600) / 60); + + let seconds = if fractional_seconds { + format!("{:05.2}", timestamp % 60.0) + } else { + format!("{:02}", truncated_timestamp % 60) + }; + + format!("{optional_hours}{minutes}:{seconds}") }