v1.5.1: don't include fractional seconds in displayed timestamps in chosts

This commit is contained in:
xenofem 2023-08-01 13:36:39 -04:00
parent f584b17dd1
commit 9c366efab1
3 changed files with 35 additions and 20 deletions

2
Cargo.lock generated
View file

@ -1220,7 +1220,7 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "screencap-bot"
version = "1.5.0"
version = "1.5.1"
dependencies = [
"anyhow",
"dotenvy",

View file

@ -1,6 +1,6 @@
[package]
name = "screencap-bot"
version = "1.5.0"
version = "1.5.1"
edition = "2021"
authors = ["xenofem <xenofem@xeno.science>"]
license = "MIT"

View file

@ -97,7 +97,7 @@ async fn post_random_capture<R: Rng>(
.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<R: Rng>(
));
}
let timestamp = max_timestamp * rng.sample::<f64, _>(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<R: Rng>(
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<R: Rng>(
)
.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<R: Rng>(
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<R: Rng>(
Ok(())
}
fn format_timestamp(timestamp: f64, total_duration: Option<f64>) -> String {
fn format_timestamp(
timestamp: f64,
total_duration: Option<f64>,
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)
let truncated_timestamp = timestamp as u32;
let optional_hours = if total_duration >= 3600.0 {
format!("{}:", truncated_timestamp / 3600)
} else {
String::new()
},
((timestamp % 3600.0) / 60.0) as u32,
timestamp % 60.0
)
};
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}")
}