HLS/previews: rerun on the CPU when the GPU can't be used right now #216
No files matched your search
+125
-64
@@ -635,6 +635,95 @@ impl Handler<GeneratePlaylistMessage> for PlaylistGenerator {
|
||||
|
||||
let nvenc = !use_copy && crate::video::ffmpeg::is_nvenc_available().await;
|
||||
|
||||
// Default timeout is generous — a long 4K transcode on CPU can take hours.
|
||||
let timeout_secs = std::env::var("HLS_TIMEOUT_SECONDS")
|
||||
.ok()
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.unwrap_or(7200);
|
||||
|
||||
let mut ffmpeg_result = run_ffmpeg(
|
||||
hls_command(&video_file, &playlist_tmp, &segment_pattern, use_copy, nvenc),
|
||||
timeout_secs,
|
||||
)
|
||||
.await;
|
||||
|
||||
// A GPU that's busy right now (#215: llama-swap holding the 3090)
|
||||
// says nothing about the file. Encode on the CPU instead of
|
||||
// writing a sentinel that would skip this video for good.
|
||||
if nvenc
|
||||
&& matches!(&ffmpeg_result, Ok(out) if !out.status.success()
|
||||
&& crate::video::ffmpeg::is_gpu_init_failure(&String::from_utf8_lossy(&out.stderr)))
|
||||
{
|
||||
warn!(
|
||||
"GPU unavailable for {}, retrying the playlist on the CPU encoder",
|
||||
video_file
|
||||
);
|
||||
span.add_event("GPU unavailable, retrying on CPU", vec![]);
|
||||
cleanup_partial_hls(&hash_dir).await;
|
||||
ffmpeg_result = run_ffmpeg(
|
||||
hls_command(&video_file, &playlist_tmp, &segment_pattern, use_copy, false),
|
||||
timeout_secs,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
drop(permit);
|
||||
|
||||
let success = matches!(&ffmpeg_result, Ok(out) if out.status.success());
|
||||
|
||||
if success {
|
||||
if let Err(e) = tokio::fs::rename(&playlist_tmp, &playlist_path).await {
|
||||
error!(
|
||||
"ffmpeg succeeded but rename {} -> {} failed: {}",
|
||||
playlist_tmp, playlist_file, e
|
||||
);
|
||||
cleanup_partial_hls(&hash_dir).await;
|
||||
span.set_status(Status::error(format!("rename failed: {}", e)));
|
||||
return Err(e);
|
||||
}
|
||||
debug!("Playlist complete: {}", playlist_file);
|
||||
span.set_status(Status::Ok);
|
||||
Ok(())
|
||||
} else {
|
||||
let detail = match &ffmpeg_result {
|
||||
Ok(out) => format!(
|
||||
"exit {}: {}",
|
||||
out.status,
|
||||
String::from_utf8_lossy(&out.stderr).trim()
|
||||
),
|
||||
Err(e) => format!("ffmpeg failed: {}", e),
|
||||
};
|
||||
error!("ffmpeg failed for {}: {}", video_file, detail);
|
||||
cleanup_partial_hls(&hash_dir).await;
|
||||
if let Err(se) = tokio::fs::write(&sentinel_path, b"").await {
|
||||
warn!(
|
||||
"Failed to write playlist sentinel {}: {}",
|
||||
sentinel_path.display(),
|
||||
se
|
||||
);
|
||||
} else {
|
||||
info!(
|
||||
"Wrote playlist sentinel {} so future scans skip {}",
|
||||
sentinel_path.display(),
|
||||
video_file
|
||||
);
|
||||
}
|
||||
span.set_status(Status::error(detail.clone()));
|
||||
Err(std::io::Error::other(detail))
|
||||
}
|
||||
}
|
||||
.instrument(fut_span))
|
||||
}
|
||||
}
|
||||
|
||||
/// The ffmpeg command that writes a video's HLS playlist and segments.
|
||||
fn hls_command(
|
||||
video_file: &str,
|
||||
playlist_tmp: &str,
|
||||
segment_pattern: &str,
|
||||
use_copy: bool,
|
||||
nvenc: bool,
|
||||
) -> tokio::process::Command {
|
||||
let mut cmd = tokio::process::Command::new("ffmpeg");
|
||||
cmd.arg("-y");
|
||||
if nvenc {
|
||||
@@ -645,7 +734,7 @@ impl Handler<GeneratePlaylistMessage> for PlaylistGenerator {
|
||||
// back to software decode for codecs NVDEC lacks (ProRes).
|
||||
cmd.arg("-hwaccel").arg("cuda");
|
||||
}
|
||||
cmd.arg("-i").arg(&video_file);
|
||||
cmd.arg("-i").arg(video_file);
|
||||
|
||||
if use_copy {
|
||||
cmd.arg("-c:v").arg("copy");
|
||||
@@ -701,21 +790,21 @@ impl Handler<GeneratePlaylistMessage> for PlaylistGenerator {
|
||||
// decoded without reference to any other — the matching guarantee
|
||||
// for the forced keyframes above.
|
||||
cmd.arg("-hls_flags").arg("independent_segments");
|
||||
cmd.arg("-hls_segment_filename").arg(&segment_pattern);
|
||||
cmd.arg(&playlist_tmp);
|
||||
cmd.arg("-hls_segment_filename").arg(segment_pattern);
|
||||
cmd.arg(playlist_tmp);
|
||||
cmd.stdout(Stdio::null());
|
||||
cmd.stderr(Stdio::piped());
|
||||
cmd.kill_on_drop(true);
|
||||
cmd
|
||||
}
|
||||
|
||||
// Spawn + wait under a timeout so a hung ffmpeg (corrupt source,
|
||||
// NFS stall, etc.) doesn't permanently hold a semaphore slot.
|
||||
// Default is generous — a long 4K transcode on CPU can take hours.
|
||||
let timeout_secs = std::env::var("HLS_TIMEOUT_SECONDS")
|
||||
.ok()
|
||||
.and_then(|v| v.parse::<u64>().ok())
|
||||
.unwrap_or(7200);
|
||||
|
||||
let ffmpeg_result = match cmd.spawn() {
|
||||
/// Run ffmpeg under a timeout, so a hung run (corrupt source, NFS stall)
|
||||
/// doesn't hold a semaphore slot forever.
|
||||
async fn run_ffmpeg(
|
||||
mut cmd: tokio::process::Command,
|
||||
timeout_secs: u64,
|
||||
) -> Result<std::process::Output> {
|
||||
match cmd.spawn() {
|
||||
Ok(child) => {
|
||||
match tokio::time::timeout(
|
||||
std::time::Duration::from_secs(timeout_secs),
|
||||
@@ -724,9 +813,7 @@ impl Handler<GeneratePlaylistMessage> for PlaylistGenerator {
|
||||
.await
|
||||
{
|
||||
Ok(res) => res
|
||||
.inspect_err(|e| {
|
||||
error!("Failed to wait on ffmpeg child process: {}", e)
|
||||
})
|
||||
.inspect_err(|e| error!("Failed to wait on ffmpeg child process: {}", e))
|
||||
.map_err(|e| std::io::Error::other(e.to_string())),
|
||||
Err(_) => Err(std::io::Error::other(format!(
|
||||
"ffmpeg exceeded {}s timeout",
|
||||
@@ -738,54 +825,6 @@ impl Handler<GeneratePlaylistMessage> for PlaylistGenerator {
|
||||
error!("Failed to spawn ffmpeg: {}", e);
|
||||
Err(std::io::Error::other(e.to_string()))
|
||||
}
|
||||
};
|
||||
|
||||
drop(permit);
|
||||
|
||||
let success = matches!(&ffmpeg_result, Ok(out) if out.status.success());
|
||||
|
||||
if success {
|
||||
if let Err(e) = tokio::fs::rename(&playlist_tmp, &playlist_path).await {
|
||||
error!(
|
||||
"ffmpeg succeeded but rename {} -> {} failed: {}",
|
||||
playlist_tmp, playlist_file, e
|
||||
);
|
||||
cleanup_partial_hls(&hash_dir).await;
|
||||
span.set_status(Status::error(format!("rename failed: {}", e)));
|
||||
return Err(e);
|
||||
}
|
||||
debug!("Playlist complete: {}", playlist_file);
|
||||
span.set_status(Status::Ok);
|
||||
Ok(())
|
||||
} else {
|
||||
let detail = match &ffmpeg_result {
|
||||
Ok(out) => format!(
|
||||
"exit {}: {}",
|
||||
out.status,
|
||||
String::from_utf8_lossy(&out.stderr).trim()
|
||||
),
|
||||
Err(e) => format!("ffmpeg failed: {}", e),
|
||||
};
|
||||
error!("ffmpeg failed for {}: {}", video_file, detail);
|
||||
cleanup_partial_hls(&hash_dir).await;
|
||||
if let Err(se) = tokio::fs::write(&sentinel_path, b"").await {
|
||||
warn!(
|
||||
"Failed to write playlist sentinel {}: {}",
|
||||
sentinel_path.display(),
|
||||
se
|
||||
);
|
||||
} else {
|
||||
info!(
|
||||
"Wrote playlist sentinel {} so future scans skip {}",
|
||||
sentinel_path.display(),
|
||||
video_file
|
||||
);
|
||||
}
|
||||
span.set_status(Status::error(detail.clone()));
|
||||
Err(std::io::Error::other(detail))
|
||||
}
|
||||
}
|
||||
.instrument(fut_span))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -958,7 +997,29 @@ impl Handler<GeneratePreviewClipMessage> for PreviewClipGenerator {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{VideoStreamMeta, fits_hls_copy_limits, parse_ffprobe_rational};
|
||||
use super::{VideoStreamMeta, fits_hls_copy_limits, hls_command, parse_ffprobe_rational};
|
||||
|
||||
fn args(nvenc: bool) -> Vec<String> {
|
||||
hls_command("in.mp4", "out.m3u8.tmp", "seg_%03d.ts", false, nvenc)
|
||||
.as_std()
|
||||
.get_args()
|
||||
.map(|a| a.to_string_lossy().into_owned())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cpu_fallback_command_never_touches_the_gpu() {
|
||||
// #215: the retry after a GPU init failure must not ask for CUDA again.
|
||||
let cpu = args(false);
|
||||
assert!(!cpu.iter().any(|a| a == "-hwaccel" || a == "h264_nvenc"));
|
||||
assert!(cpu.windows(2).any(|w| w[0] == "-c:v" && w[1] == "h264"));
|
||||
let gpu = args(true);
|
||||
assert!(gpu.windows(2).any(|w| w[0] == "-hwaccel" && w[1] == "cuda"));
|
||||
assert!(
|
||||
gpu.windows(2)
|
||||
.any(|w| w[0] == "-c:v" && w[1] == "h264_nvenc")
|
||||
);
|
||||
}
|
||||
|
||||
const LIMIT: u64 = 20_000_000;
|
||||
|
||||
|
||||
+84
-21
@@ -36,6 +36,24 @@ pub async fn is_nvenc_available() -> bool {
|
||||
available
|
||||
}
|
||||
|
||||
/// Whether ffmpeg's stderr says the GPU couldn't be used *right now*, as
|
||||
/// opposed to the input being bad (#215). `is_nvenc_available` only checks
|
||||
/// that ffmpeg was built with NVENC; on a GPU shared with llama-swap a resident
|
||||
/// model can leave no memory for a CUDA context, and ffmpeg then exits 187
|
||||
/// with `cuCtxCreate(...) failed -> CUDA_ERROR_OUT_OF_MEMORY`. Callers rerun
|
||||
/// such a failure on the CPU encoder instead of recording the file as broken.
|
||||
pub fn is_gpu_init_failure(stderr: &str) -> bool {
|
||||
const MARKERS: &[&str] = &[
|
||||
"CUDA_ERROR_",
|
||||
"cuCtxCreate",
|
||||
"OpenEncodeSessionEx failed",
|
||||
"No capable devices found",
|
||||
"Cannot load libcuda",
|
||||
"Device creation failed",
|
||||
];
|
||||
MARKERS.iter().any(|m| stderr.contains(m))
|
||||
}
|
||||
|
||||
pub struct Ffmpeg;
|
||||
|
||||
pub enum GifType {
|
||||
@@ -271,25 +289,13 @@ fn parse_ffprobe_duration(stdout: &str) -> Option<f64> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Generate a preview clip from a video file.
|
||||
///
|
||||
/// Creates a ~10 second MP4 by extracting up to 10 equally-spaced 1-second segments
|
||||
/// at 480p with H.264 video and AAC audio. For short videos (<10s), uses fewer segments.
|
||||
/// For very short videos (<1s), transcodes the entire video.
|
||||
///
|
||||
/// Returns (duration_seconds, file_size_bytes) on success.
|
||||
pub async fn generate_preview_clip(input_file: &str, output_file: &str) -> Result<(f64, u64)> {
|
||||
info!("Generating preview clip for: '{}'", input_file);
|
||||
let start = Instant::now();
|
||||
|
||||
let duration = get_duration_seconds(input_file).await?;
|
||||
let use_nvenc = is_nvenc_available().await;
|
||||
|
||||
// Create parent directories for output
|
||||
if let Some(parent) = std::path::Path::new(output_file).parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
/// The ffmpeg command for a preview clip, and the clip duration it produces.
|
||||
fn preview_clip_command(
|
||||
input_file: &str,
|
||||
output_file: &str,
|
||||
duration: Option<f64>,
|
||||
use_nvenc: bool,
|
||||
) -> (Command, f64) {
|
||||
let mut cmd = Command::new("ffmpeg");
|
||||
cmd.arg("-y");
|
||||
|
||||
@@ -350,7 +356,43 @@ pub async fn generate_preview_clip(input_file: &str, output_file: &str) -> Resul
|
||||
cmd.stdout(Stdio::null());
|
||||
cmd.stderr(Stdio::piped());
|
||||
|
||||
let output = cmd.output().await?;
|
||||
(cmd, clip_duration)
|
||||
}
|
||||
|
||||
/// Generate a preview clip from a video file.
|
||||
///
|
||||
/// Creates a ~10 second MP4 by extracting up to 10 equally-spaced 1-second segments
|
||||
/// at 480p with H.264 video and AAC audio. For short videos (<10s), uses fewer segments.
|
||||
/// For very short videos (<1s), transcodes the entire video.
|
||||
///
|
||||
/// Returns (duration_seconds, file_size_bytes) on success.
|
||||
pub async fn generate_preview_clip(input_file: &str, output_file: &str) -> Result<(f64, u64)> {
|
||||
info!("Generating preview clip for: '{}'", input_file);
|
||||
let start = Instant::now();
|
||||
|
||||
let duration = get_duration_seconds(input_file).await?;
|
||||
let use_nvenc = is_nvenc_available().await;
|
||||
|
||||
// Create parent directories for output
|
||||
if let Some(parent) = std::path::Path::new(output_file).parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let (mut cmd, clip_duration) =
|
||||
preview_clip_command(input_file, output_file, duration, use_nvenc);
|
||||
let mut output = cmd.output().await?;
|
||||
|
||||
if use_nvenc
|
||||
&& !output.status.success()
|
||||
&& is_gpu_init_failure(&String::from_utf8_lossy(&output.stderr))
|
||||
{
|
||||
warn!(
|
||||
"GPU unavailable for preview of '{}', retrying on the CPU encoder",
|
||||
input_file
|
||||
);
|
||||
let (mut cmd, _) = preview_clip_command(input_file, output_file, duration, false);
|
||||
output = cmd.output().await?;
|
||||
}
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
@@ -376,7 +418,28 @@ pub async fn generate_preview_clip(input_file: &str, output_file: &str) -> Resul
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::parse_ffprobe_duration;
|
||||
use super::{is_gpu_init_failure, parse_ffprobe_duration};
|
||||
|
||||
#[test]
|
||||
fn gpu_busy_is_a_gpu_init_failure() {
|
||||
// Verbatim from the e2e server with a llama-swap model resident (#215).
|
||||
let oom = "[CUDA @ 0x5d1c2c0] cu->cuCtxCreate(&hwctx->cuda_ctx, desired_flags, hwctx->internal->cuda_device) failed -> CUDA_ERROR_OUT_OF_MEMORY: out of memory\nDevice creation failed: -542398533.";
|
||||
assert!(is_gpu_init_failure(oom));
|
||||
assert!(is_gpu_init_failure(
|
||||
"[h264_nvenc @ 0x1] OpenEncodeSessionEx failed: out of memory (10): (no details)"
|
||||
));
|
||||
assert!(is_gpu_init_failure(
|
||||
"[h264_nvenc @ 0x1] No capable devices found"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_input_is_not_a_gpu_init_failure() {
|
||||
assert!(!is_gpu_init_failure(
|
||||
"[mov,mp4,m4a,3gp,3g2,mj2 @ 0x1] moov atom not found\nclip.mp4: Invalid data found when processing input"
|
||||
));
|
||||
assert!(!is_gpu_init_failure("Conversion failed!"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_output_returns_none() {
|
||||
|
||||
Reference in new issue
Block a user