Created
September 5, 2023 12:27
-
-
Save jb-alvarado/8874ecf019fe48b6f37ae79b4ef5666f to your computer and use it in GitHub Desktop.
fill list with randomized clips until limit is reached
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use rand::Rng; | |
#[derive(Debug, Clone, Copy, PartialEq)] | |
struct Clip { | |
source: &'static str, | |
duration: f64, | |
} | |
impl Clip { | |
fn new(source: &'static str, duration: f64) -> Self { | |
Self { source, duration } | |
} | |
} | |
fn sum_floats(clip_list: &Vec<Clip>) -> f64 { | |
let mut list_duration = 0.0; | |
for item in clip_list { | |
list_duration += item.duration | |
} | |
list_duration | |
} | |
fn prepare_random_list(clip_list: Vec<Clip>, total_length: f64) -> Vec<Clip> { | |
let mut max_attempts = 10000; | |
let mut randomized_clip_list: Vec<Clip> = vec![]; | |
let mut target_duration = 0.0; | |
let clip_list_length = clip_list.len(); | |
let usage_limit = (total_length / sum_floats(&clip_list)).floor() + 1.0; | |
let mut last_clip = Clip::new("dummy", 0.0); | |
while target_duration < total_length && max_attempts > 0 { | |
let index = rand::thread_rng().gen_range(0..clip_list_length); | |
let selected_clip = clip_list[index]; | |
let selected_clip_count = randomized_clip_list | |
.iter() | |
.filter(|&n| *n == selected_clip) | |
.count() as f64; | |
if selected_clip_count == usage_limit | |
|| last_clip == selected_clip | |
|| target_duration + selected_clip.duration > total_length | |
{ | |
max_attempts -= 1; | |
continue; | |
} | |
target_duration += selected_clip.duration; | |
randomized_clip_list.push(selected_clip); | |
max_attempts -= 1; | |
last_clip = selected_clip; | |
} | |
randomized_clip_list | |
} | |
fn main() { | |
let clip_list = vec![ | |
Clip::new("video01.mp4", 18.0), | |
Clip::new("video02.mp4", 300.01), | |
Clip::new("video03.mp4", 13.04), | |
Clip::new("video04.mp4", 15.04), | |
Clip::new("video05.mp4", 60.0), | |
Clip::new("video06.mp4", 18.0), | |
Clip::new("video07.mp4", 14.04), | |
Clip::new("video08.mp4", 20.04), | |
Clip::new("video09.mp4", 14.04), | |
Clip::new("video10.mp4", 15.04), | |
Clip::new("video11.mp4", 18.0), | |
Clip::new("video12.mp4", 20.0), | |
Clip::new("video13.mp4", 18.048), | |
Clip::new("video14.mp4", 19.0), | |
Clip::new("video15.mp4", 18.0), | |
Clip::new("video16.mp4", 35.048), | |
Clip::new("video17.mp4", 18.0), | |
Clip::new("video18.mp4", 100.048), | |
]; | |
let target_length = 7200.0; | |
let randomized_clip_list = prepare_random_list(clip_list, target_length); | |
let mut counts_random: HashMap<&'static str, usize> = HashMap::new(); | |
for item in randomized_clip_list.clone() { | |
let count = counts_random.entry(item.source).or_insert(0); | |
*count += 1; | |
} | |
println!( | |
"target length: {target_length} || real length: {}", | |
sum_floats(&randomized_clip_list) | |
); | |
println!("counts_random: {:#?}", counts_random); | |
//println!( | |
// "randomized_clip_list: {:#?}", | |
// randomized_clip_list.iter().map(|c| c.source).collect::<Vec<&'static str>>() | |
//); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment