Switch to the hls encoder instead of segments

This commit is contained in:
Zoe Roux 2023-04-27 17:56:42 +09:00
parent 98f6fda99f
commit 4e4a90b2d2
No known key found for this signature in database

View File

@ -1,5 +1,6 @@
use rand::distributions::Alphanumeric; use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng}; use rand::{thread_rng, Rng};
use serde::Serialize;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Stdio; use std::process::Stdio;
@ -11,7 +12,7 @@ use tokio::sync::watch::{self, Receiver};
use crate::utils::Signalable; use crate::utils::Signalable;
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq, Serialize)]
pub enum Quality { pub enum Quality {
P240, P240,
P360, P360,
@ -78,18 +79,18 @@ async fn start_transcode(path: String, quality: Quality, start_time: u32) -> Tra
std::fs::create_dir(&out_dir).expect("Could not create cache directory"); std::fs::create_dir(&out_dir).expect("Could not create cache directory");
let segment_time: u32 = 10; let segment_time: u32 = 10;
let mut child = Command::new("ffmpeg") let mut cmd = Command::new("ffmpeg");
.args(&["-progress", "pipe:1"]) cmd.args(&["-progress", "pipe:1"])
.arg("-nostats") .arg("-nostats")
.args(&["-ss", start_time.to_string().as_str()]) .args(&["-ss", start_time.to_string().as_str()])
.args(&["-i", path.as_str()]) .args(&["-i", path.as_str()])
.args(&["-f", "segment"]) .args(&["-f", "hls"])
.args(&["-segment_list_type", "m3u8"])
// Use a .tmp file for segments (.ts files) // Use a .tmp file for segments (.ts files)
.args(&["-hls_flags", "temp_files"]) .args(&["-hls_flags", "temp_file"])
.args(&["-hls_allow_cache", "1"])
// Keep all segments in the list (else only last X are presents, useful for livestreams) // Keep all segments in the list (else only last X are presents, useful for livestreams)
.args(&["-segment_list_size", "0"]) .args(&["-hls_list_size", "0"])
.args(&["-segment_time", segment_time.to_string().as_str()]) .args(&["-hls_time", segment_time.to_string().as_str()])
// Force segments to be exactly segment_time (only works when transcoding) // Force segments to be exactly segment_time (only works when transcoding)
.args(&[ .args(&[
"-force_key_frames", "-force_key_frames",
@ -101,16 +102,13 @@ async fn start_transcode(path: String, quality: Quality, start_time: u32) -> Tra
]) ])
.args(get_transcode_video_quality_args(&quality)) .args(get_transcode_video_quality_args(&quality))
.args(&[ .args(&[
"-segment_list".to_string(), "-hls_segment_filename".to_string(),
format!("{out_dir}/stream.m3u8"),
format!("{out_dir}/segments-%02d.ts"), format!("{out_dir}/segments-%02d.ts"),
format!("{out_dir}/stream.m3u8"),
]) ])
// TODO: Figure out which flag would be the best. .stdout(Stdio::piped());
.args(&["-segment_list_flags", "live"]) println!("Starting a transcode with the command: {:?}", cmd);
// .args(&["-segment_list_flags", "cache"]) let mut child = cmd.spawn().expect("ffmpeg failed to start");
.stdout(Stdio::piped())
.spawn()
.expect("ffmpeg failed to start");
let stdout = child.stdout.take().unwrap(); let stdout = child.stdout.take().unwrap();
let (tx, mut rx) = watch::channel(0u32); let (tx, mut rx) = watch::channel(0u32);