Compare commits

..

No commits in common. "240624" and "240527" have entirely different histories.

10 changed files with 249 additions and 329 deletions

View file

@ -2,7 +2,6 @@ name: Trigger quay.io Webhook
on: on:
push: push:
branches: [main]
jobs: jobs:
run: run:

View file

@ -1,54 +0,0 @@
//! This module contains helper functions for implementing CLI/TUI.
use crate::time::{parse_time, Time};
use console::style;
use std::{
fmt::Display,
io::{self, BufRead as _, Write as _}
};
pub fn ask(question: impl Display) -> String {
let mut stdout = io::stdout().lock();
let mut stdin = io::stdin().lock();
write!(
stdout,
"{} {} ",
style(question).bold().magenta(),
style(">").cyan()
)
.unwrap();
stdout.flush().unwrap();
let mut line = String::new();
stdin.read_line(&mut line).unwrap();
line.trim().to_owned()
}
pub fn ask_time(question: impl Display + Copy) -> Time {
let mut stdout = io::stdout().lock();
let mut stdin = io::stdin().lock();
let mut line = String::new();
loop {
line.clear();
write!(
stdout,
"{} {} ",
style(question).bold().magenta(),
style(">").cyan()
)
.unwrap();
stdout.flush().unwrap();
stdin.read_line(&mut line).unwrap();
let line = line.trim();
match parse_time(line) {
Ok(time) => return time,
Err(err) => writeln!(
stdout,
"{} {line:?}: {err}",
style("Invalid Input").bold().red()
)
.unwrap()
}
}
}

View file

@ -1,9 +1,6 @@
//! A module for writing intros and outros //! A module for writing intros and outros
use crate::{ use crate::{time::Date, ProjectLecture, Resolution};
project::{ProjectLecture, Resolution},
time::Date
};
use anyhow::anyhow; use anyhow::anyhow;
use std::{ use std::{
fmt::{self, Debug, Display, Formatter}, fmt::{self, Debug, Display, Formatter},

View file

@ -1,17 +0,0 @@
#![allow(clippy::manual_range_contains)]
#![warn(clippy::unreadable_literal, rust_2018_idioms)]
#![forbid(elided_lifetimes_in_paths, unsafe_code)]
pub mod cli;
pub mod iotro;
pub mod preset;
pub mod project;
pub mod question;
pub mod render;
pub mod time;
#[cfg(feature = "mem_limit")]
use std::sync::RwLock;
#[cfg(feature = "mem_limit")]
pub static MEM_LIMIT: RwLock<String> = RwLock::new(String::new());

View file

@ -2,17 +2,32 @@
#![warn(clippy::unreadable_literal, rust_2018_idioms)] #![warn(clippy::unreadable_literal, rust_2018_idioms)]
#![forbid(elided_lifetimes_in_paths, unsafe_code)] #![forbid(elided_lifetimes_in_paths, unsafe_code)]
mod iotro;
mod preset;
mod project;
mod question;
mod render;
mod time;
use self::{
project::{Project, ProjectLecture, ProjectSource, Resolution},
render::Renderer,
time::{parse_date, parse_time, Time}
};
use crate::preset::Preset;
use camino::Utf8PathBuf as PathBuf; use camino::Utf8PathBuf as PathBuf;
use clap::Parser; use clap::Parser;
use console::style; use console::style;
use render_video::{ #[cfg(feature = "mem_limit")]
cli::{ask, ask_time}, use std::sync::RwLock;
preset::Preset, use std::{
project::{Project, ProjectLecture, ProjectSource, Resolution}, fmt::Display,
render::Renderer, fs,
time::parse_date io::{self, BufRead as _, Write}
}; };
use std::fs;
#[cfg(feature = "mem_limit")]
static MEM_LIMIT: RwLock<String> = RwLock::new(String::new());
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
struct Args { struct Args {
@ -45,12 +60,58 @@ struct Args {
stereo: bool stereo: bool
} }
fn ask(question: impl Display) -> String {
let mut stdout = io::stdout().lock();
let mut stdin = io::stdin().lock();
write!(
stdout,
"{} {} ",
style(question).bold().magenta(),
style(">").cyan()
)
.unwrap();
stdout.flush().unwrap();
let mut line = String::new();
stdin.read_line(&mut line).unwrap();
line.trim().to_owned()
}
fn ask_time(question: impl Display + Copy) -> Time {
let mut stdout = io::stdout().lock();
let mut stdin = io::stdin().lock();
let mut line = String::new();
loop {
line.clear();
write!(
stdout,
"{} {} ",
style(question).bold().magenta(),
style(">").cyan()
)
.unwrap();
stdout.flush().unwrap();
stdin.read_line(&mut line).unwrap();
let line = line.trim();
match parse_time(line) {
Ok(time) => return time,
Err(err) => writeln!(
stdout,
"{} {line:?}: {err}",
style("Invalid Input").bold().red()
)
.unwrap()
}
}
}
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
#[cfg(feature = "mem_limit")] #[cfg(feature = "mem_limit")]
{ {
*(render_video::MEM_LIMIT.write().unwrap()) = args.mem_limit; *(MEM_LIMIT.write().unwrap()) = args.mem_limit;
} }
// process arguments // process arguments
@ -74,8 +135,7 @@ fn main() {
let lower = name.to_ascii_lowercase(); let lower = name.to_ascii_lowercase();
if (lower.ends_with(".mp4") if (lower.ends_with(".mp4")
|| lower.ends_with(".mts") || lower.ends_with(".mts")
|| lower.ends_with(".mkv") || lower.ends_with(".mkv"))
|| lower.ends_with(".mov"))
&& !entry.file_type().unwrap().is_dir() && !entry.file_type().unwrap().is_dir()
{ {
files.push(String::from(name)); files.push(String::from(name));
@ -122,7 +182,7 @@ fn main() {
project project
}; };
let mut renderer = Renderer::new(&directory, &project).unwrap(); let renderer = Renderer::new(&directory, &project).unwrap();
let recording = renderer.recording_mkv(); let recording = renderer.recording_mkv();
// preprocess the video // preprocess the video
@ -215,7 +275,7 @@ fn main() {
// rescale the video // rescale the video
if let Some(lowest_res) = args.transcode.or(preset.transcode) { if let Some(lowest_res) = args.transcode.or(preset.transcode) {
for res in Resolution::STANDARD_RESOLUTIONS.into_iter().rev() { for res in Resolution::values().into_iter().rev() {
if res > project.source.metadata.as_ref().unwrap().source_res if res > project.source.metadata.as_ref().unwrap().source_res
|| res > args.transcode_start.unwrap_or(preset.transcode_start) || res > args.transcode_start.unwrap_or(preset.transcode_start)
|| res < lowest_res || res < lowest_res

View file

@ -11,59 +11,57 @@ use std::{fs, io};
#[serde_as] #[serde_as]
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct Preset { pub(crate) struct Preset {
// options for the intro slide // options for the intro slide
pub course: String, pub(crate) course: String,
pub label: String, pub(crate) label: String,
pub docent: String, pub(crate) docent: String,
/// Course language /// Course language
#[serde(default)] #[serde(default = "Default::default")]
#[serde_as(as = "DisplayFromStr")] #[serde_as(as = "DisplayFromStr")]
pub lang: Language<'static>, pub(crate) lang: Language<'static>,
// coding options // coding options
#[serde_as(as = "DisplayFromStr")] pub(crate) transcode_start: Resolution,
pub transcode_start: Resolution, pub(crate) transcode: Option<Resolution>
#[serde_as(as = "Option<DisplayFromStr>")]
pub transcode: Option<Resolution>
} }
pub fn preset_23ws_malo2() -> Preset { fn preset_23ws_malo2() -> Preset {
Preset { Preset {
course: "23ws-malo2".into(), course: "23ws-malo2".into(),
label: "Mathematische Logik II".into(), label: "Mathematische Logik II".into(),
docent: "Prof. E. Grädel".into(), docent: "Prof. E. Grädel".into(),
lang: GERMAN, lang: GERMAN,
transcode_start: "1440p".parse().unwrap(), transcode_start: Resolution::WQHD,
transcode: Some("360p".parse().unwrap()) transcode: Some(Resolution::nHD)
} }
} }
pub fn preset_24ss_algomod() -> Preset { fn preset_24ss_algomod() -> Preset {
Preset { Preset {
course: "24ss-algomod".into(), course: "24ss-algomod".into(),
label: "Algorithmische Modelltheorie".into(), label: "Algorithmische Modelltheorie".into(),
docent: "Prof. E. Grädel".into(), docent: "Prof. E. Grädel".into(),
lang: GERMAN, lang: GERMAN,
transcode_start: "1440p".parse().unwrap(), transcode_start: Resolution::WQHD,
transcode: Some("720p".parse().unwrap()) transcode: Some(Resolution::HD)
} }
} }
pub fn preset_24ss_qc() -> Preset { fn preset_24ss_qc() -> Preset {
Preset { Preset {
course: "24ss-qc".into(), course: "24ss-qc".into(),
label: "Introduction to Quantum Computing".into(), label: "Introduction to Quantum Computing".into(),
docent: "Prof. D. Unruh".into(), docent: "Prof. D. Unruh".into(),
lang: BRITISH, lang: BRITISH,
transcode_start: "1440p".parse().unwrap(), transcode_start: Resolution::WQHD,
transcode: Some("720p".parse().unwrap()) transcode: Some(Resolution::HD)
} }
} }
impl Preset { impl Preset {
pub fn find(name: &str) -> anyhow::Result<Self> { pub(crate) fn find(name: &str) -> anyhow::Result<Self> {
match fs::read(name) { match fs::read(name) {
Ok(buf) => return Ok(toml::from_slice(&buf)?), Ok(buf) => return Ok(toml::from_slice(&buf)?),
Err(err) if err.kind() == io::ErrorKind::NotFound => {}, Err(err) if err.kind() == io::ErrorKind::NotFound => {},

View file

@ -8,199 +8,157 @@ use crate::{
use rational::Rational; use rational::Rational;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr}; use serde_with::{serde_as, DisplayFromStr};
use std::{ use std::{collections::BTreeSet, str::FromStr};
cmp,
collections::BTreeSet,
fmt::{self, Display, Formatter},
str::FromStr
};
#[derive(Clone, Copy, Debug, Deserialize, Serialize)] macro_rules! resolutions {
pub struct Resolution(u32, u32); ($($res:ident: $width:literal x $height:literal at $bitrate:literal in $format:ident),+) => {
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
impl Resolution { #[derive(Clone, Copy, Debug, Deserialize, Eq, Ord, PartialEq, PartialOrd, Serialize)]
pub fn new(width: u32, height: u32) -> Self { pub(crate) enum Resolution {
Self(width, height) $(
} #[doc = concat!(stringify!($width), "x", stringify!($height))]
$res
pub fn width(self) -> u32 { ),+
self.0
}
pub fn height(self) -> u32 {
self.1
}
pub(crate) fn bitrate(self) -> u64 {
// 640 * 360: 500k
if self.width() <= 640 {
500_000
} }
// 1280 * 720: 1M
else if self.width() <= 1280 {
1_000_000
}
// 1920 * 1080: 2M
else if self.width() <= 1920 {
2_000_000
}
// 2560 * 1440: 3M
else if self.width() <= 2560 {
3_000_000
}
// 3840 * 2160: 4M
// TODO die bitrate von 4M ist absolut an den haaren herbeigezogen
else if self.width() <= 3840 {
4_000_000
}
// we'll cap everything else at 5M for no apparent reason
else {
5_000_000
}
}
pub(crate) fn default_codec(self) -> FfmpegOutputFormat { const NUM_RESOLUTIONS: usize = {
if self.width() > 1920 { let mut num = 0;
FfmpegOutputFormat::Av1Opus $(num += 1; stringify!($res);)+
} else { num
FfmpegOutputFormat::AvcAac };
impl Resolution {
pub(crate) fn values() -> [Self; NUM_RESOLUTIONS] {
[$(Self::$res),+]
}
pub(crate) fn width(self) -> usize {
match self {
$(Self::$res => $width),+
}
}
pub(crate) fn height(self) -> usize {
match self {
$(Self::$res => $height),+
}
}
pub(crate) fn bitrate(self) -> u64 {
match self {
$(Self::$res => $bitrate),+
}
}
pub(crate) fn format(self) -> FfmpegOutputFormat {
match self {
$(Self::$res => FfmpegOutputFormat::$format),+
}
}
} }
}
pub const STANDARD_RESOLUTIONS: [Self; 5] = [ impl FromStr for Resolution {
Self(640, 360), type Err = anyhow::Error;
Self(1280, 720),
Self(1920, 1080),
Self(2560, 1440),
Self(3840, 2160)
];
}
impl Display for Resolution { fn from_str(s: &str) -> anyhow::Result<Self> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { Ok(match s {
write!(f, "{}p", self.height()) $(concat!(stringify!($height), "p") => Self::$res,)+
_ => anyhow::bail!("Unknown Resolution: {s:?}")
})
}
}
} }
} }
impl FromStr for Resolution { resolutions! {
type Err = anyhow::Error; nHD: 640 x 360 at 500_000 in AvcAac,
HD: 1280 x 720 at 1_000_000 in AvcAac,
fn from_str(s: &str) -> anyhow::Result<Self> { FullHD: 1920 x 1080 at 750_000 in Av1Opus,
Ok(match s.to_lowercase().as_str() { WQHD: 2560 x 1440 at 1_000_000 in Av1Opus,
"360p" | "nhd" => Self(640, 360), // TODO qsx muss mal sagen wieviel bitrate für 4k
"540p" | "qhd" => Self(960, 540), UHD: 3840 x 2160 at 2_000_000 in Av1Opus
"720p" | "hd" => Self(1280, 720),
"900p" | "hd+" => Self(1600, 900),
"1080p" | "fhd" | "fullhd" => Self(1920, 1080),
"1440p" | "wqhd" => Self(2560, 1440),
"2160p" | "4k" | "uhd" => Self(3840, 2160),
_ => anyhow::bail!("Unknown Resolution: {s:?}")
})
}
} }
impl Ord for Resolution { #[derive(Deserialize, Serialize)]
fn cmp(&self, other: &Self) -> cmp::Ordering { pub(crate) struct Project {
(self.0 * self.1).cmp(&(other.0 * other.1)) pub(crate) lecture: ProjectLecture,
} pub(crate) source: ProjectSource,
} pub(crate) progress: ProjectProgress
impl Eq for Resolution {}
impl PartialOrd for Resolution {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Resolution {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == cmp::Ordering::Equal
}
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Project {
pub lecture: ProjectLecture,
pub source: ProjectSource,
pub progress: ProjectProgress
} }
#[serde_as] #[serde_as]
#[derive(Debug, Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct ProjectLecture { pub(crate) struct ProjectLecture {
pub course: String, pub(crate) course: String,
pub label: String, pub(crate) label: String,
pub docent: String, pub(crate) docent: String,
#[serde_as(as = "DisplayFromStr")] #[serde_as(as = "DisplayFromStr")]
pub date: Date, pub(crate) date: Date,
#[serde(default = "Default::default")] #[serde(default = "Default::default")]
#[serde_as(as = "DisplayFromStr")] #[serde_as(as = "DisplayFromStr")]
pub lang: Language<'static> pub(crate) lang: Language<'static>
} }
#[serde_as] #[serde_as]
#[derive(Debug, Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct ProjectSource { pub(crate) struct ProjectSource {
pub files: Vec<String>, pub(crate) files: Vec<String>,
pub stereo: bool, pub(crate) stereo: bool,
#[serde_as(as = "Option<DisplayFromStr>")] #[serde_as(as = "Option<DisplayFromStr>")]
pub start: Option<Time>, pub(crate) start: Option<Time>,
#[serde_as(as = "Option<DisplayFromStr>")] #[serde_as(as = "Option<DisplayFromStr>")]
pub end: Option<Time>, pub(crate) end: Option<Time>,
#[serde(default)] #[serde(default)]
#[serde_as(as = "Vec<(DisplayFromStr, DisplayFromStr)>")] #[serde_as(as = "Vec<(DisplayFromStr, DisplayFromStr)>")]
pub fast: Vec<(Time, Time)>, pub(crate) fast: Vec<(Time, Time)>,
#[serde(default)] #[serde(default)]
#[serde_as(as = "Vec<(DisplayFromStr, DisplayFromStr, _)>")] #[serde_as(as = "Vec<(DisplayFromStr, DisplayFromStr, _)>")]
pub questions: Vec<(Time, Time, String)>, pub(crate) questions: Vec<(Time, Time, String)>,
pub metadata: Option<ProjectSourceMetadata> pub(crate) metadata: Option<ProjectSourceMetadata>
} }
#[serde_as] #[serde_as]
#[derive(Debug, Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub struct ProjectSourceMetadata { pub(crate) struct ProjectSourceMetadata {
/// The duration of the source video. /// The duration of the source video.
#[serde_as(as = "DisplayFromStr")] #[serde_as(as = "DisplayFromStr")]
pub source_duration: Time, pub(crate) source_duration: Time,
/// The FPS of the source video. /// The FPS of the source video.
#[serde_as(as = "DisplayFromStr")] #[serde_as(as = "DisplayFromStr")]
pub source_fps: Rational, pub(crate) source_fps: Rational,
/// The time base of the source video. /// The time base of the source video.
#[serde_as(as = "DisplayFromStr")] #[serde_as(as = "DisplayFromStr")]
pub source_tbn: Rational, pub(crate) source_tbn: Rational,
/// The resolution of the source video. /// The resolution of the source video.
pub source_res: Resolution, pub(crate) source_res: Resolution,
/// The sample rate of the source audio. /// The sample rate of the source audio.
pub source_sample_rate: u32 pub(crate) source_sample_rate: u32
} }
#[serde_as] #[derive(Default, Deserialize, Serialize)]
#[derive(Debug, Default, Deserialize, Serialize)] pub(crate) struct ProjectProgress {
pub struct ProjectProgress {
#[serde(default)] #[serde(default)]
pub preprocessed: bool, pub(crate) preprocessed: bool,
#[serde(default)] #[serde(default)]
pub asked_start_end: bool, pub(crate) asked_start_end: bool,
#[serde(default)] #[serde(default)]
pub asked_fast: bool, pub(crate) asked_fast: bool,
#[serde(default)] #[serde(default)]
pub asked_questions: bool, pub(crate) asked_questions: bool,
#[serde(default)] #[serde(default)]
pub rendered_assets: bool, pub(crate) rendered_assets: bool,
#[serde(default)] #[serde(default)]
pub rendered: bool, pub(crate) rendered: bool,
#[serde_as(as = "BTreeSet<DisplayFromStr>")]
#[serde(default)] #[serde(default)]
pub transcoded: BTreeSet<Resolution> pub(crate) transcoded: BTreeSet<Resolution>
} }

View file

@ -1,4 +1,4 @@
use crate::{iotro::Language, project::Resolution}; use crate::{iotro::Language, Resolution};
use fontconfig::Fontconfig; use fontconfig::Fontconfig;
use harfbuzz_rs::{Face, Font, Owned, UnicodeBuffer}; use harfbuzz_rs::{Face, Font, Owned, UnicodeBuffer};
use std::sync::OnceLock; use std::sync::OnceLock;

View file

@ -1,8 +1,8 @@
use super::{cmd, filter::Filter}; use super::{cmd, filter::Filter};
use crate::{ use crate::{
project::Resolution,
render::filter::channel, render::filter::channel,
time::{format_time, Time} time::{format_time, Time},
Resolution
}; };
use anyhow::bail; use anyhow::bail;
use camino::Utf8PathBuf as PathBuf; use camino::Utf8PathBuf as PathBuf;
@ -41,8 +41,8 @@ impl FfmpegInput {
cmd.arg("-r").arg(fps.to_string()); cmd.arg("-r").arg(fps.to_string());
} }
if let Some(start) = self.start { if let Some(start) = self.start {
if self.path.ends_with(".mp4") || self.path.ends_with(".mov") { if self.path.ends_with(".mp4") {
cmd.arg("-seek_streams_individually").arg("false"); cmd.arg("-seek_streams_individualy").arg("false");
} }
cmd.arg("-ss").arg(format_time(start)); cmd.arg("-ss").arg(format_time(start));
} }
@ -59,30 +59,12 @@ pub(crate) enum FfmpegOutputFormat {
Av1Flac, Av1Flac,
/// AV1 / OPUS /// AV1 / OPUS
Av1Opus, Av1Opus,
/// AVC (H.264) / FLAC
AvcFlac,
/// AVC (H.264) / AAC /// AVC (H.264) / AAC
AvcAac AvcAac
} }
impl FfmpegOutputFormat {
pub(crate) fn with_flac_audio(self) -> Self {
match self {
Self::Av1Flac | Self::AvcFlac => self,
Self::Av1Opus => Self::Av1Flac,
Self::AvcAac => Self::AvcFlac
}
}
}
pub(crate) enum FfmpegOutputQuality {
Default,
VisuallyLossless
}
pub(crate) struct FfmpegOutput { pub(crate) struct FfmpegOutput {
pub(crate) format: FfmpegOutputFormat, pub(crate) format: FfmpegOutputFormat,
pub(crate) quality: FfmpegOutputQuality,
pub(crate) audio_bitrate: Option<u64>, pub(crate) audio_bitrate: Option<u64>,
pub(crate) video_bitrate: Option<u64>, pub(crate) video_bitrate: Option<u64>,
@ -107,7 +89,6 @@ impl FfmpegOutput {
pub(crate) fn new(format: FfmpegOutputFormat, path: PathBuf) -> Self { pub(crate) fn new(format: FfmpegOutputFormat, path: PathBuf) -> Self {
Self { Self {
format, format,
quality: FfmpegOutputQuality::Default,
audio_bitrate: None, audio_bitrate: None,
video_bitrate: None, video_bitrate: None,
@ -137,48 +118,41 @@ impl FfmpegOutput {
} }
fn append_to_cmd(self, cmd: &mut Command, venc: bool, _aenc: bool, vaapi: bool) { fn append_to_cmd(self, cmd: &mut Command, venc: bool, _aenc: bool, vaapi: bool) {
// select codec and bitrate/crf // select codec and bitrate
const QUALITY: &str = "18";
if venc { if venc {
let vcodec = match (self.format, vaapi) { let vcodec = match (self.format, vaapi) {
(FfmpegOutputFormat::Av1Flac, false) (FfmpegOutputFormat::Av1Flac, false)
| (FfmpegOutputFormat::Av1Opus, false) => "libsvtav1", | (FfmpegOutputFormat::Av1Opus, false) => "libsvtav1",
(FfmpegOutputFormat::Av1Flac, true) (FfmpegOutputFormat::Av1Flac, true)
| (FfmpegOutputFormat::Av1Opus, true) => "av1_vaapi", | (FfmpegOutputFormat::Av1Opus, true) => "av1_vaapi",
(FfmpegOutputFormat::AvcAac, false) (FfmpegOutputFormat::AvcAac, false) => "h264",
| (FfmpegOutputFormat::AvcFlac, false) => "h264", (FfmpegOutputFormat::AvcAac, true) => "h264_vaapi"
(FfmpegOutputFormat::AvcAac, true)
| (FfmpegOutputFormat::AvcFlac, true) => "h264_vaapi"
}; };
cmd.arg("-c:v").arg(vcodec); cmd.arg("-c:v").arg(vcodec);
if vcodec == "libsvtav1" { if vcodec == "libsvtav1" {
cmd.arg("-svtav1-params").arg("fast-decode=1"); cmd.arg("-svtav1-params").arg("fast-decode=1");
cmd.arg("-preset").arg("7"); cmd.arg("-preset").arg("8");
cmd.arg("-crf").arg(match self.quality { }
FfmpegOutputQuality::Default => "28",
FfmpegOutputQuality::VisuallyLossless => "18" match self.video_bitrate {
}); Some(bv) if vcodec != "libsvtav1" => {
} else if vcodec == "h264" { cmd.arg("-b:v").arg(bv.to_string());
match self.quality { },
FfmpegOutputQuality::Default => { None if vaapi => {
cmd.arg("-preset").arg("slow"); cmd.arg("-rc_mode").arg("CQP");
cmd.arg("-crf").arg("21"); cmd.arg("-global_quality").arg(QUALITY);
}, },
FfmpegOutputQuality::VisuallyLossless => { _ => {
// the quality is not impacted by speed, only the bitrate, and cmd.arg("-crf").arg(QUALITY);
// for this setting we don't really care about bitrate
cmd.arg("-preset").arg("veryfast");
cmd.arg("-crf").arg("17");
}
} }
} else if let Some(bv) = self.video_bitrate {
cmd.arg("-b:v").arg(bv.to_string());
} }
} else { } else {
cmd.arg("-c:v").arg("copy"); cmd.arg("-c:v").arg("copy");
} }
cmd.arg("-c:a").arg(match self.format { cmd.arg("-c:a").arg(match self.format {
FfmpegOutputFormat::Av1Flac | FfmpegOutputFormat::AvcFlac => "flac", FfmpegOutputFormat::Av1Flac => "flac",
FfmpegOutputFormat::Av1Opus => "libopus", FfmpegOutputFormat::Av1Opus => "libopus",
FfmpegOutputFormat::AvcAac => "aac" FfmpegOutputFormat::AvcAac => "aac"
}); });

View file

@ -15,7 +15,6 @@ use crate::{
use anyhow::{bail, Context}; use anyhow::{bail, Context};
use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf}; use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf};
use console::style; use console::style;
use ffmpeg::FfmpegOutputQuality;
use std::{ use std::{
borrow::Cow, borrow::Cow,
collections::VecDeque, collections::VecDeque,
@ -42,8 +41,8 @@ const QUESTION_FADE_LEN: Time = Time {
}; };
const FF_MULTIPLIER: usize = 8; const FF_MULTIPLIER: usize = 8;
// logo sizes at full hd, will be scaled to source resolution // logo sizes at full hd, will be scaled to source resolution
const FF_LOGO_SIZE: u32 = 128; const FF_LOGO_SIZE: usize = 128;
const LOGO_SIZE: u32 = 96; const LOGO_SIZE: usize = 96;
fn cmd() -> Command { fn cmd() -> Command {
#[cfg(feature = "mem_limit")] #[cfg(feature = "mem_limit")]
@ -120,7 +119,7 @@ fn ffprobe_audio(query: &str, concat_input: &Path) -> anyhow::Result<String> {
) )
} }
pub struct Renderer<'a> { pub(crate) struct Renderer<'a> {
/// The directory with all the sources. /// The directory with all the sources.
directory: &'a Path, directory: &'a Path,
@ -140,7 +139,6 @@ fn svg2mkv(
duration: Time duration: Time
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let mut ffmpeg = Ffmpeg::new(FfmpegOutput { let mut ffmpeg = Ffmpeg::new(FfmpegOutput {
quality: FfmpegOutputQuality::VisuallyLossless,
duration: Some(duration), duration: Some(duration),
time_base: Some(meta.source_tbn), time_base: Some(meta.source_tbn),
fps_mode_vfr: true, fps_mode_vfr: true,
@ -159,7 +157,7 @@ fn svg2mkv(
ffmpeg.run() ffmpeg.run()
} }
fn svg2png(svg: &Path, png: &Path, width: u32, height: u32) -> anyhow::Result<()> { fn svg2png(svg: &Path, png: &Path, width: usize, height: usize) -> anyhow::Result<()> {
let mut cmd = cmd(); let mut cmd = cmd();
cmd.arg("inkscape") cmd.arg("inkscape")
.arg("-w") .arg("-w")
@ -177,7 +175,7 @@ fn svg2png(svg: &Path, png: &Path, width: u32, height: u32) -> anyhow::Result<()
} }
impl<'a> Renderer<'a> { impl<'a> Renderer<'a> {
pub fn new(directory: &'a Path, project: &Project) -> anyhow::Result<Self> { pub(crate) fn new(directory: &'a Path, project: &Project) -> anyhow::Result<Self> {
let slug = format!( let slug = format!(
"{}-{}", "{}-{}",
project.lecture.course, project.lecture.course,
@ -186,21 +184,23 @@ impl<'a> Renderer<'a> {
let target = directory.join(&slug); let target = directory.join(&slug);
fs::create_dir_all(&target)?; fs::create_dir_all(&target)?;
// Ensure we have at least one input file. let first: PathBuf = directory.join(
project project
.source .source
.files .files
.first() .first()
.context("No source files present")?; .context("No source files present")?
);
// In case we don't have a resolution yet, we'll asign this after preprocessing. let height: u32 = ffprobe_video("stream=height", &first)?
let format = project .split('\n')
.source .next()
.metadata .unwrap()
.as_ref() .parse()?;
.map(|meta| meta.source_res.default_codec()) let format = if height <= 1080 {
.unwrap_or(FfmpegOutputFormat::Av1Flac) FfmpegOutputFormat::AvcAac
.with_flac_audio(); } else {
FfmpegOutputFormat::Av1Flac
};
Ok(Self { Ok(Self {
directory, directory,
@ -210,7 +210,7 @@ impl<'a> Renderer<'a> {
}) })
} }
pub fn recording_mkv(&self) -> PathBuf { pub(crate) fn recording_mkv(&self) -> PathBuf {
self.target.join("recording.mkv") self.target.join("recording.mkv")
} }
@ -230,7 +230,7 @@ impl<'a> Renderer<'a> {
self.target.join(format!("question{q_idx}.png")) self.target.join(format!("question{q_idx}.png"))
} }
pub fn preprocess(&mut self, project: &mut Project) -> anyhow::Result<()> { pub(crate) fn preprocess(&self, project: &mut Project) -> anyhow::Result<()> {
assert!(!project.progress.preprocessed); assert!(!project.progress.preprocessed);
let recording_txt = self.target.join("recording.txt"); let recording_txt = self.target.join("recording.txt");
@ -263,7 +263,14 @@ impl<'a> Renderer<'a> {
let width = ffprobe_video("stream=width", &recording_mkv)?.parse()?; let width = ffprobe_video("stream=width", &recording_mkv)?.parse()?;
let height = ffprobe_video("stream=height", &recording_mkv)?.parse()?; let height = ffprobe_video("stream=height", &recording_mkv)?.parse()?;
let source_res = Resolution::new(width, height); let source_res = match (width, height) {
(3840, 2160) => Resolution::UHD,
(2560, 1440) => Resolution::WQHD,
(1920, 1080) => Resolution::FullHD,
(1280, 720) => Resolution::HD,
(640, 360) => Resolution::nHD,
(width, height) => bail!("Unknown resolution: {width}x{height}")
};
project.source.metadata = Some(ProjectSourceMetadata { project.source.metadata = Some(ProjectSourceMetadata {
source_duration: ffprobe_video("format=duration", &recording_mkv)?.parse()?, source_duration: ffprobe_video("format=duration", &recording_mkv)?.parse()?,
source_fps: ffprobe_video("stream=r_frame_rate", &recording_mkv)?.parse()?, source_fps: ffprobe_video("stream=r_frame_rate", &recording_mkv)?.parse()?,
@ -271,13 +278,12 @@ impl<'a> Renderer<'a> {
source_res, source_res,
source_sample_rate source_sample_rate
}); });
self.format = source_res.default_codec().with_flac_audio();
Ok(()) Ok(())
} }
/// Prepare assets like intro, outro and questions. /// Prepare assets like intro, outro and questions.
pub fn render_assets(&self, project: &Project) -> anyhow::Result<()> { pub(crate) fn render_assets(&self, project: &Project) -> anyhow::Result<()> {
let metadata = project.source.metadata.as_ref().unwrap(); let metadata = project.source.metadata.as_ref().unwrap();
println!(); println!();
@ -359,8 +365,8 @@ impl<'a> Renderer<'a> {
/// Get the video file for a specific resolution, completely finished. /// Get the video file for a specific resolution, completely finished.
fn video_file_res(&self, res: Resolution) -> PathBuf { fn video_file_res(&self, res: Resolution) -> PathBuf {
let extension = match res.default_codec() { let extension = match res.format() {
FfmpegOutputFormat::Av1Flac | FfmpegOutputFormat::AvcFlac => "mkv", FfmpegOutputFormat::Av1Flac => "mkv",
FfmpegOutputFormat::Av1Opus => "webm", FfmpegOutputFormat::Av1Opus => "webm",
FfmpegOutputFormat::AvcAac => "mp4" FfmpegOutputFormat::AvcAac => "mp4"
}; };
@ -369,16 +375,15 @@ impl<'a> Renderer<'a> {
} }
/// Get the video file directly outputed to further transcode. /// Get the video file directly outputed to further transcode.
pub fn video_file_output(&self) -> PathBuf { pub(crate) fn video_file_output(&self) -> PathBuf {
self.target.join(format!("{}.mkv", self.slug)) self.target.join(format!("{}.mkv", self.slug))
} }
pub fn render(&self, project: &mut Project) -> anyhow::Result<PathBuf> { pub(crate) fn render(&self, project: &mut Project) -> anyhow::Result<PathBuf> {
let source_res = project.source.metadata.as_ref().unwrap().source_res; let source_res = project.source.metadata.as_ref().unwrap().source_res;
let output = self.video_file_output(); let output = self.video_file_output();
let mut ffmpeg = Ffmpeg::new(FfmpegOutput { let mut ffmpeg = Ffmpeg::new(FfmpegOutput {
quality: FfmpegOutputQuality::VisuallyLossless,
video_bitrate: Some(source_res.bitrate() * 3), video_bitrate: Some(source_res.bitrate() * 3),
..FfmpegOutput::new(self.format, output.clone()) ..FfmpegOutput::new(self.format, output.clone())
}); });
@ -670,7 +675,7 @@ impl<'a> Renderer<'a> {
comment: Some(lecture.lang.video_created_by_us.into()), comment: Some(lecture.lang.video_created_by_us.into()),
language: Some(lecture.lang.lang.into()), language: Some(lecture.lang.lang.into()),
..FfmpegOutput::new(res.default_codec(), output.clone()).enable_faststart() ..FfmpegOutput::new(res.format(), output.clone()).enable_faststart()
}); });
ffmpeg.add_input(FfmpegInput::new(input)); ffmpeg.add_input(FfmpegInput::new(input));
ffmpeg.rescale_video(res); ffmpeg.rescale_video(res);