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},
|
|
|
|
render::ffmpeg::{Ffmpeg, FfmpegInput},
|
2023-10-28 23:38:17 +02:00
|
|
|
time::{format_date, Time},
|
2024-05-24 12:17:40 +02:00
|
|
|
Project, ProjectLecture, ProjectSourceMetadata, Resolution
|
2023-10-28 23:38:17 +02:00
|
|
|
};
|
|
|
|
use anyhow::{bail, Context};
|
|
|
|
use camino::{Utf8Path as Path, Utf8PathBuf as PathBuf};
|
|
|
|
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
|
|
|
|
};
|
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
|
|
|
|
const FF_LOGO_SIZE: usize = 128;
|
|
|
|
const LOGO_SIZE: usize = 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)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct Renderer<'a> {
|
|
|
|
/// 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()
|
|
|
|
}
|
|
|
|
|
2023-10-30 17:55:53 +01:00
|
|
|
fn svg2png(svg: &Path, png: &Path, size: usize) -> anyhow::Result<()> {
|
|
|
|
let mut cmd = cmd();
|
|
|
|
let size = size.to_string();
|
2023-10-30 20:28:17 +01:00
|
|
|
cmd.arg("inkscape")
|
|
|
|
.arg("-w")
|
|
|
|
.arg(&size)
|
|
|
|
.arg("-h")
|
|
|
|
.arg(&size);
|
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> {
|
|
|
|
pub(crate) fn new(directory: &'a Path, project: &Project) -> anyhow::Result<Self> {
|
|
|
|
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()?;
|
|
|
|
let format = if height <= 1080 {
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
pub(crate) fn recording_mkv(&self) -> PathBuf {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-10-28 23:38:17 +02:00
|
|
|
pub(crate) fn preprocess(&self, project: &mut Project) -> anyhow::Result<()> {
|
|
|
|
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);
|
|
|
|
|
2023-10-30 20:28:17 +01:00
|
|
|
println!("\x1B[1m ==> Concatenating Video and Normalising Audio ...\x1B[0m");
|
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()?;
|
2023-10-30 17:32:21 +01:00
|
|
|
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 {
|
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
|
|
|
|
});
|
2023-11-15 15:44:58 +01:00
|
|
|
let metadata = project.source.metadata.as_ref().unwrap();
|
2023-10-28 23:38:17 +02:00
|
|
|
|
2023-10-30 20:28:17 +01:00
|
|
|
println!("\x1B[1m ==> Preparing assets ...\x1B[0m");
|
|
|
|
|
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-04-10 12:55:00 +02:00
|
|
|
intro(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-17 21:29:22 +02:00
|
|
|
outro(&project.lecture.lang, source_res)
|
|
|
|
.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");
|
2023-10-30 23:13:08 +01:00
|
|
|
svg2png(&logo_svg, &logo_png, LOGO_SIZE * source_res.width() / 1920)?;
|
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");
|
2023-10-30 20:28:17 +01:00
|
|
|
svg2png(
|
|
|
|
&fastforward_svg,
|
|
|
|
&fastforward_png,
|
2023-10-30 23:13:08 +01:00
|
|
|
FF_LOGO_SIZE * source_res.width() / 1920
|
2023-10-30 20:28:17 +01:00
|
|
|
)?;
|
2023-10-30 17:55:53 +01: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 {
|
|
|
|
let extension = match res.format() {
|
|
|
|
FfmpegOutputFormat::Av1Flac => "mkv",
|
|
|
|
FfmpegOutputFormat::Av1Opus => "webm",
|
|
|
|
FfmpegOutputFormat::AvcAac => "mp4"
|
|
|
|
};
|
2023-11-03 10:02:30 +01:00
|
|
|
self.target
|
2023-12-19 23:56:04 +01: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.
|
|
|
|
pub(crate) fn video_file_output(&self) -> PathBuf {
|
|
|
|
self.target.join(format!("{}.mkv", self.slug))
|
2023-11-03 10:02:30 +01:00
|
|
|
}
|
|
|
|
|
2023-10-30 21:26:17 +01:00
|
|
|
pub(crate) 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 {
|
|
|
|
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
|
|
|
|
let mut part2 = VecDeque::new();
|
|
|
|
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
|
|
|
|
// the previous ff'ed section and our new section, unless we are the first
|
|
|
|
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 {
|
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),
|
2023-11-02 11:07:35 +01:00
|
|
|
duration: Some(prev_end - *ff_end),
|
|
|
|
..FfmpegInput::new(rec_file.clone())
|
2023-10-30 23:13:08 +01:00
|
|
|
});
|
|
|
|
part2.push_front(recffbetween.into());
|
|
|
|
} 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
}
|
|
|
|
// otherwise add the first and last parts separately
|
|
|
|
else {
|
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),
|
|
|
|
duration: Some(part2_end_of_the_start.unwrap() - start),
|
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());
|
|
|
|
|
|
|
|
part2_last_part_duration = end - part2_start_of_the_end.unwrap();
|
2023-11-02 11:07:35 +01:00
|
|
|
let rectrimend = ffmpeg.add_input(FfmpegInput {
|
2023-10-30 23:13:08 +01:00
|
|
|
start: Some(part2_start_of_the_end.unwrap()),
|
|
|
|
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());
|
|
|
|
}
|
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";
|
2023-11-16 12:23:10 +01: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(),
|
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);
|
2023-11-03 10:02:30 +01:00
|
|
|
println!("\x1B[1m ==> Rescaling to {}p\x1B[0m", res.height());
|
|
|
|
|
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()),
|
|
|
|
|
2023-12-19 23:56:04 +01:00
|
|
|
..FfmpegOutput::new(res.format(), output.clone()).enable_faststart()
|
|
|
|
});
|
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
|
|
|
}
|