Add calculation of begin and end time

This commit is contained in:
2022-11-04 19:14:36 +01:00
parent 9cff696dec
commit 0631e2a483

View File

@@ -5,7 +5,8 @@ use clap::Parser;
use tokio::time::{sleep_until, Instant, Duration}; use tokio::time::{sleep_until, Instant, Duration};
use tokio::process::Command; use tokio::process::Command;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use chrono::NaiveTime; use chrono::prelude::*;
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
@@ -20,11 +21,14 @@ async fn main() -> Result<()> {
} }
}; };
let (begin, end) = calculate_begin_end(&args)?;
// Print the recording details // Print the recording details
println!("Recording starts in {}min for {}min", args.start_minutes, args.duration_minutes); println!("Recording starts in {:?} for {:?}", begin, end);
// Wait the given amount of time // Wait the given amount of time
sleep_until(Instant::now() + Duration::from_secs(args.start_minutes * 60)).await; sleep_until(Instant::now() + begin).await;
// Connect to the OBS instance through obs-websocket. // Connect to the OBS instance through obs-websocket.
let client = Client::connect("localhost", cfg.websocket_port, Some(cfg.websocket_password)).await?; let client = Client::connect("localhost", cfg.websocket_port, Some(cfg.websocket_password)).await?;
@@ -41,7 +45,7 @@ async fn main() -> Result<()> {
println!("Recording started."); println!("Recording started.");
// Wait for the recording to finish // Wait for the recording to finish
sleep_until(Instant::now() + Duration::from_secs(args.duration_minutes * 60)).await; sleep_until(Instant::now() + end).await;
// Stop the recording // Stop the recording
stop_recording(&client).await?; stop_recording(&client).await?;
@@ -78,11 +82,40 @@ async fn shutdown() -> Result<(), anyhow::Error> {
Ok(()) Ok(())
} }
fn parse_time(time: &str) -> Result<NaiveTime, anyhow::Error> { fn parse_time(time: &str) -> Result<chrono::Duration, anyhow::Error> {
let parsed = NaiveTime::parse_from_str(time, "%H:%M")?; let parsed = NaiveTime::parse_from_str(time, "%H:%M")?;
Ok(parsed) let local = Local::now();
let datetime = local.date().and_time(parsed);
if let Some(dt) = datetime {
let duration = dt - local;
return Ok(duration);
}
anyhow::bail!("Can't convert to duration.")
} }
fn calculate_begin_end(args: &Args) -> Result<(Duration, Duration), anyhow::Error> {
let begin;
let end;
if let Some(btime) = args.begin_time.clone() {
let pt = parse_time(&btime)?;
begin = pt.to_std()?;
} else {
begin = Duration::from_secs(args.start_minutes * 60);
}
if let Some(etime) = args.end_time.clone() {
let pt = parse_time(&etime)?;
// We wait for the begin duration and have to substract that from the end duration,
// so we get the recording duration.
end = pt.to_std()? - begin;
} else {
end = Duration::from_secs(args.duration_minutes * 60);
}
Ok((begin, end))
}
/// obs-cli is a simple cli tool for planned OBS recordings /// obs-cli is a simple cli tool for planned OBS recordings
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
@@ -93,16 +126,16 @@ struct Args {
start_minutes: u64, start_minutes: u64,
/// Define the duration of the recording in minutes /// Define the duration of the recording in minutes
#[arg(short, long, default_value_t = 1)] #[arg(short, long, default_value_t = 0)]
duration_minutes: u64, duration_minutes: u64,
/// Define the start time in HH:MM format /// Define the start time in HH:MM format
#[arg(short, long, default_value_t = String::from("14:00"))] #[arg(short, long, default_value = None)]
begin_time: String, begin_time: Option<String>,
/// Define the end time in HH:MM format /// Define the end time in HH:MM format
#[arg(short, long, default_value_t = String::from("14:00"))] #[arg(short, long, default_value = None)]
end_time: String, end_time: Option<String>,
/// Flag to shutdown the machine after recording. /// Flag to shutdown the machine after recording.
#[arg(short, long, default_value_t = false)] #[arg(short, long, default_value_t = false)]