diff --git a/src/main.rs b/src/main.rs index 38a0fc5..634b5d5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -126,12 +126,17 @@ async fn generate_video(_claims: Claims, body: web::Json) -> i let filename = PathBuf::from(&body.path); if let Some(name) = filename.file_stem() { - let filename = name.to_str().expect("Filename should conver to string"); - create_playlist(&body.path, &format!("tmp/{}.m3u8", filename)); + let filename = name.to_str().expect("Filename should convert to string"); + let playlist = format!("tmp/{}.m3u8", filename); + if let Some(path) = is_valid_path(&body.path) { + create_playlist(&path.to_str().unwrap(), &playlist); + } else { + return HttpResponse::BadRequest().finish(); + } - HttpResponse::Ok() + HttpResponse::Ok().json(playlist) } else { - HttpResponse::BadRequest() + HttpResponse::BadRequest().finish() } } @@ -149,10 +154,10 @@ async fn stream_video(request: HttpRequest, path: web::Query) #[get("/video/{path}")] async fn get_video_part(request: HttpRequest, path: web::Path) -> impl Responder { - let playlist = &path.path; - println!("Video part: {}", playlist); + let part = &path.path; + println!("Video part: {}", part); - if let Ok(file) = NamedFile::open(String::from("tmp/") + playlist) { + if let Ok(file) = NamedFile::open(String::from("tmp/") + part) { file.into_response(&request).unwrap() } else { HttpResponse::NotFound().finish() diff --git a/src/video.rs b/src/video.rs index a0cddab..cc154ff 100644 --- a/src/video.rs +++ b/src/video.rs @@ -1,19 +1,30 @@ use std::process::Command; +use std::path::Path; // ffmpeg -i test.mp4 -c:v h264 -flags +cgop -g 30 -hls_time 3 out.m3u8 +// ffmpeg -i "filename.mp4" -preset veryfast -c:v libx264 -f hls -hls_list_size 100 -hls_time 2 -crf 24 -vf scale=1080:-2,setsar=1:1 attempt/vid_out.m3u8 pub fn create_playlist(video_path: &str, playlist_file: &str) { + if Path::new(playlist_file).exists() { + println!("Playlist already exists: {}", playlist_file); + return; + } + let result = Command::new("ffmpeg") .arg("-i") .arg(video_path) .arg("-c:v") .arg("h264") - .arg("-flags") - .arg("+cgop") - .arg("-g") - .arg("30") + .arg("-crf") + .arg("23") + .arg("-preset") + .arg("veryfast") .arg("-hls_time") - .arg("5") + .arg("3") + .arg("-hls_list_size") + .arg("100") + .arg("-vf") + .arg("scale=1080:-2,setsar=1:1") .arg(playlist_file) .output() .expect("Expected this to work..");