435 lines
14 KiB
Rust
435 lines
14 KiB
Rust
use crate::{archive, model::*, playback, reports};
|
||
use anyhow::{Context, Result};
|
||
use std::{
|
||
collections::BTreeSet,
|
||
fs::{self, File},
|
||
path::{Path, PathBuf},
|
||
process::{Command, Stdio},
|
||
sync::atomic::{AtomicBool, Ordering},
|
||
time::{Duration, Instant},
|
||
};
|
||
|
||
#[derive(Clone, Debug)]
|
||
pub struct Event {
|
||
pub message: String,
|
||
pub done: usize,
|
||
pub total: usize,
|
||
pub warning: bool,
|
||
pub path: Option<PathBuf>,
|
||
}
|
||
impl Event {
|
||
pub fn step(message: impl Into<String>, done: usize, total: usize) -> Self {
|
||
Self {
|
||
message: message.into(),
|
||
done,
|
||
total,
|
||
warning: false,
|
||
path: None,
|
||
}
|
||
}
|
||
}
|
||
#[derive(Default, Debug)]
|
||
pub struct Outcome {
|
||
pub report: Option<Report>,
|
||
pub paths: Vec<PathBuf>,
|
||
pub output: PathBuf,
|
||
}
|
||
pub fn cancelled(cancel: &AtomicBool) -> Result<()> {
|
||
anyhow::ensure!(!cancel.load(Ordering::Relaxed), "Cancelled.");
|
||
Ok(())
|
||
}
|
||
pub fn find_ffprobe(override_path: Option<&Path>) -> Result<PathBuf> {
|
||
if let Some(path) = override_path {
|
||
anyhow::ensure!(path.is_file(), "FFprobe not found: {}", path.display());
|
||
return Ok(path.to_owned());
|
||
}
|
||
let name = if cfg!(windows) {
|
||
"ffprobe.exe"
|
||
} else {
|
||
"ffprobe"
|
||
};
|
||
let mut dirs = vec![];
|
||
if let Ok(exe) = std::env::current_exe()
|
||
&& let Some(dir) = exe.parent()
|
||
{
|
||
dirs.extend([dir.to_owned(), dir.join("bin")]);
|
||
}
|
||
if let Some(path) = std::env::var_os("PATH") {
|
||
dirs.extend(std::env::split_paths(&path));
|
||
}
|
||
dirs.into_iter().map(|d| d.join(name)).find(|p| p.is_file())
|
||
.context("FFprobe is required for timings. Put ffprobe.exe beside this app on Windows, install FFmpeg, or choose extraction only.")
|
||
}
|
||
pub fn probe_json(bytes: &[u8]) -> (Option<f64>, String, String) {
|
||
let unknown = (None, "Unknown".into(), "Unknown".into());
|
||
let Ok(v) = serde_json::from_slice::<serde_json::Value>(bytes) else {
|
||
return unknown;
|
||
};
|
||
let duration = v["format"]["duration"]
|
||
.as_str()
|
||
.and_then(|s| s.parse::<f64>().ok())
|
||
.or_else(|| v["format"]["duration"].as_f64())
|
||
.filter(|d| d.is_finite() && *d >= 0.0);
|
||
let stream = v["streams"]
|
||
.as_array()
|
||
.and_then(|a| a.iter().find(|s| s["codec_type"] == "video"));
|
||
let resolution = stream
|
||
.and_then(|s| Some((s["width"].as_u64()?, s["height"].as_u64()?)))
|
||
.filter(|(w, h)| *w > 0 && *h > 0)
|
||
.map(|(w, h)| format!("{w}×{h}"))
|
||
.unwrap_or("Unknown".into());
|
||
(
|
||
duration,
|
||
resolution,
|
||
stream
|
||
.and_then(|s| s["codec_name"].as_str())
|
||
.unwrap_or("Unknown")
|
||
.into(),
|
||
)
|
||
}
|
||
pub fn probe(
|
||
exe: &Path,
|
||
path: &Path,
|
||
cancel: &AtomicBool,
|
||
) -> Result<(Option<f64>, String, String)> {
|
||
let output = tempfile::NamedTempFile::new()?;
|
||
let mut command = Command::new(exe);
|
||
command
|
||
.args([
|
||
"-v",
|
||
"error",
|
||
"-show_entries",
|
||
"format=duration:stream=codec_type,width,height,codec_name",
|
||
"-of",
|
||
"json",
|
||
])
|
||
.arg(path)
|
||
.stdout(Stdio::from(output.reopen()?))
|
||
.stderr(Stdio::null());
|
||
#[cfg(windows)]
|
||
{
|
||
use std::os::windows::process::CommandExt;
|
||
command.creation_flags(0x08000000); // CREATE_NO_WINDOW
|
||
}
|
||
let Ok(mut child) = command.spawn() else {
|
||
return Ok(probe_json(b""));
|
||
};
|
||
let start = Instant::now();
|
||
loop {
|
||
if cancel.load(Ordering::Relaxed) || start.elapsed() > Duration::from_secs(120) {
|
||
let _ = child.kill();
|
||
let _ = child.wait();
|
||
cancelled(cancel)?;
|
||
return Ok(probe_json(b""));
|
||
}
|
||
if let Some(exit) = child.try_wait()? {
|
||
return Ok(if exit.success() {
|
||
probe_json(&fs::read(output.path())?)
|
||
} else {
|
||
probe_json(b"")
|
||
});
|
||
}
|
||
std::thread::sleep(Duration::from_millis(50));
|
||
}
|
||
}
|
||
pub fn assemble(source: &Path, mut videos: Vec<Video>, discovery: &archive::Discovery) -> Report {
|
||
videos.sort_by(|a, b| {
|
||
a.slides
|
||
.first()
|
||
.copied()
|
||
.unwrap_or(usize::MAX)
|
||
.cmp(&b.slides.first().copied().unwrap_or(usize::MAX))
|
||
.then_with(|| crate::ordering::natural(a.name.as_bytes(), b.name.as_bytes()))
|
||
});
|
||
let mut playbacks = vec![];
|
||
for video in &videos {
|
||
let mut settings = discovery
|
||
.settings
|
||
.get(&video.archive_path)
|
||
.cloned()
|
||
.unwrap_or_default();
|
||
let covered: BTreeSet<_> = settings.iter().map(|s| s.slide).collect();
|
||
let slides = if video.slides.is_empty() {
|
||
vec![None]
|
||
} else {
|
||
video.slides.iter().copied().map(Some).collect()
|
||
};
|
||
for slide in slides {
|
||
if !covered.contains(&slide) {
|
||
settings.push(Settings {
|
||
slide,
|
||
..Default::default()
|
||
});
|
||
}
|
||
}
|
||
playbacks.extend(settings.into_iter().map(|s| playback::effective(video, s)));
|
||
}
|
||
playbacks.sort_by(|a, b| {
|
||
a.settings
|
||
.slide
|
||
.unwrap_or(usize::MAX)
|
||
.cmp(&b.settings.slide.unwrap_or(usize::MAX))
|
||
.then_with(|| {
|
||
crate::ordering::natural(a.video.name.as_bytes(), b.video.name.as_bytes())
|
||
})
|
||
.then_with(|| a.settings.shape_id.cmp(&b.settings.shape_id))
|
||
});
|
||
Report {
|
||
groups: vec![],
|
||
presentation: source
|
||
.file_name()
|
||
.unwrap_or_default()
|
||
.to_string_lossy()
|
||
.into(),
|
||
embedded_videos: videos.len(),
|
||
total_size_bytes: videos.iter().map(|v| v.size_bytes).sum(),
|
||
total_duration_seconds: playbacks
|
||
.iter()
|
||
.filter_map(|p| p.video.duration_seconds)
|
||
.sum(),
|
||
total_media_duration_seconds: videos.iter().filter_map(|v| v.duration_seconds).sum(),
|
||
known_durations: videos
|
||
.iter()
|
||
.filter(|v| v.duration_seconds.is_some())
|
||
.count(),
|
||
known_playback_durations: playbacks
|
||
.iter()
|
||
.filter(|p| p.video.duration_seconds.is_some())
|
||
.count(),
|
||
playbacks,
|
||
videos,
|
||
external_media_slides: discovery.external.iter().copied().collect(),
|
||
notes: NOTES.iter().map(|s| (*s).into()).collect(),
|
||
}
|
||
}
|
||
pub fn run(options: &Options, cancel: &AtomicBool, emit: impl FnMut(Event)) -> Result<Outcome> {
|
||
options.validate()?;
|
||
if options.source.is_dir() || !options.additional_sources.is_empty() {
|
||
return crate::inputs::run(options, cancel, emit);
|
||
}
|
||
run_pptx(options, cancel, emit, true)
|
||
}
|
||
|
||
pub(crate) fn run_pptx(
|
||
options: &Options,
|
||
cancel: &AtomicBool,
|
||
mut emit: impl FnMut(Event),
|
||
publish: bool,
|
||
) -> Result<Outcome> {
|
||
cancelled(cancel)?;
|
||
emit(Event::step("Scanning presentation", 0, 0));
|
||
let mut zip =
|
||
zip::ZipArchive::new(File::open(&options.source)?).context("Cannot read PPTX archive")?;
|
||
let discovered = archive::discover(&mut zip);
|
||
let output = options.output_dir();
|
||
let mut outcome = Outcome {
|
||
output: output.clone(),
|
||
..Default::default()
|
||
};
|
||
if discovered.videos.is_empty() {
|
||
emit(Event {
|
||
warning: true,
|
||
..Event::step("No embedded videos; no output created.", 0, 0)
|
||
});
|
||
return Ok(outcome);
|
||
}
|
||
let ffprobe = if options.extract_only {
|
||
None
|
||
} else {
|
||
Some(find_ffprobe(options.ffprobe.as_deref())?)
|
||
};
|
||
#[cfg(not(feature = "pdf"))]
|
||
anyhow::ensure!(
|
||
!options
|
||
.formats
|
||
.iter()
|
||
.any(|f| f == "pdf" || f == "pdf-dense"),
|
||
"This build has no PDF support."
|
||
);
|
||
if !discovered.external.is_empty() {
|
||
emit(Event {
|
||
warning: true,
|
||
..Event::step(
|
||
format!(
|
||
"External media on slides {:?} will not be downloaded.",
|
||
discovered.external
|
||
),
|
||
0,
|
||
0,
|
||
)
|
||
});
|
||
}
|
||
for dir in [&output, &output.join("videos"), &output.join("reports")] {
|
||
anyhow::ensure!(
|
||
!dir.is_symlink(),
|
||
"Refusing output directory symlink: {}",
|
||
dir.display()
|
||
);
|
||
}
|
||
let temporary = tempfile::tempdir()?;
|
||
let mut videos = vec![];
|
||
let total = discovered.videos.len();
|
||
let mut names = BTreeSet::new();
|
||
for (i, part) in discovered.videos.iter().enumerate() {
|
||
cancelled(cancel)?;
|
||
let name = part.rsplit('/').next().unwrap_or("");
|
||
anyhow::ensure!(
|
||
!name.is_empty() && !name.contains(['\\', ':']) && name != "." && name != "..",
|
||
"Unsafe media filename"
|
||
);
|
||
anyhow::ensure!(
|
||
names.insert(name.to_lowercase()),
|
||
"Media filenames collide: {name}"
|
||
);
|
||
emit(Event::step(format!("Processing {name}"), i, total));
|
||
let dest = output.join("videos").join(name);
|
||
anyhow::ensure!(
|
||
!dest.is_symlink(),
|
||
"Refusing media symlink: {}",
|
||
dest.display()
|
||
);
|
||
if !options.timings_only && (!dest.exists() || options.overwrite) {
|
||
emit(Event::step(format!("Extracting {name}"), i, total));
|
||
archive::copy_media(&mut zip, part, &dest)?;
|
||
}
|
||
if options.extract_only {
|
||
emit(Event::step(format!("Extracted {name}"), i + 1, total));
|
||
continue;
|
||
}
|
||
let probe_path = if dest.is_file() {
|
||
dest.clone()
|
||
} else {
|
||
let temp = temporary.path().join(name);
|
||
emit(Event::step(
|
||
format!("Extracting temporary copy of {name}"),
|
||
i,
|
||
total,
|
||
));
|
||
archive::copy_media(&mut zip, part, &temp)?;
|
||
temp
|
||
};
|
||
emit(Event::step(format!("Probing {name}"), i, total));
|
||
let (duration_seconds, resolution, codec) =
|
||
probe(ffprobe.as_ref().unwrap(), &probe_path, cancel)?;
|
||
if duration_seconds.is_none() {
|
||
emit(Event {
|
||
warning: true,
|
||
..Event::step(format!("Duration unknown for {name}"), i, total)
|
||
});
|
||
}
|
||
videos.push(Video {
|
||
slides: discovered
|
||
.uses
|
||
.get(part)
|
||
.map(|s| s.iter().copied().collect())
|
||
.unwrap_or_default(),
|
||
duration_seconds,
|
||
resolution,
|
||
codec,
|
||
name: name.into(),
|
||
archive_path: part.clone(),
|
||
size_bytes: fs::metadata(&probe_path)?.len(),
|
||
extracted_file: dest.is_file().then(|| format!("../videos/{name}")),
|
||
});
|
||
emit(Event::step(format!("Processed {name}"), i + 1, total));
|
||
}
|
||
if options.extract_only {
|
||
outcome.paths.push(output.join("videos"));
|
||
return Ok(outcome);
|
||
}
|
||
let report = assemble(&options.source, videos, &discovered);
|
||
if !publish {
|
||
outcome.report = Some(report);
|
||
return Ok(outcome);
|
||
}
|
||
write_reports(options, cancel, &mut emit, report, outcome)
|
||
}
|
||
|
||
pub(crate) fn write_reports(
|
||
options: &Options,
|
||
cancel: &AtomicBool,
|
||
emit: &mut impl FnMut(Event),
|
||
report: Report,
|
||
mut outcome: Outcome,
|
||
) -> Result<Outcome> {
|
||
#[cfg(not(feature = "pdf"))]
|
||
anyhow::ensure!(
|
||
!options
|
||
.formats
|
||
.iter()
|
||
.any(|f| f == "pdf" || f == "pdf-dense"),
|
||
"This build has no PDF support."
|
||
);
|
||
let output = &outcome.output;
|
||
for dir in [output.clone(), output.join("reports")] {
|
||
anyhow::ensure!(
|
||
!dir.is_symlink(),
|
||
"Refusing output directory symlink: {}",
|
||
dir.display()
|
||
);
|
||
}
|
||
// Generate and compile everything before replacing any report.
|
||
let staging = tempfile::tempdir()?;
|
||
let mut requested = options.formats.clone();
|
||
if requested.iter().any(|f| f == "pdf") && !requested.iter().any(|f| f == "typst") {
|
||
requested.push("typst".into());
|
||
}
|
||
let mut files = BTreeSet::new();
|
||
for (i, format) in requested.iter().enumerate() {
|
||
cancelled(cancel)?;
|
||
emit(Event::step(
|
||
format!("Generating {format} report"),
|
||
i,
|
||
requested.len(),
|
||
));
|
||
let (name, bytes) = if format == "pdf" || format == "pdf-dense" {
|
||
let dense = format == "pdf-dense";
|
||
let stem = if dense { "timings-dense" } else { "timings" };
|
||
let text = reports::typst(&report, dense);
|
||
fs::write(staging.path().join(format!("{stem}.typ")), &text)?;
|
||
files.insert(format!("{stem}.typ"));
|
||
#[cfg(feature = "pdf")]
|
||
let bytes = crate::pdf::compile(&text)?;
|
||
#[cfg(not(feature = "pdf"))]
|
||
let bytes = Vec::<u8>::new();
|
||
(format!("{stem}.pdf"), bytes)
|
||
} else {
|
||
let ext = match format.as_str() {
|
||
"typst" => "typ",
|
||
"ontime" => "ontime.json",
|
||
s => s,
|
||
};
|
||
(
|
||
format!("timings.{ext}"),
|
||
reports::render(&report, format)?.into_bytes(),
|
||
)
|
||
};
|
||
fs::write(staging.path().join(&name), bytes)?;
|
||
files.insert(name);
|
||
}
|
||
cancelled(cancel)?;
|
||
let report_dir = output.join("reports");
|
||
fs::create_dir_all(&report_dir)?;
|
||
for name in &files {
|
||
let dest = report_dir.join(name);
|
||
anyhow::ensure!(
|
||
!dest.is_symlink() && (!dest.exists() || dest.is_file()),
|
||
"Refusing unsafe report destination: {}",
|
||
dest.display()
|
||
);
|
||
}
|
||
for name in files {
|
||
let dest = report_dir.join(&name);
|
||
let mut temp = tempfile::NamedTempFile::new_in(&report_dir)?;
|
||
std::io::copy(&mut File::open(staging.path().join(name))?, &mut temp)?;
|
||
temp.persist(&dest).map_err(|e| e.error)?;
|
||
outcome.paths.push(dest.clone());
|
||
emit(Event {
|
||
path: Some(dest.clone()),
|
||
..Event::step(format!("Report: {}", dest.display()), 0, 0)
|
||
});
|
||
}
|
||
outcome.report = Some(report);
|
||
Ok(outcome)
|
||
}
|