1use std::collections::BTreeMap;
7
8use alpha2_model::MediaFingerprint;
9use alpha2_time::FrameRate;
10use serde::{Deserialize, Serialize};
11
12use crate::spec::{FileKind, MediaItem, Node, VideoSpec};
13
14#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
16#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
17#[serde(deny_unknown_fields)]
18pub struct Facts {
19 #[serde(default)]
21 pub media: BTreeMap<String, MediaFacts>,
22 #[serde(default)]
24 pub texts: BTreeMap<String, String>,
25}
26
27#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
29#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
30#[serde(deny_unknown_fields)]
31pub struct MediaFacts {
32 #[serde(default, skip_serializing_if = "Option::is_none")]
34 pub width: Option<u32>,
35 #[serde(default, skip_serializing_if = "Option::is_none")]
37 pub height: Option<u32>,
38 #[serde(default, skip_serializing_if = "Option::is_none")]
40 pub frame_rate: Option<FrameRate>,
41 #[serde(default, skip_serializing_if = "Option::is_none")]
43 pub duration_ticks: Option<i64>,
44 #[serde(default, skip_serializing_if = "Option::is_none")]
46 pub has_audio: Option<bool>,
47}
48
49impl MediaFacts {
50 #[must_use]
52 pub fn from_fingerprint(fingerprint: &MediaFingerprint) -> Self {
53 let streams = fingerprint.streams.as_ref();
54 let video = streams.and_then(|streams| streams.video.as_ref());
55 Self {
56 width: video.map(|video| video.width),
57 height: video.map(|video| video.height),
58 frame_rate: video.and_then(|video| video.frame_rate),
59 duration_ticks: fingerprint.duration_ticks,
60 has_audio: Some(streams.is_some_and(|streams| streams.audio.is_some())),
61 }
62 }
63}
64
65#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
67#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
68#[serde(deny_unknown_fields)]
69pub struct Requirements {
70 pub media: Vec<RequiredMedia>,
72 pub texts: Vec<String>,
74}
75
76#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
78#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
79#[serde(deny_unknown_fields)]
80pub struct RequiredMedia {
81 pub path: String,
83 pub kind: FileKind,
85}
86
87#[must_use]
91pub fn kind_of_path(path: &str) -> FileKind {
92 let name = path.rsplit(['/', '\\']).next().unwrap_or(path);
93 let extension = name
94 .rsplit_once('.')
95 .map(|(_, extension)| extension.to_ascii_lowercase())
96 .unwrap_or_default();
97 match extension.as_str() {
98 "png" | "jpg" | "jpeg" | "webp" | "bmp" | "tif" | "tiff" | "avif" => FileKind::Image,
99 "mp3" | "m4a" | "aac" | "wav" | "flac" | "ogg" | "oga" | "opus" | "aif" | "aiff"
100 | "caf" => FileKind::Audio,
101 _ => FileKind::Video,
102 }
103}
104
105#[must_use]
107pub fn requirements(spec: &VideoSpec) -> Requirements {
108 let mut found = Requirements::default();
109 let nodes = std::iter::once(&spec.body)
110 .chain(&spec.overlays)
111 .chain(&spec.audio);
112 for node in nodes {
113 walk(node, &mut found);
114 }
115 found
116}
117
118fn add_media(found: &mut Requirements, path: &str, kind: Option<FileKind>) {
121 let kind = kind.unwrap_or_else(|| kind_of_path(path));
122 match found.media.iter_mut().find(|media| media.path == path) {
123 Some(listed) => {
124 if listed.kind == FileKind::Audio && kind != FileKind::Audio {
125 listed.kind = kind;
126 }
127 }
128 None => found.media.push(RequiredMedia {
129 path: path.to_owned(),
130 kind,
131 }),
132 }
133}
134
135fn add_item(found: &mut Requirements, item: &MediaItem) {
136 match item {
137 MediaItem::Path(path) => add_media(found, path, None),
138 MediaItem::Node(node) => walk(node, found),
139 }
140}
141
142fn walk_effects(effects: &[crate::spec::EffectUse], found: &mut Requirements) {
143 for effect in effects {
144 let Some(spec) = alpha2_model::builtin_effect(&crate::lower::effect_id(&effect.effect))
145 else {
146 continue;
147 };
148 for input in spec
149 .inputs
150 .iter()
151 .filter_map(|name| effect.params.get(name))
152 {
153 if let Ok(node) = serde_json::from_value::<Node>(input.clone()) {
154 walk(&node, found);
155 }
156 }
157 }
158}
159
160fn walk(node: &Node, found: &mut Requirements) {
161 match node {
162 Node::Sequence(sequence) => sequence
163 .children
164 .iter()
165 .for_each(|child| walk(child, found)),
166 Node::Stack(stack) => stack.children.iter().for_each(|child| walk(child, found)),
167 Node::Matte(matte) => {
168 walk(&matte.content, found);
169 walk(&matte.by, found);
170 }
171 Node::Mask(mask) => walk(&mask.content, found),
172 Node::Box(node) => node
173 .children
174 .iter()
175 .for_each(|child| walk(child.node(), found)),
176 Node::Group(group) => {
177 group.children.iter().for_each(|child| walk(child, found));
178 walk_effects(&group.effects, found);
179 }
180 Node::Adjustment(adjustment) => walk_effects(&adjustment.effects, found),
181 Node::Grid(grid) => grid.children.iter().for_each(|child| walk(child, found)),
182 Node::Media(media) => add_media(found, &media.path, media.kind),
183 Node::Audio(audio) => add_media(found, &audio.path, Some(FileKind::Audio)),
184 Node::MusicBed(music) => add_media(found, &music.path, Some(FileKind::Audio)),
185 Node::Captions(captions) => {
186 if let Some(file) = &captions.file
187 && !found.texts.contains(file)
188 {
189 found.texts.push(file.clone());
190 }
191 }
192 Node::Slideshow(slideshow) => {
193 slideshow
194 .items
195 .iter()
196 .for_each(|item| add_item(found, item));
197 if let Some(path) = &slideshow.fit_to {
198 add_media(found, path, Some(FileKind::Audio));
199 }
200 }
201 Node::CaptionedVideo(captioned) => {
202 add_item(found, &captioned.video);
203 walk(&Node::Captions((*captioned.captions).clone()), found);
204 }
205 Node::Text(_)
206 | Node::Shape(_)
207 | Node::Color(_)
208 | Node::Generator(_)
209 | Node::Layout(_)
210 | Node::Raw(_)
211 | Node::Counter(_) => {}
212 }
213}