209 lines
7.8 KiB
Rust
209 lines
7.8 KiB
Rust
use crate::{
|
|
archive::{P, P14, R, Relationships},
|
|
model::{Playback, Settings, Video},
|
|
};
|
|
use roxmltree::{Document, Node};
|
|
|
|
pub fn time_offset(input: &str) -> Option<f64> {
|
|
let s = input.trim();
|
|
let end = s
|
|
.find(|c: char| !c.is_ascii_digit() && c != '.')
|
|
.unwrap_or(s.len());
|
|
let number = &s[..end];
|
|
if number.is_empty()
|
|
|| number.starts_with('.')
|
|
|| number.ends_with('.')
|
|
|| number.matches('.').count() > 1
|
|
{
|
|
return None;
|
|
}
|
|
let scale = match &s[end..] {
|
|
"" | "ms" => 0.001,
|
|
"s" => 1.0,
|
|
"min" => 60.0,
|
|
"h" => 3600.0,
|
|
"µs" | "μs" => 0.000001,
|
|
"ns" => 0.000000001,
|
|
_ => return None,
|
|
};
|
|
let value = number.parse::<f64>().ok()? * scale;
|
|
value.is_finite().then_some(value)
|
|
}
|
|
fn custom(s: &mut Settings, note: &str) {
|
|
if s.playback_status != "looping" {
|
|
s.playback_status = "custom-timing".into();
|
|
}
|
|
if !s.playback_notes.iter().any(|n| n == note) {
|
|
s.playback_notes.push(note.into());
|
|
}
|
|
}
|
|
fn direct<'a>(node: Node<'a, 'a>, name: &str) -> Option<Node<'a, 'a>> {
|
|
node.children().find(|n| n.has_tag_name((P, name)))
|
|
}
|
|
pub fn shape_settings(
|
|
doc: &Document<'_>,
|
|
rels: &Relationships,
|
|
videos: &[String],
|
|
slide: usize,
|
|
) -> Vec<(String, Settings)> {
|
|
let mut result = vec![];
|
|
for shape in doc.descendants().filter(|n| {
|
|
["pic", "sp", "graphicFrame"]
|
|
.iter()
|
|
.any(|s| n.has_tag_name((P, *s)))
|
|
}) {
|
|
let id = shape
|
|
.descendants()
|
|
.find(|n| n.has_tag_name((P, "cNvPr")))
|
|
.and_then(|n| n.attribute("id"))
|
|
.map(str::to_owned);
|
|
let mut parts = std::collections::BTreeMap::<String, Vec<Node<'_, '_>>>::new();
|
|
for media in shape
|
|
.descendants()
|
|
.filter(|n| matches!(n.tag_name().name(), "videoFile" | "media"))
|
|
{
|
|
for attr in ["embed", "link"] {
|
|
let Some(part) = media
|
|
.attribute((R, attr))
|
|
.and_then(|r| rels.get(r))
|
|
.and_then(|r| r.0.as_ref())
|
|
else {
|
|
continue;
|
|
};
|
|
if !videos.contains(part) {
|
|
continue;
|
|
}
|
|
let trims = parts.entry(part.clone()).or_default();
|
|
if media.has_tag_name((P14, "media")) {
|
|
trims.extend(media.children().filter(|n| n.has_tag_name((P14, "trim"))));
|
|
}
|
|
}
|
|
}
|
|
for (part, trims) in parts {
|
|
let mut s = Settings {
|
|
slide: Some(slide),
|
|
shape_id: id.clone(),
|
|
..Default::default()
|
|
};
|
|
let mut values = vec![];
|
|
let mut invalid = false;
|
|
for trim in trims {
|
|
match (
|
|
time_offset(trim.attribute("st").unwrap_or("0")),
|
|
time_offset(trim.attribute("end").unwrap_or("0")),
|
|
) {
|
|
(Some(a), Some(b)) => {
|
|
if !values.contains(&(a, b)) {
|
|
values.push((a, b));
|
|
}
|
|
}
|
|
_ => invalid = true,
|
|
}
|
|
}
|
|
if invalid || values.len() > 1 {
|
|
s.playback_status = "invalid-trim".into();
|
|
s.playback_notes
|
|
.push("Invalid or conflicting trim settings.".into());
|
|
} else if let Some((a, b)) = values.first() {
|
|
s.trim_start_seconds = *a;
|
|
s.trim_end_seconds = *b;
|
|
}
|
|
if let Some(id) = &id {
|
|
for media in doc
|
|
.descendants()
|
|
.filter(|n| n.has_tag_name((P, "cMediaNode")))
|
|
{
|
|
if direct(media, "tgtEl")
|
|
.and_then(|t| direct(t, "spTgt"))
|
|
.and_then(|n| n.attribute("spid"))
|
|
!= Some(id.as_str())
|
|
{
|
|
continue;
|
|
}
|
|
let timing = media
|
|
.children()
|
|
.filter(|n| n.has_tag_name((P, "cTn")))
|
|
.chain(media.ancestors().filter(|n| n.has_tag_name((P, "cTn"))));
|
|
for node in timing {
|
|
let repeat = node
|
|
.attribute("repeatCount")
|
|
.is_some_and(|r| !["1000", "1000.0", "100%"].contains(&r));
|
|
if repeat || node.attribute("repeatDur").is_some_and(|r| r != "0") {
|
|
s.playback_status = "looping".into();
|
|
let note = "Repeated playback; not exported to Ontime.".to_string();
|
|
if !s.playback_notes.contains(¬e) {
|
|
s.playback_notes.push(note);
|
|
}
|
|
}
|
|
if node
|
|
.attribute("spd")
|
|
.is_some_and(|r| !["100000", "100%"].contains(&r))
|
|
|| node
|
|
.attribute("autoRev")
|
|
.is_some_and(|r| ["1", "true"].contains(&r))
|
|
|| ["accel", "decel"].iter().any(|a| {
|
|
node.attribute(*a)
|
|
.is_some_and(|r| !["0", "0%"].contains(&r))
|
|
})
|
|
|| node.attribute("tmFilter").is_some()
|
|
|| direct(node, "endCondLst").is_some()
|
|
{
|
|
custom(
|
|
&mut s,
|
|
"Custom speed, reverse, or end conditions are not simulated.",
|
|
);
|
|
}
|
|
}
|
|
if media.attribute("numSld").is_some_and(|r| r != "1") {
|
|
custom(&mut s, "Playback across slides is not simulated.");
|
|
}
|
|
if media
|
|
.children()
|
|
.filter(|n| n.has_tag_name((P, "cTn")))
|
|
.any(|n| {
|
|
n.attribute("dur")
|
|
.is_some_and(|r| !["indefinite", "media"].contains(&r))
|
|
})
|
|
{
|
|
custom(&mut s, "Explicit animation duration is not simulated.");
|
|
}
|
|
}
|
|
}
|
|
result.push((part, s));
|
|
}
|
|
}
|
|
result
|
|
}
|
|
pub fn effective(video: &Video, mut settings: Settings) -> Playback {
|
|
let original = video.duration_seconds;
|
|
let mut video = video.clone();
|
|
video.slides = settings.slide.into_iter().collect();
|
|
video.duration_seconds = None;
|
|
if settings.playback_status == "normal" {
|
|
let trim = settings.trim_start_seconds + settings.trim_end_seconds;
|
|
if let Some(duration) = original {
|
|
if trim > 0.0 && trim >= duration {
|
|
settings.playback_status = "invalid-trim".into();
|
|
settings
|
|
.playback_notes
|
|
.push("Trim removes the whole media or exceeds its duration.".into());
|
|
} else {
|
|
video.duration_seconds = Some(((duration - trim) * 1e9).round() / 1e9);
|
|
if trim > 0.0 {
|
|
settings.playback_status = "trimmed".into();
|
|
}
|
|
}
|
|
} else {
|
|
settings.playback_status = "unknown".into();
|
|
}
|
|
}
|
|
let ontime_eligible =
|
|
["normal", "trimmed", "unknown"].contains(&settings.playback_status.as_str());
|
|
Playback {
|
|
video,
|
|
settings,
|
|
original_duration_seconds: original,
|
|
ontime_eligible,
|
|
}
|
|
}
|