global hotkeys wired up
This commit is contained in:
+87
-14
@@ -1,3 +1,4 @@
|
||||
use std::collections::HashSet;
|
||||
use std::ffi::{OsStr, OsString};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
@@ -69,19 +70,83 @@ pub fn transcode(ffmpeg: &Path, req: &TranscodeRequest) -> Result<()> {
|
||||
std::fs::create_dir_all(parent).context("creating output dir")?;
|
||||
}
|
||||
|
||||
let encoders = list_encoders(ffmpeg)?;
|
||||
|
||||
match req.format {
|
||||
Format::Mp4 => two_pass_video(ffmpeg, req, mp4_video_codec(req.mp4_codec), "aac", "mp4"),
|
||||
Format::Webm => two_pass_video(
|
||||
ffmpeg,
|
||||
req,
|
||||
webm_video_codec(req.webm_codec),
|
||||
"libopus",
|
||||
"webm",
|
||||
),
|
||||
Format::Mp4 => {
|
||||
let codec = pick_video_codec(
|
||||
&encoders,
|
||||
&[mp4_video_codec(req.mp4_codec), "libx264"],
|
||||
)?;
|
||||
let audio = pick_audio_codec(&encoders, &["aac", "libfdk_aac"])?;
|
||||
two_pass_video(ffmpeg, req, codec, audio, "mp4")
|
||||
}
|
||||
Format::Webm => {
|
||||
let codec = pick_video_codec(
|
||||
&encoders,
|
||||
&[webm_video_codec(req.webm_codec), "libvpx-vp9", "libvpx"],
|
||||
)?;
|
||||
let audio = pick_audio_codec(&encoders, &["libopus", "libvorbis"])?;
|
||||
two_pass_video(ffmpeg, req, codec, audio, "webm")
|
||||
}
|
||||
Format::Gif => encode_gif(ffmpeg, req),
|
||||
}
|
||||
}
|
||||
|
||||
fn list_encoders(ffmpeg: &Path) -> Result<HashSet<String>> {
|
||||
let out = Command::new(ffmpeg)
|
||||
.args(["-hide_banner", "-encoders"])
|
||||
.output()
|
||||
.context("listing ffmpeg encoders")?;
|
||||
let text = String::from_utf8_lossy(&out.stdout);
|
||||
let mut set: HashSet<String> = HashSet::new();
|
||||
for line in text.lines() {
|
||||
let trimmed = line.trim_start();
|
||||
let mut iter = trimmed.split_whitespace();
|
||||
let (Some(flags), Some(name)) = (iter.next(), iter.next()) else {
|
||||
continue;
|
||||
};
|
||||
if flags.len() != 6 {
|
||||
continue;
|
||||
}
|
||||
if !matches!(flags.chars().next(), Some('V' | 'A' | 'S')) {
|
||||
continue;
|
||||
}
|
||||
set.insert(name.to_string());
|
||||
}
|
||||
Ok(set)
|
||||
}
|
||||
|
||||
fn pick_video_codec<'a>(
|
||||
encoders: &HashSet<String>,
|
||||
candidates: &[&'a str],
|
||||
) -> Result<&'a str> {
|
||||
for c in candidates {
|
||||
if encoders.contains(*c) {
|
||||
return Ok(c);
|
||||
}
|
||||
}
|
||||
bail!(
|
||||
"no video encoder available - tried {}",
|
||||
candidates.join(", ")
|
||||
)
|
||||
}
|
||||
|
||||
fn pick_audio_codec<'a>(
|
||||
encoders: &HashSet<String>,
|
||||
candidates: &[&'a str],
|
||||
) -> Result<&'a str> {
|
||||
for c in candidates {
|
||||
if encoders.contains(*c) {
|
||||
return Ok(c);
|
||||
}
|
||||
}
|
||||
bail!(
|
||||
"no audio encoder available - tried {}",
|
||||
candidates.join(", ")
|
||||
)
|
||||
}
|
||||
|
||||
fn mp4_video_codec(c: Mp4Codec) -> &'static str {
|
||||
match c {
|
||||
Mp4Codec::H264 => "libx264",
|
||||
@@ -97,7 +162,7 @@ fn webm_video_codec(c: WebmCodec) -> &'static str {
|
||||
}
|
||||
|
||||
fn video_bitrate_kbps(req: &TranscodeRequest) -> Result<u64> {
|
||||
let target_bits = req.target_bytes.saturating_mul(8) as f64;
|
||||
let target_bits = (req.target_bytes as f64) * 8.0 * 0.95;
|
||||
let audio_bits = if req.keep_audio {
|
||||
AUDIO_BITRATE_KBPS as f64 * 1000.0 * req.duration_secs
|
||||
} else {
|
||||
@@ -118,10 +183,11 @@ fn two_pass_video(
|
||||
audio_codec: &str,
|
||||
container: &str,
|
||||
) -> Result<()> {
|
||||
let _ = container;
|
||||
let bitrate_kbps = video_bitrate_kbps(req)?;
|
||||
let bitrate = format!("{bitrate_kbps}k");
|
||||
|
||||
let log_stem = req.scratch_dir.join(format!("ffmpeg2pass-{container}"));
|
||||
let log_stem = req.scratch_dir.join("ffmpeg2pass");
|
||||
let log_stem_str = log_stem
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow!("scratch dir contains non-UTF8"))?;
|
||||
@@ -145,7 +211,7 @@ fn two_pass_video(
|
||||
.args(["-pass", "1"])
|
||||
.args(["-passlogfile", log_stem_str])
|
||||
.arg("-an")
|
||||
.args(["-f", container])
|
||||
.args(["-f", "null"])
|
||||
.arg(null_target);
|
||||
run(&mut pass1, "pass 1")?;
|
||||
|
||||
@@ -355,6 +421,8 @@ pub fn concat_segments(ffmpeg: &Path, segments: &[PathBuf], output: &Path) -> Re
|
||||
}
|
||||
|
||||
fn run(cmd: &mut Command, label: &str) -> Result<()> {
|
||||
use std::process::Stdio;
|
||||
|
||||
let mut printable = OsString::from(label);
|
||||
printable.push(": ");
|
||||
printable.push(cmd.get_program());
|
||||
@@ -364,9 +432,14 @@ fn run(cmd: &mut Command, label: &str) -> Result<()> {
|
||||
}
|
||||
tracing::debug!("{}", printable.to_string_lossy());
|
||||
|
||||
let status = cmd.status().with_context(|| format!("spawning ffmpeg for {label}"))?;
|
||||
if !status.success() {
|
||||
bail!("ffmpeg {label} exited with {status}");
|
||||
cmd.stdout(Stdio::null()).stderr(Stdio::piped());
|
||||
let output = cmd
|
||||
.output()
|
||||
.with_context(|| format!("spawning ffmpeg for {label}"))?;
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
let tail: String = stderr.lines().rev().take(12).collect::<Vec<_>>().into_iter().rev().collect::<Vec<_>>().join("\n");
|
||||
bail!("ffmpeg {label} exited with {}: {}", output.status, tail);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user