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:
21
src/main.rs
21
src/main.rs
@@ -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));
|
||||
|
||||
HttpResponse::Ok()
|
||||
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 {
|
||||
HttpResponse::BadRequest()
|
||||
return HttpResponse::BadRequest().finish();
|
||||
}
|
||||
|
||||
HttpResponse::Ok().json(playlist)
|
||||
} else {
|
||||
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()
|
||||
|
||||
21
src/video.rs
21
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..");
|
||||
|
||||
Reference in New Issue
Block a user