Check and use correct paths for generating HLS playlist

This also should prevent generating the playlist if it already exists
and uses a better ffmpeg command for transcoding the video.
This commit is contained in:
Cameron Cordes
2020-07-14 15:45:10 -04:00
parent c39bf970be
commit ca761f605b
2 changed files with 28 additions and 12 deletions

View File

@@ -126,12 +126,17 @@ async fn generate_video(_claims: Claims, body: web::Json<ThumbnailRequest>) -> 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<ThumbnailRequest>)
#[get("/video/{path}")]
async fn get_video_part(request: HttpRequest, path: web::Path<ThumbnailRequest>) -> 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()

View File

@@ -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..");