From e4c23c0fe5fc4bd74693d75b09e45afce49b2188 Mon Sep 17 00:00:00 2001 From: Cameron Cordes Date: Mon, 8 Feb 2021 19:46:38 -0500 Subject: [PATCH] Improve video streaming speed Instead of waiting for an entire video to stream, we spawn a child ffmpeg process to start generating the HLS playlist, and once it creates the first part of the playlist we return the response so the client can start streaming almost immediately. HTTP Live streaming can handle playlist updates after the initial playlist is created, although I don't think the user can skip to the end and skip streaming parts of the video. --- src/video.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/video.rs b/src/video.rs index 5ac753a..98027b9 100644 --- a/src/video.rs +++ b/src/video.rs @@ -26,11 +26,16 @@ pub fn create_playlist(video_path: &str, playlist_file: &str) { .arg("-vf") .arg("scale=1080:-2,setsar=1:1") .arg(playlist_file) - .output() - .expect("Expected this to work.."); + .spawn(); - println!("{:?}", result); - println!("Status: {}", String::from_utf8(result.stdout).unwrap()) + let start_time = std::time::Instant::now(); + loop { + std::thread::sleep(std::time::Duration::from_secs(1)); + + if Path::new(playlist_file).exists() || std::time::Instant::now() - start_time > std::time::Duration::from_secs(5) { + break; + } + } } pub fn generate_video_thumbnail(path: &Path, destination: &Path) {