2023-10-28 23:38:17 +02:00
|
|
|
pub mod ffmpeg;
|
2023-10-30 16:05:21 +01:00
|
|
|
mod filter;
|
2023-10-28 23:38:17 +02:00
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
use self::{
|
|
|
|
ffmpeg::{FfmpegOutput, FfmpegOutputFormat},
|
|
|
|
filter::Filter
|
|
|
|
};
|
2023-10-28 23:38:17 +02:00
|
|
|
use crate::{
|
2023-10-30 17:32:21 +01:00
|
|
|
iotro::{intro, outro},
|
2024-05-26 15:20:40 +02:00
|
|
|
project::{Project, ProjectLecture, ProjectSourceMetadata, Resolution},
|
2024-05-25 16:20:43 +02:00
|
|
|
question::Question,
|
2023-10-30 17:32:21 +01:00
|
|
|
render::ffmpeg::{Ffmpeg, FfmpegInput},
|
2024-05-26 15:20:40 +02:00
|
|
|
time::{format_date, format_time, Time}
|
2023-10-28 23:38:17 +02:00
|
|
|
};
|
|
|
|
use anyhow::{bail, Context};
|
|
|
|
use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf};
|
2024-05-25 14:17:07 +02:00
|
|
|
use console::style;
|
2024-06-26 12:04:39 +02:00
|
|
|
use ffmpeg::FfmpegOutputQuality;
|
2023-10-28 23:38:17 +02:00
|
|
|
use std::{
|
2023-10-30 21:26:17 +01:00
|
|
|
borrow::Cow,
|
2023-10-30 22:48:53 +01:00
|
|
|
collections::VecDeque,
|
2023-10-28 23:38:17 +02:00
|
|
|
fs::{self, File},
|
|
|
|
io::Write as _,
|
|
|
|
process::{Command, Stdio}
|
|
|
|
};
|
|
|
|
|
|
|
|
const INTRO_LEN: Time = Time {
|
|
|
|
seconds: 3,
|
|
|
|
micros: 0
|
|
|
|
};
|
|
|
|
const OUTRO_LEN: Time = Time {
|
|
|
|
seconds: 5,
|
|
|
|
micros: 0
|
|
|
|
};
|
|
|
|
const TRANSITION_LEN: Time = Time {
|
|
|
|
seconds: 0,
|
|
|
|
micros: 200_000
|
|
|
|
};
|
2024-05-25 16:20:43 +02:00
|
|
|
const QUESTION_FADE_LEN: Time = Time {
|
|
|
|
seconds: 0,
|
|
|
|
micros: 400_000
|
|
|
|
};
|
2023-10-30 22:22:56 +01:00
|
|
|
const FF_MULTIPLIER: usize = 8;
|
2023-10-30 23:13:08 +01:00
|
|
|
// logo sizes at full hd, will be scaled to source resolution
|
2024-06-23 15:53:45 +00:00
|
|
|
const FF_LOGO_SIZE: u32 = 128;
|
|
|
|
const LOGO_SIZE: u32 = 96;
|
2023-10-28 23:38:17 +02:00
|
|
|
|
|
|
|
fn cmd() -> Command {
|
2024-05-18 12:52:08 +02:00
|
|
|
#[cfg(feature = "mem_limit")]
|
|
|
|
let mut cmd = {
|
|
|
|
// we use systemd-run to limit the process memory
|
|
|
|
// I tried others like ulimit, chpst or isolate, but none worked
|
|
|
|
let mut cmd = Command::new("systemd-run");
|
|
|
|
cmd.arg("--scope")
|
|
|
|
.arg("-q")
|
|
|
|
.arg("--expand-environment=no")
|
|
|
|
.arg("-p")
|
|
|
|
.arg(format!("MemoryMax={}", crate::MEM_LIMIT.read().unwrap()))
|
|
|
|
.arg("--user");
|
|
|
|
// we use busybox ash for having a shell that outputs commands with -x
|
|
|
|
cmd.arg("busybox");
|
|
|
|
cmd
|
|
|
|
};
|
|
|
|
#[cfg(not(feature = "mem_limit"))]
|
|
|
|
let mut cmd = Command::new("busybox");
|
|
|
|
cmd.arg("ash")
|
2023-10-28 23:38:17 +02:00
|
|
|
.arg("-exuo")
|
|
|
|
.arg("pipefail")
|
|
|
|
.arg("-c")
|
2023-10-30 20:28:17 +01:00
|
|
|
.arg("exec \"$0\" \"${@}\"");
|
2023-10-28 23:38:17 +02:00
|
|
|
cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ffprobe() -> Command {
|
|
|
|
let mut cmd = cmd();
|
|
|
|
cmd.arg("ffprobe")
|
|
|
|
.arg("-v")
|
|
|
|
.arg("error")
|
|
|
|
.arg("-of")
|
|
|
|
.arg("default=noprint_wrappers=1:nokey=1");
|
|
|
|
cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_output(cmd: &mut Command) -> anyhow::Result<String> {
|
|
|
|
let out = cmd.stderr(Stdio::inherit()).output()?;
|
|
|
|
if !out.status.success() {
|
|
|
|
bail!(
|
|
|
|
"Executed command failed with exit status {:?}",
|
|
|
|
out.status.code()
|
|
|
|
);
|
|
|
|
}
|
2023-10-30 20:28:17 +01:00
|
|
|
String::from_utf8(out.stdout)
|
|
|
|
.context("Command returned non-utf8 output")
|
|
|
|
.map(|str| str.trim().into())
|
2023-10-28 23:38:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ffprobe_video(query: &str, input: &Path) -> anyhow::Result<String> {
|
|
|
|
read_output(
|
|
|
|
ffprobe()
|
|
|
|
.arg("-select_streams")
|
|
|
|
.arg("v:0")
|
|
|
|
.arg("-show_entries")
|
|
|
|
.arg(query)
|
|
|
|
.arg(input)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ffprobe_audio(query: &str, concat_input: &Path) -> anyhow::Result<String> {
|
|
|
|
read_output(
|
|
|
|
ffprobe()
|
|
|
|
.arg("-select_streams")
|
|
|
|
.arg("a:0")
|
|
|
|
.arg("-show_entries")
|
|
|
|
.arg(query)
|
|
|
|
.arg("-safe")
|
|
|
|
.arg("0")
|
|
|
|
.arg("-f")
|
|
|
|
.arg("concat")
|
|
|
|
.arg(concat_input)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-23 15:53:45 +00:00
|
|
|
pub struct Renderer<'a> {
|
2023-10-28 23:38:17 +02:00
|
|
|
/// The directory with all the sources.
|
|
|
|
directory: &'a Path,
|
|
|
|
|
|
|
|
/// The slug (i.e. 23ws-malo2-231016).
|
|
|
|
slug: String,
|
|
|
|
/// The target directory.
|
2024-01-15 18:50:15 +01:00
|
|
|
target: PathBuf,
|
|
|
|
/// The format to use for intermediate products
|
|
|
|
format: FfmpegOutputFormat
|
2023-10-28 23:38:17 +02:00
|
|
|
}
|
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
fn svg2mkv(
|
2023-11-15 15:44:58 +01:00
|
|
|
meta: &ProjectSourceMetadata,
|
|
|
|
svg: PathBuf,
|
2023-12-19 23:56:04 +01:00
|
|
|
mkv: PathBuf,
|
2024-01-15 18:50:15 +01:00
|
|
|
format: FfmpegOutputFormat,
|
2023-11-15 15:44:58 +01:00
|
|
|
duration: Time
|
|
|
|
) -> anyhow::Result<()> {
|
|
|
|
let mut ffmpeg = Ffmpeg::new(FfmpegOutput {
|
|
|
|
duration: Some(duration),
|
|
|
|
time_base: Some(meta.source_tbn),
|
|
|
|
fps_mode_vfr: true,
|
2024-01-15 18:50:15 +01:00
|
|
|
..FfmpegOutput::new(format, mkv)
|
2023-11-15 15:44:58 +01:00
|
|
|
});
|
2023-10-30 17:32:21 +01:00
|
|
|
ffmpeg.add_input(FfmpegInput {
|
|
|
|
loop_input: true,
|
2023-11-15 15:44:58 +01:00
|
|
|
fps: Some(meta.source_fps),
|
2023-10-30 17:32:21 +01:00
|
|
|
..FfmpegInput::new(svg)
|
|
|
|
});
|
|
|
|
ffmpeg.add_filter(Filter::GenerateSilence {
|
|
|
|
video: "0".into(),
|
|
|
|
output: "out".into()
|
|
|
|
});
|
|
|
|
ffmpeg.set_filter_output("out");
|
|
|
|
ffmpeg.run()
|
|
|
|
}
|
|
|
|
|
2024-06-23 15:53:45 +00:00
|
|
|
fn svg2png(svg: &Path, png: &Path, width: u32, height: u32) -> anyhow::Result<()> {
|
2023-10-30 17:55:53 +01:00
|
|
|
let mut cmd = cmd();
|
2023-10-30 20:28:17 +01:00
|
|
|
cmd.arg("inkscape")
|
|
|
|
.arg("-w")
|
2024-05-25 16:20:43 +02:00
|
|
|
.arg(width.to_string())
|
2023-10-30 20:28:17 +01:00
|
|
|
.arg("-h")
|
2024-05-25 16:20:43 +02:00
|
|
|
.arg(height.to_string());
|
2023-10-30 17:55:53 +01:00
|
|
|
cmd.arg(svg).arg("-o").arg(png);
|
2023-10-30 20:28:17 +01:00
|
|
|
|
2023-10-30 17:55:53 +01:00
|
|
|
let status = cmd.status()?;
|
2023-10-30 20:28:17 +01:00
|
|
|
if status.success() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
bail!("inkscape failed with exit code {:?}", status.code())
|
|
|
|
}
|
2023-10-30 17:55:53 +01:00
|
|
|
}
|
|
|
|
|
2023-10-28 23:38:17 +02:00
|
|
|
impl<'a> Renderer<'a> {
|
2024-06-23 15:53:45 +00:00
|
|
|
pub fn new(directory: &'a Path, project: &Project) -> anyhow::Result<Self> {
|
2023-10-28 23:38:17 +02:00
|
|
|
let slug = format!(
|
|
|
|
"{}-{}",
|
|
|
|
project.lecture.course,
|
|
|
|
format_date(project.lecture.date)
|
|
|
|
);
|
|
|
|
let target = directory.join(&slug);
|
2023-10-30 20:28:17 +01:00
|
|
|
fs::create_dir_all(&target)?;
|
2023-10-28 23:38:17 +02:00
|
|
|
|
2024-01-15 18:50:15 +01:00
|
|
|
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()?;
|
2024-06-04 09:51:45 +02:00
|
|
|
let format = if height < 1080 {
|
2024-01-15 18:50:15 +01:00
|
|
|
FfmpegOutputFormat::AvcAac
|
|
|
|
} else {
|
|
|
|
FfmpegOutputFormat::Av1Flac
|
|
|
|
};
|
|
|
|
|
2023-10-28 23:38:17 +02:00
|
|
|
Ok(Self {
|
|
|
|
directory,
|
|
|
|
slug,
|
2024-01-15 18:50:15 +01:00
|
|
|
target,
|
|
|
|
format
|
2023-10-28 23:38:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-06-23 15:53:45 +00:00
|
|
|
pub fn recording_mkv(&self) -> PathBuf {
|
2023-12-19 23:56:04 +01:00
|
|
|
self.target.join("recording.mkv")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn intro_mkv(&self) -> PathBuf {
|
|
|
|
self.target.join("intro.mkv")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn outro_mkv(&self) -> PathBuf {
|
|
|
|
self.target.join("outro.mkv")
|
2023-10-30 20:28:17 +01:00
|
|
|
}
|
|
|
|
|
2024-05-25 16:20:43 +02:00
|
|
|
fn question_svg(&self, q_idx: usize) -> PathBuf {
|
|
|
|
self.target.join(format!("question{q_idx}.svg"))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn question_png(&self, q_idx: usize) -> PathBuf {
|
|
|
|
self.target.join(format!("question{q_idx}.png"))
|
|
|
|
}
|
|
|
|
|
2024-06-23 15:53:45 +00:00
|
|
|
pub fn preprocess(&self, project: &mut Project) -> anyhow::Result<()> {
|
2023-10-28 23:38:17 +02:00
|
|
|
assert!(!project.progress.preprocessed);
|
|
|
|
|
|
|
|
let recording_txt = self.target.join("recording.txt");
|
2023-10-30 17:32:21 +01:00
|
|
|
let mut file = File::create(&recording_txt)?;
|
2023-10-28 23:38:17 +02:00
|
|
|
for filename in &project.source.files {
|
2023-11-16 12:12:17 +01:00
|
|
|
writeln!(file, "file '{}'", self.directory.join(filename))?;
|
2023-10-28 23:38:17 +02:00
|
|
|
}
|
|
|
|
drop(file);
|
|
|
|
|
2024-05-25 14:17:07 +02:00
|
|
|
println!();
|
|
|
|
println!(
|
|
|
|
" {} {}",
|
|
|
|
style("==>").bold().cyan(),
|
|
|
|
style("Concatenating Video and Normalising Audio ...").bold()
|
|
|
|
);
|
|
|
|
|
2023-10-30 17:32:21 +01:00
|
|
|
let source_sample_rate =
|
|
|
|
ffprobe_audio("stream=sample_rate", &recording_txt)?.parse()?;
|
2023-12-19 23:56:04 +01:00
|
|
|
let recording_mkv = self.recording_mkv();
|
|
|
|
let mut ffmpeg = Ffmpeg::new(FfmpegOutput::new(
|
|
|
|
FfmpegOutputFormat::Av1Flac,
|
|
|
|
recording_mkv.clone()
|
|
|
|
));
|
2023-10-30 17:32:21 +01:00
|
|
|
ffmpeg.add_input(FfmpegInput {
|
|
|
|
concat: true,
|
|
|
|
..FfmpegInput::new(recording_txt)
|
|
|
|
});
|
2024-01-06 18:55:27 +01:00
|
|
|
ffmpeg.enable_loudnorm(project.source.stereo);
|
2023-10-30 17:32:21 +01:00
|
|
|
ffmpeg.run()?;
|
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
let width = ffprobe_video("stream=width", &recording_mkv)?.parse()?;
|
|
|
|
let height = ffprobe_video("stream=height", &recording_mkv)?.parse()?;
|
2024-06-23 15:53:45 +00:00
|
|
|
let source_res = Resolution { width, height };
|
2023-10-30 17:32:21 +01:00
|
|
|
project.source.metadata = Some(ProjectSourceMetadata {
|
2023-12-19 23:56:04 +01:00
|
|
|
source_duration: ffprobe_video("format=duration", &recording_mkv)?.parse()?,
|
|
|
|
source_fps: ffprobe_video("stream=r_frame_rate", &recording_mkv)?.parse()?,
|
|
|
|
source_tbn: ffprobe_video("stream=time_base", &recording_mkv)?.parse()?,
|
2023-10-30 17:32:21 +01:00
|
|
|
source_res,
|
|
|
|
source_sample_rate
|
|
|
|
});
|
2024-05-25 16:20:43 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepare assets like intro, outro and questions.
|
2024-06-23 15:53:45 +00:00
|
|
|
pub fn render_assets(&self, project: &Project) -> anyhow::Result<()> {
|
2023-11-15 15:44:58 +01:00
|
|
|
let metadata = project.source.metadata.as_ref().unwrap();
|
2023-10-28 23:38:17 +02:00
|
|
|
|
2024-05-25 14:17:07 +02:00
|
|
|
println!();
|
|
|
|
println!(
|
|
|
|
" {} {}",
|
|
|
|
style("==>").bold().cyan(),
|
|
|
|
style("Preparing assets ...").bold()
|
|
|
|
);
|
2023-10-30 20:28:17 +01:00
|
|
|
|
2023-10-30 17:55:53 +01:00
|
|
|
// render intro to svg then mp4
|
2023-10-28 23:38:17 +02:00
|
|
|
let intro_svg = self.target.join("intro.svg");
|
2023-10-30 17:32:21 +01:00
|
|
|
fs::write(
|
|
|
|
&intro_svg,
|
2024-05-25 16:20:43 +02:00
|
|
|
intro(metadata.source_res, &project.lecture)
|
2023-10-30 17:32:21 +01:00
|
|
|
.to_string_pretty()
|
|
|
|
.into_bytes()
|
|
|
|
)?;
|
2023-12-19 23:56:04 +01:00
|
|
|
let intro_mkv = self.intro_mkv();
|
2024-01-15 18:50:15 +01:00
|
|
|
svg2mkv(metadata, intro_svg, intro_mkv, self.format, INTRO_LEN)?;
|
2023-10-30 17:32:21 +01:00
|
|
|
|
2023-10-30 17:55:53 +01:00
|
|
|
// render outro to svg then mp4
|
2023-10-30 17:32:21 +01:00
|
|
|
let outro_svg = self.target.join("outro.svg");
|
|
|
|
fs::write(
|
|
|
|
&outro_svg,
|
2024-05-25 16:20:43 +02:00
|
|
|
outro(&project.lecture.lang, metadata.source_res)
|
2024-05-17 21:29:22 +02:00
|
|
|
.to_string_pretty()
|
|
|
|
.into_bytes()
|
2023-10-30 17:32:21 +01:00
|
|
|
)?;
|
2023-12-19 23:56:04 +01:00
|
|
|
let outro_mkv = self.outro_mkv();
|
2024-01-15 18:50:15 +01:00
|
|
|
svg2mkv(metadata, outro_svg, outro_mkv, self.format, OUTRO_LEN)?;
|
2023-10-28 23:38:17 +02:00
|
|
|
|
2023-10-30 17:55:53 +01:00
|
|
|
// copy logo then render to png
|
|
|
|
let logo_svg = self.target.join("logo.svg");
|
|
|
|
fs::write(
|
|
|
|
&logo_svg,
|
|
|
|
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/assets/logo.svg"))
|
|
|
|
)?;
|
|
|
|
let logo_png = self.target.join("logo.png");
|
2024-06-23 15:53:45 +00:00
|
|
|
let logo_size = LOGO_SIZE * metadata.source_res.width / 1920;
|
2024-05-25 16:20:43 +02:00
|
|
|
svg2png(&logo_svg, &logo_png, logo_size, logo_size)?;
|
2023-10-30 17:55:53 +01:00
|
|
|
|
|
|
|
// copy fastforward then render to png
|
|
|
|
let fastforward_svg = self.target.join("fastforward.svg");
|
|
|
|
fs::write(
|
|
|
|
&fastforward_svg,
|
|
|
|
include_bytes!(concat!(
|
|
|
|
env!("CARGO_MANIFEST_DIR"),
|
|
|
|
"/assets/fastforward.svg"
|
|
|
|
))
|
|
|
|
)?;
|
|
|
|
let fastforward_png = self.target.join("fastforward.png");
|
2024-06-23 15:53:45 +00:00
|
|
|
let ff_logo_size = FF_LOGO_SIZE * metadata.source_res.width / 1920;
|
2023-10-30 20:28:17 +01:00
|
|
|
svg2png(
|
|
|
|
&fastforward_svg,
|
|
|
|
&fastforward_png,
|
2024-05-25 16:20:43 +02:00
|
|
|
ff_logo_size,
|
|
|
|
ff_logo_size
|
2023-10-30 20:28:17 +01:00
|
|
|
)?;
|
2023-10-30 17:55:53 +01:00
|
|
|
|
2024-05-25 16:20:43 +02:00
|
|
|
// write questions then render to png
|
|
|
|
for (q_idx, (_, _, q_text)) in project.source.questions.iter().enumerate() {
|
|
|
|
let q = Question::new(metadata.source_res, &project.lecture.lang, q_text)
|
|
|
|
.finish()
|
|
|
|
.to_string_pretty()
|
|
|
|
.into_bytes();
|
|
|
|
let q_svg = self.question_svg(q_idx);
|
|
|
|
let q_png = self.question_png(q_idx);
|
|
|
|
fs::write(&q_svg, q)?;
|
|
|
|
svg2png(
|
|
|
|
&q_svg,
|
|
|
|
&q_png,
|
2024-06-23 15:53:45 +00:00
|
|
|
metadata.source_res.width,
|
|
|
|
metadata.source_res.height
|
2024-05-25 16:20:43 +02:00
|
|
|
)?;
|
|
|
|
}
|
|
|
|
|
2023-10-28 23:38:17 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-10-30 21:26:17 +01:00
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
/// Get the video file for a specific resolution, completely finished.
|
|
|
|
fn video_file_res(&self, res: Resolution) -> PathBuf {
|
2024-06-23 15:53:45 +00:00
|
|
|
let extension = match res.default_codec() {
|
2023-12-19 23:56:04 +01:00
|
|
|
FfmpegOutputFormat::Av1Flac => "mkv",
|
|
|
|
FfmpegOutputFormat::Av1Opus => "webm",
|
|
|
|
FfmpegOutputFormat::AvcAac => "mp4"
|
|
|
|
};
|
2023-11-03 10:02:30 +01:00
|
|
|
self.target
|
2024-06-23 15:53:45 +00:00
|
|
|
.join(format!("{}-{}p.{extension}", self.slug, res.height))
|
2023-11-03 10:02:30 +01:00
|
|
|
}
|
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
/// Get the video file directly outputed to further transcode.
|
2024-06-23 15:53:45 +00:00
|
|
|
pub fn video_file_output(&self) -> PathBuf {
|
2023-12-19 23:56:04 +01:00
|
|
|
self.target.join(format!("{}.mkv", self.slug))
|
2023-11-03 10:02:30 +01:00
|
|
|
}
|
|
|
|
|
2024-06-23 15:53:45 +00:00
|
|
|
pub fn render(&self, project: &mut Project) -> anyhow::Result<PathBuf> {
|
2023-11-16 12:23:10 +01:00
|
|
|
let source_res = project.source.metadata.as_ref().unwrap().source_res;
|
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
let output = self.video_file_output();
|
|
|
|
let mut ffmpeg = Ffmpeg::new(FfmpegOutput {
|
2024-06-26 12:04:39 +02:00
|
|
|
quality: FfmpegOutputQuality::VisuallyLossless,
|
2023-12-19 23:56:04 +01:00
|
|
|
video_bitrate: Some(source_res.bitrate() * 3),
|
2024-01-15 18:50:15 +01:00
|
|
|
..FfmpegOutput::new(self.format, output.clone())
|
2023-12-19 23:56:04 +01:00
|
|
|
});
|
2023-10-30 21:26:17 +01:00
|
|
|
|
|
|
|
// add all of our inputs
|
2023-12-19 23:56:04 +01:00
|
|
|
let intro = ffmpeg.add_input(FfmpegInput::new(self.intro_mkv()));
|
|
|
|
let rec_file = self.recording_mkv();
|
|
|
|
let outro = ffmpeg.add_input(FfmpegInput::new(self.outro_mkv()));
|
2023-10-30 21:26:17 +01:00
|
|
|
let logo = ffmpeg.add_input(FfmpegInput::new(self.target.join("logo.png")));
|
|
|
|
let ff = ffmpeg.add_input(FfmpegInput::new(self.target.join("fastforward.png")));
|
|
|
|
|
|
|
|
let mut part1: Cow<'static, str> = intro.into();
|
|
|
|
let mut part3: Cow<'static, str> = outro.into();
|
|
|
|
|
2023-10-30 23:13:08 +01:00
|
|
|
// the recording is fun because of all the fast forwarding
|
2024-05-25 16:20:43 +02:00
|
|
|
let mut part2 = VecDeque::<Cow<'static, str>>::new();
|
|
|
|
let mut part2_ts = VecDeque::new();
|
2023-10-30 23:13:08 +01:00
|
|
|
let mut part2_start_of_the_end = None;
|
|
|
|
let mut part2_end_of_the_start = None;
|
|
|
|
|
|
|
|
// ok so ff is fun. we will add the ff'ed section as well as the part between
|
2024-05-25 16:20:43 +02:00
|
|
|
// the previous ff'ed section and our new section, unless we are the first.
|
2023-10-30 23:13:08 +01:00
|
|
|
project.source.fast.sort();
|
|
|
|
for (i, (ff_st, ff_end)) in project.source.fast.iter().rev().enumerate() {
|
|
|
|
if let Some(prev_end) = part2_end_of_the_start {
|
2024-05-25 16:20:43 +02:00
|
|
|
let duration = prev_end - *ff_end;
|
2023-11-02 11:07:35 +01:00
|
|
|
let recffbetween = ffmpeg.add_input(FfmpegInput {
|
2023-10-30 23:13:08 +01:00
|
|
|
start: Some(*ff_end),
|
2024-05-25 16:20:43 +02:00
|
|
|
duration: Some(duration),
|
2023-11-02 11:07:35 +01:00
|
|
|
..FfmpegInput::new(rec_file.clone())
|
2023-10-30 23:13:08 +01:00
|
|
|
});
|
|
|
|
part2.push_front(recffbetween.into());
|
2024-05-25 16:20:43 +02:00
|
|
|
part2_ts.push_front(Some((*ff_end, duration)));
|
2023-10-30 23:13:08 +01:00
|
|
|
} else {
|
|
|
|
part2_start_of_the_end = Some(*ff_end);
|
|
|
|
}
|
|
|
|
part2_end_of_the_start = Some(*ff_st);
|
|
|
|
|
2023-11-02 11:07:35 +01:00
|
|
|
let recffpart = ffmpeg.add_input(FfmpegInput {
|
2023-10-30 23:13:08 +01:00
|
|
|
start: Some(*ff_st),
|
|
|
|
duration: Some(*ff_end - *ff_st),
|
2023-11-02 11:07:35 +01:00
|
|
|
..FfmpegInput::new(rec_file.clone())
|
2023-10-30 23:13:08 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
let recff = format!("recff{i}");
|
|
|
|
ffmpeg.add_filter(Filter::FastForward {
|
|
|
|
input: recffpart.into(),
|
|
|
|
ffinput: ff.clone().into(),
|
|
|
|
multiplier: FF_MULTIPLIER,
|
|
|
|
output: recff.clone().into()
|
|
|
|
});
|
|
|
|
part2.push_front(recff.into());
|
2024-05-25 16:20:43 +02:00
|
|
|
part2_ts.push_front(None);
|
2023-10-30 23:13:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// if the recording was not ff'ed, perform a normal trim
|
2023-10-30 21:26:17 +01:00
|
|
|
let start = project.source.start.unwrap();
|
2023-10-30 23:13:08 +01:00
|
|
|
let end = project.source.end.unwrap();
|
|
|
|
let part2_last_part_duration;
|
|
|
|
if part2.is_empty() {
|
|
|
|
part2_last_part_duration = end - start;
|
2023-11-02 11:07:35 +01:00
|
|
|
let rectrim = ffmpeg.add_input(FfmpegInput {
|
2023-10-30 23:13:08 +01:00
|
|
|
start: Some(start),
|
|
|
|
duration: Some(part2_last_part_duration),
|
2023-11-02 11:07:35 +01:00
|
|
|
..FfmpegInput::new(rec_file.clone())
|
2023-10-30 23:13:08 +01:00
|
|
|
});
|
|
|
|
part2.push_back(rectrim.into());
|
2024-05-25 16:20:43 +02:00
|
|
|
part2_ts.push_back(Some((start, part2_last_part_duration)));
|
2023-10-30 23:13:08 +01:00
|
|
|
}
|
|
|
|
// otherwise add the first and last parts separately
|
|
|
|
else {
|
2024-05-25 16:20:43 +02:00
|
|
|
let duration = part2_end_of_the_start.unwrap() - start;
|
2023-11-02 11:07:35 +01:00
|
|
|
let rectrimst = ffmpeg.add_input(FfmpegInput {
|
2023-10-30 23:13:08 +01:00
|
|
|
start: Some(start),
|
2024-05-25 16:20:43 +02:00
|
|
|
duration: Some(duration),
|
2023-11-02 11:07:35 +01:00
|
|
|
..FfmpegInput::new(rec_file.clone())
|
2023-10-30 23:13:08 +01:00
|
|
|
});
|
|
|
|
part2.push_front(rectrimst.into());
|
2024-05-25 16:20:43 +02:00
|
|
|
part2_ts.push_front(Some((start, duration)));
|
2023-10-30 23:13:08 +01:00
|
|
|
|
2024-05-25 16:20:43 +02:00
|
|
|
let part2_start_of_the_end = part2_start_of_the_end.unwrap();
|
|
|
|
part2_last_part_duration = end - part2_start_of_the_end;
|
2023-11-02 11:07:35 +01:00
|
|
|
let rectrimend = ffmpeg.add_input(FfmpegInput {
|
2024-05-25 16:20:43 +02:00
|
|
|
start: Some(part2_start_of_the_end),
|
2023-10-30 23:13:08 +01:00
|
|
|
duration: Some(part2_last_part_duration),
|
2023-11-02 11:07:35 +01:00
|
|
|
..FfmpegInput::new(rec_file.clone())
|
2023-10-30 23:13:08 +01:00
|
|
|
});
|
|
|
|
part2.push_back(rectrimend.into());
|
2024-05-25 16:20:43 +02:00
|
|
|
part2_ts.push_back(Some((part2_start_of_the_end, part2_last_part_duration)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok now we have a bunch of parts and a bunch of questions that want to get
|
|
|
|
// overlayed over those parts.
|
|
|
|
project.source.questions.sort();
|
|
|
|
let mut q_idx = 0;
|
|
|
|
for (i, ts) in part2_ts.iter().enumerate() {
|
|
|
|
let Some((start, duration)) = ts else {
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
loop {
|
|
|
|
if q_idx >= project.source.questions.len() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let (q_start, q_end, _) = &project.source.questions[q_idx];
|
|
|
|
if q_start < start {
|
|
|
|
bail!(
|
|
|
|
"Question starting at {} did not fit into the video",
|
|
|
|
format_time(*q_start)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if q_start >= start && *q_end <= *start + *duration {
|
|
|
|
// add the question as input to ffmpeg
|
|
|
|
let q_inp = ffmpeg.add_input(FfmpegInput {
|
|
|
|
loop_input: true,
|
|
|
|
fps: Some(project.source.metadata.as_ref().unwrap().source_fps),
|
|
|
|
duration: Some(*q_end - *q_start),
|
|
|
|
..FfmpegInput::new(self.question_png(q_idx))
|
|
|
|
});
|
|
|
|
|
|
|
|
// fade in the question
|
|
|
|
let q_fadein = format!("q{q_idx}fin");
|
|
|
|
ffmpeg.add_filter(Filter::FadeAlpha {
|
|
|
|
input: q_inp.into(),
|
|
|
|
direction: "in",
|
|
|
|
start: Time {
|
|
|
|
seconds: 0,
|
|
|
|
micros: 0
|
|
|
|
},
|
|
|
|
duration: QUESTION_FADE_LEN,
|
|
|
|
output: q_fadein.clone().into()
|
|
|
|
});
|
|
|
|
|
|
|
|
// fade out the question
|
|
|
|
let q_fadeout = format!("q{q_idx}fout");
|
|
|
|
ffmpeg.add_filter(Filter::FadeAlpha {
|
|
|
|
input: q_fadein.into(),
|
|
|
|
direction: "out",
|
|
|
|
start: *q_end - *q_start - QUESTION_FADE_LEN,
|
|
|
|
duration: QUESTION_FADE_LEN,
|
|
|
|
output: q_fadeout.clone().into()
|
|
|
|
});
|
|
|
|
|
|
|
|
// move the question to the correct timestamp
|
|
|
|
let q_pts = format!("q{q_idx}pts");
|
|
|
|
ffmpeg.add_filter(Filter::VideoOffset {
|
|
|
|
input: q_fadeout.into(),
|
|
|
|
seconds: *q_start - *start,
|
|
|
|
output: q_pts.clone().into()
|
|
|
|
});
|
|
|
|
|
|
|
|
// overlay the part in question
|
|
|
|
let q_overlay = format!("q{q_idx}o");
|
|
|
|
ffmpeg.add_filter(Filter::Overlay {
|
|
|
|
video_input: part2[i].clone(),
|
|
|
|
overlay_input: q_pts.into(),
|
|
|
|
x: "0".into(),
|
|
|
|
y: "0".into(),
|
|
|
|
repeatlast: false,
|
|
|
|
output: q_overlay.clone().into()
|
|
|
|
});
|
|
|
|
part2[i] = q_overlay.into();
|
|
|
|
|
|
|
|
q_idx += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if q_idx < project.source.questions.len() {
|
|
|
|
bail!(
|
|
|
|
"Question starting at {} did not fit into the video before it was over",
|
|
|
|
format_time(project.source.questions[q_idx].0)
|
|
|
|
);
|
2023-10-30 23:13:08 +01:00
|
|
|
}
|
2023-10-30 21:26:17 +01:00
|
|
|
|
2023-10-30 21:46:17 +01:00
|
|
|
// fade out the intro
|
|
|
|
let introfade = "introfade";
|
|
|
|
ffmpeg.add_filter(Filter::Fade {
|
|
|
|
input: part1,
|
|
|
|
direction: "out",
|
|
|
|
start: INTRO_LEN - TRANSITION_LEN,
|
|
|
|
duration: TRANSITION_LEN,
|
|
|
|
output: introfade.into()
|
|
|
|
});
|
|
|
|
part1 = introfade.into();
|
|
|
|
|
|
|
|
// fade in the recording
|
|
|
|
let recfadein = "recfadein";
|
|
|
|
ffmpeg.add_filter(Filter::Fade {
|
2023-10-30 23:13:08 +01:00
|
|
|
input: part2.pop_front().unwrap(),
|
2023-10-30 21:46:17 +01:00
|
|
|
direction: "in",
|
|
|
|
start: Time {
|
|
|
|
seconds: 0,
|
|
|
|
micros: 0
|
|
|
|
},
|
|
|
|
duration: TRANSITION_LEN,
|
|
|
|
output: recfadein.into()
|
|
|
|
});
|
2023-10-30 23:13:08 +01:00
|
|
|
part2.push_front(recfadein.into());
|
2023-10-30 21:46:17 +01:00
|
|
|
|
|
|
|
// fade out the recording
|
|
|
|
let recfadeout = "recfadeout";
|
|
|
|
ffmpeg.add_filter(Filter::Fade {
|
2023-10-30 23:13:08 +01:00
|
|
|
input: part2.pop_back().unwrap(),
|
2023-10-30 21:46:17 +01:00
|
|
|
direction: "out",
|
2023-10-30 23:13:08 +01:00
|
|
|
start: part2_last_part_duration - TRANSITION_LEN,
|
2023-10-30 21:46:17 +01:00
|
|
|
duration: TRANSITION_LEN,
|
|
|
|
output: recfadeout.into()
|
|
|
|
});
|
2023-10-30 23:13:08 +01:00
|
|
|
part2.push_back(recfadeout.into());
|
2023-10-30 21:46:17 +01:00
|
|
|
|
|
|
|
// fade in the outro
|
|
|
|
let outrofade = "outrofade";
|
|
|
|
ffmpeg.add_filter(Filter::Fade {
|
|
|
|
input: part3,
|
|
|
|
direction: "in",
|
|
|
|
start: Time {
|
|
|
|
seconds: 0,
|
|
|
|
micros: 0
|
|
|
|
},
|
|
|
|
duration: TRANSITION_LEN,
|
|
|
|
output: outrofade.into()
|
|
|
|
});
|
|
|
|
part3 = outrofade.into();
|
|
|
|
|
2023-10-30 21:26:17 +01:00
|
|
|
// concatenate everything
|
2023-10-30 23:13:08 +01:00
|
|
|
let mut parts = part2;
|
2023-10-30 22:48:53 +01:00
|
|
|
parts.push_front(part1);
|
|
|
|
parts.push_back(part3);
|
2023-10-30 21:26:17 +01:00
|
|
|
let concat = "concat";
|
|
|
|
ffmpeg.add_filter(Filter::Concat {
|
2023-10-30 22:48:53 +01:00
|
|
|
inputs: parts,
|
2023-10-30 21:26:17 +01:00
|
|
|
output: concat.into()
|
|
|
|
});
|
|
|
|
|
|
|
|
// overlay the logo
|
2023-11-02 23:48:56 +01:00
|
|
|
let logoalpha = "logoalpha";
|
|
|
|
ffmpeg.add_filter(Filter::Alpha {
|
|
|
|
input: logo.into(),
|
|
|
|
alpha: 0.5,
|
|
|
|
output: logoalpha.into()
|
|
|
|
});
|
2023-10-30 21:26:17 +01:00
|
|
|
let overlay = "overlay";
|
2024-06-23 15:53:45 +00:00
|
|
|
let overlay_off_x = 130 * source_res.width / 3840;
|
|
|
|
let overlay_off_y = 65 * source_res.height / 2160;
|
2023-10-30 21:26:17 +01:00
|
|
|
ffmpeg.add_filter(Filter::Overlay {
|
|
|
|
video_input: concat.into(),
|
2023-11-02 23:48:56 +01:00
|
|
|
overlay_input: logoalpha.into(),
|
2023-11-16 12:23:10 +01:00
|
|
|
x: format!("main_w-overlay_w-{overlay_off_x}").into(),
|
|
|
|
y: format!("main_h-overlay_h-{overlay_off_y}").into(),
|
2024-05-25 16:20:43 +02:00
|
|
|
repeatlast: true,
|
2023-10-30 21:26:17 +01:00
|
|
|
output: overlay.into()
|
|
|
|
});
|
|
|
|
|
|
|
|
// we're done :)
|
|
|
|
ffmpeg.set_filter_output(overlay);
|
|
|
|
ffmpeg.run()?;
|
|
|
|
|
|
|
|
Ok(output)
|
|
|
|
}
|
2023-11-03 10:02:30 +01:00
|
|
|
|
2024-05-24 12:17:40 +02:00
|
|
|
pub fn rescale(
|
|
|
|
&self,
|
|
|
|
lecture: &ProjectLecture,
|
|
|
|
res: Resolution
|
|
|
|
) -> anyhow::Result<PathBuf> {
|
2023-12-19 23:56:04 +01:00
|
|
|
let input = self.video_file_output();
|
|
|
|
let output = self.video_file_res(res);
|
2024-05-25 14:17:07 +02:00
|
|
|
|
|
|
|
println!();
|
|
|
|
println!(
|
|
|
|
" {} {}",
|
|
|
|
style("==>").bold().cyan(),
|
2024-06-23 15:53:45 +00:00
|
|
|
style(format!("Rescaling to {}p", res.height)).bold()
|
2024-05-25 14:17:07 +02:00
|
|
|
);
|
2023-11-03 10:02:30 +01:00
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
let mut ffmpeg = Ffmpeg::new(FfmpegOutput {
|
|
|
|
video_bitrate: Some(res.bitrate()),
|
2024-05-24 12:17:40 +02:00
|
|
|
|
|
|
|
title: Some(format!(
|
|
|
|
"{} {} {}",
|
|
|
|
lecture.label,
|
|
|
|
lecture.lang.from,
|
|
|
|
(lecture.lang.format_date_long)(lecture.date)
|
|
|
|
)),
|
|
|
|
author: Some(lecture.docent.clone()),
|
|
|
|
album: Some(lecture.course.clone()),
|
|
|
|
year: Some(lecture.date.year.to_string()),
|
|
|
|
comment: Some(lecture.lang.video_created_by_us.into()),
|
|
|
|
language: Some(lecture.lang.lang.into()),
|
|
|
|
|
2024-06-23 15:53:45 +00:00
|
|
|
..FfmpegOutput::new(res.default_codec(), output.clone()).enable_faststart()
|
2023-12-19 23:56:04 +01:00
|
|
|
});
|
2023-11-16 09:33:58 +01:00
|
|
|
ffmpeg.add_input(FfmpegInput::new(input));
|
|
|
|
ffmpeg.rescale_video(res);
|
|
|
|
ffmpeg.run()?;
|
|
|
|
Ok(output)
|
2023-11-03 10:02:30 +01:00
|
|
|
}
|
2023-10-28 23:38:17 +02:00
|
|
|
}
|