mirror of
https://github.com/crunchy-labs/crunchy-cli.git
synced 2026-01-21 12:12:00 -06:00
Move ffmpeg presets to utils
This commit is contained in:
parent
2e4e897dc1
commit
2f7e992a6e
2 changed files with 125 additions and 127 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use crate::utils::context::Context;
|
||||
use anyhow::Result;
|
||||
use anyhow::{bail, Result};
|
||||
use crunchyroll_rs::media::{Resolution, VariantData, VariantSegment};
|
||||
use isahc::AsyncReadResponseExt;
|
||||
use log::{debug, LevelFilter};
|
||||
|
|
@ -183,3 +183,124 @@ pub async fn download_segments(
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum FFmpegPreset {
|
||||
Nvidia,
|
||||
|
||||
Av1,
|
||||
H265,
|
||||
H264,
|
||||
}
|
||||
|
||||
impl ToString for FFmpegPreset {
|
||||
fn to_string(&self) -> String {
|
||||
match self {
|
||||
&FFmpegPreset::Nvidia => "nvidia",
|
||||
&FFmpegPreset::Av1 => "av1",
|
||||
&FFmpegPreset::H265 => "h265",
|
||||
&FFmpegPreset::H264 => "h264",
|
||||
}
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl FFmpegPreset {
|
||||
pub(crate) fn all() -> Vec<FFmpegPreset> {
|
||||
vec![
|
||||
FFmpegPreset::Nvidia,
|
||||
FFmpegPreset::Av1,
|
||||
FFmpegPreset::H265,
|
||||
FFmpegPreset::H264,
|
||||
]
|
||||
}
|
||||
|
||||
pub(crate) fn description(self) -> String {
|
||||
match self {
|
||||
FFmpegPreset::Nvidia => "If you're have a nvidia card, use hardware / gpu accelerated video processing if available",
|
||||
FFmpegPreset::Av1 => "Encode the video(s) with the av1 codec. Hardware acceleration is currently not possible with this",
|
||||
FFmpegPreset::H265 => "Encode the video(s) with the h265 codec",
|
||||
FFmpegPreset::H264 => "Encode the video(s) with the h264 codec"
|
||||
}.to_string()
|
||||
}
|
||||
|
||||
pub(crate) fn parse(s: &str) -> Result<FFmpegPreset, String> {
|
||||
Ok(match s.to_lowercase().as_str() {
|
||||
"nvidia" => FFmpegPreset::Nvidia,
|
||||
"av1" => FFmpegPreset::Av1,
|
||||
"h265" | "h.265" | "hevc" => FFmpegPreset::H265,
|
||||
"h264" | "h.264" => FFmpegPreset::H264,
|
||||
_ => return Err(format!("'{}' is not a valid ffmpeg preset", s)),
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn ffmpeg_presets(mut presets: Vec<FFmpegPreset>) -> Result<(Vec<String>, Vec<String>)> {
|
||||
fn preset_check_remove(presets: &mut Vec<FFmpegPreset>, preset: FFmpegPreset) -> bool {
|
||||
if let Some(i) = presets.iter().position(|p| p == &preset) {
|
||||
presets.remove(i);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
let nvidia = preset_check_remove(&mut presets, FFmpegPreset::Nvidia);
|
||||
if presets.len() > 1 {
|
||||
bail!(
|
||||
"Can only use one video codec, {} found: {}",
|
||||
presets.len(),
|
||||
presets
|
||||
.iter()
|
||||
.map(|p| p.to_string())
|
||||
.collect::<Vec<String>>()
|
||||
.join(", ")
|
||||
)
|
||||
}
|
||||
|
||||
let (mut input, mut output) = (vec![], vec![]);
|
||||
for preset in presets {
|
||||
if nvidia {
|
||||
match preset {
|
||||
FFmpegPreset::Av1 => bail!("'nvidia' hardware acceleration preset is not available in combination with the 'av1' codec preset"),
|
||||
FFmpegPreset::H265 => {
|
||||
input.extend(["-hwaccel", "cuvid", "-c:v", "h264_cuvid"]);
|
||||
output.extend(["-c:v", "hevc_nvenc"]);
|
||||
}
|
||||
FFmpegPreset::H264 => {
|
||||
input.extend(["-hwaccel", "cuvid", "-c:v", "h264_cuvid"]);
|
||||
output.extend(["-c:v", "h264_nvenc"]);
|
||||
}
|
||||
_ => ()
|
||||
}
|
||||
} else {
|
||||
match preset {
|
||||
FFmpegPreset::Av1 => {
|
||||
output.extend(["-c:v", "libaom-av1"]);
|
||||
}
|
||||
FFmpegPreset::H265 => {
|
||||
output.extend(["-c:v", "libx265"]);
|
||||
}
|
||||
FFmpegPreset::H264 => {
|
||||
output.extend(["-c:v", "libx264"]);
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if input.is_empty() && output.is_empty() {
|
||||
output.extend(["-c", "copy"])
|
||||
} else {
|
||||
if output.is_empty() {
|
||||
output.extend(["-c", "copy"])
|
||||
} else {
|
||||
output.extend(["-c:a", "copy", "-c:s", "copy"])
|
||||
}
|
||||
}
|
||||
|
||||
Ok((
|
||||
input.into_iter().map(|i| i.to_string()).collect(),
|
||||
output.into_iter().map(|o| o.to_string()).collect(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue