Compare commits

..

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

12 changed files with 250 additions and 348 deletions

View file

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

View file

@ -24,4 +24,3 @@ toml = { package = "basic-toml", version = "0.1.4" }
[features]
default = ["mem_limit"]
mem_limit = []
vaapi = []

View file

@ -1,17 +0,0 @@
**ACHTUNG!** This repository might be mirrored at different places, but the main repository is and remains at [msrd0.dev/msrd0/render_video](https://msrd0.dev/msrd0/render_video). Please redirect all issues and pull requests there.
# render_video
This "script" is an extremely fancy wrapper around ffmpeg to cut/render videos for the [VideoAG](https://video.fsmpi.rwth-aachen.de) of the [Fachschaft I/1 der RWTH Aachen University](https://fsmpi.rwth-aachen.de).
You can find a ready-to-use docker image at [`quay.io/msrd0/render_video`](https://quay.io/msrd0/render_video).
## Features
- **Extract a single audio channel from stereo recording.** We use that with one of our cameras that supports plugging a lavalier microphone (mono source) into one channel of the stereo recording, and using the camera microphone (mono source) for the other channel of the stereo recording.
- **Cut away before/after the lecture.** We don't hit the start record button the exact time that the lecture starts, and don't hit the stop button exactly when the lecture ends, so we need to cut away those unwanted bits.
- **Fast-forward through _Tafelwischpausen_.** Sometimes docents still use blackboards and need to wipe those, which can be fast-forwarded by this tool.
- **Overlay questions from the audience.** Sometimes people in the audience have questions, and those are usually poorly understood on the lavalier microphones. Therefore you can subtitle these using the other microphones in the room that don't make it into the final video and have those overlayed.
- **Add intro and outro.** We add intro and outro slides at the start/end at all lectures, which this tool can do for you.
- **Add our logo watermark.** We add a logo watermark in the bottom right corner of all videos, which this tool can do for you.
- **Rescale to lower resolutions.** We usually published videos at different resolutions, and this tool can rescale your video for all resolutions you want.

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
use crate::{
project::{ProjectLecture, Resolution},
time::Date
};
use crate::{time::Date, ProjectLecture, Resolution};
use anyhow::anyhow;
use std::{
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)]
#![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 clap::Parser;
use console::style;
use render_video::{
cli::{ask, ask_time},
preset::Preset,
project::{Project, ProjectLecture, ProjectSource, Resolution},
render::Renderer,
time::parse_date
#[cfg(feature = "mem_limit")]
use std::sync::RwLock;
use std::{
fmt::Display,
fs,
io::{self, BufRead as _, Write}
};
use std::fs;
#[cfg(feature = "mem_limit")]
static MEM_LIMIT: RwLock<String> = RwLock::new(String::new());
#[derive(Debug, Parser)]
struct Args {
@ -45,12 +60,58 @@ struct Args {
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() {
let args = Args::parse();
#[cfg(feature = "mem_limit")]
{
*(render_video::MEM_LIMIT.write().unwrap()) = args.mem_limit;
*(MEM_LIMIT.write().unwrap()) = args.mem_limit;
}
// process arguments
@ -74,8 +135,7 @@ fn main() {
let lower = name.to_ascii_lowercase();
if (lower.ends_with(".mp4")
|| lower.ends_with(".mts")
|| lower.ends_with(".mkv")
|| lower.ends_with(".mov"))
|| lower.ends_with(".mkv"))
&& !entry.file_type().unwrap().is_dir()
{
files.push(String::from(name));
@ -122,7 +182,7 @@ fn main() {
project
};
let mut renderer = Renderer::new(&directory, &project).unwrap();
let renderer = Renderer::new(&directory, &project).unwrap();
let recording = renderer.recording_mkv();
// preprocess the video
@ -215,7 +275,7 @@ fn main() {
// rescale the video
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
|| res > args.transcode_start.unwrap_or(preset.transcode_start)
|| res < lowest_res

View file

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

View file

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

View file

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

View file

@ -15,7 +15,6 @@ use crate::{
use anyhow::{bail, Context};
use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf};
use console::style;
use ffmpeg::FfmpegOutputQuality;
use std::{
borrow::Cow,
collections::VecDeque,
@ -42,8 +41,8 @@ const QUESTION_FADE_LEN: Time = Time {
};
const FF_MULTIPLIER: usize = 8;
// logo sizes at full hd, will be scaled to source resolution
const FF_LOGO_SIZE: u32 = 128;
const LOGO_SIZE: u32 = 96;
const FF_LOGO_SIZE: usize = 128;
const LOGO_SIZE: usize = 96;
fn cmd() -> Command {
#[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.
directory: &'a Path,
@ -140,7 +139,6 @@ fn svg2mkv(
duration: Time
) -> anyhow::Result<()> {
let mut ffmpeg = Ffmpeg::new(FfmpegOutput {
quality: FfmpegOutputQuality::VisuallyLossless,
duration: Some(duration),
time_base: Some(meta.source_tbn),
fps_mode_vfr: true,
@ -159,7 +157,7 @@ fn svg2mkv(
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();
cmd.arg("inkscape")
.arg("-w")
@ -177,7 +175,7 @@ fn svg2png(svg: &Path, png: &Path, width: u32, height: u32) -> anyhow::Result<()
}
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!(
"{}-{}",
project.lecture.course,
@ -186,21 +184,23 @@ impl<'a> Renderer<'a> {
let target = directory.join(&slug);
fs::create_dir_all(&target)?;
// Ensure we have at least one input file.
project
.source
.files
.first()
.context("No source files present")?;
// In case we don't have a resolution yet, we'll asign this after preprocessing.
let format = project
.source
.metadata
.as_ref()
.map(|meta| meta.source_res.default_codec())
.unwrap_or(FfmpegOutputFormat::Av1Flac)
.with_flac_audio();
let first: PathBuf = directory.join(
project
.source
.files
.first()
.context("No source files present")?
);
let height: u32 = ffprobe_video("stream=height", &first)?
.split('\n')
.next()
.unwrap()
.parse()?;
let format = if height <= 1080 {
FfmpegOutputFormat::AvcAac
} else {
FfmpegOutputFormat::Av1Flac
};
Ok(Self {
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")
}
@ -230,7 +230,7 @@ impl<'a> Renderer<'a> {
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);
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 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 {
source_duration: ffprobe_video("format=duration", &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_sample_rate
});
self.format = source_res.default_codec().with_flac_audio();
Ok(())
}
/// 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();
println!();
@ -359,8 +365,8 @@ impl<'a> Renderer<'a> {
/// Get the video file for a specific resolution, completely finished.
fn video_file_res(&self, res: Resolution) -> PathBuf {
let extension = match res.default_codec() {
FfmpegOutputFormat::Av1Flac | FfmpegOutputFormat::AvcFlac => "mkv",
let extension = match res.format() {
FfmpegOutputFormat::Av1Flac => "mkv",
FfmpegOutputFormat::Av1Opus => "webm",
FfmpegOutputFormat::AvcAac => "mp4"
};
@ -369,16 +375,15 @@ impl<'a> Renderer<'a> {
}
/// 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))
}
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 output = self.video_file_output();
let mut ffmpeg = Ffmpeg::new(FfmpegOutput {
quality: FfmpegOutputQuality::VisuallyLossless,
video_bitrate: Some(source_res.bitrate() * 3),
..FfmpegOutput::new(self.format, output.clone())
});
@ -670,7 +675,7 @@ impl<'a> Renderer<'a> {
comment: Some(lecture.lang.video_created_by_us.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.rescale_video(res);