The export stops with the last picture, caption or no caption
Browse files at this commit64e09f91bb2cf54a70fa9b1925a4d59c4848e05bParent: d6c0e92- fix
- the export stops with the last picture, caption or no caption
2 changed files +135 -6
crates/engine/src/export.rsmodified+46 -6
| @@ -924,6 +924,27 @@ fn lane_subtitles(project: &Project, fps: f64) -> Vec<SubParams> { | |||
| 924 | .collect() | 924 | .collect() |
| 925 | } | 925 | } |
| 926 | 926 | ||
| 927 | +/// The composite spans an export *writes*: [`Project::composite_spans_from`] | ||
| 928 | +/// cut to where the media runs out ([`Project::media_frames`]). | ||
| 929 | +/// | ||
| 930 | +/// The walk itself is the timeline's ([`Project::timeline_frames`]), because a | ||
| 931 | +/// caption placed past the last picture holds the timeline open under it and | ||
| 932 | +/// playback parks on that tail -- but there is nothing to *write* out there, so | ||
| 933 | +/// an unbounded walk encoded and muxed black frames past the end of the media | ||
| 934 | +/// and padded the sound to match. This is the one place that end is decided: | ||
| 935 | +/// the frame count [`run`] publishes, the cues [`lane_subtitles`] clips and the | ||
| 936 | +/// blocks [`CopyPlan`] copies are then the same number by construction, which | ||
| 937 | +/// is what makes [`planned_lanes`]' "cut off there" true of the file. | ||
| 938 | +fn export_spans(project: &Project) -> Vec<crate::project::Span> { | ||
| 939 | + let end = project.media_frames(); | ||
| 940 | + let mut spans = project.composite_spans_from(0); | ||
| 941 | + spans.retain(|span| span.start < end); | ||
| 942 | + for span in &mut spans { | ||
| 943 | + span.len = span.len.min(end - span.start); | ||
| 944 | + } | ||
| 945 | + spans | ||
| 946 | +} | ||
| 947 | + | ||
| 927 | /// The track's cues where the *exported* timeline puts them, which is the only | 948 | /// The track's cues where the *exported* timeline puts them, which is the only |
| 928 | /// clock the file has: the export writes timeline frame 0 onwards and nothing | 949 | /// clock the file has: the export writes timeline frame 0 onwards and nothing |
| 929 | /// else, exactly as the sound is mixed from frame 0 and resized to the | 950 | /// else, exactly as the sound is mixed from frame 0 and resized to the |
| @@ -1275,7 +1296,24 @@ fn copy_audio( | |||
| 1275 | // everything on `A2`), and copying `A1`'s list in that case would write a | 1296 | // everything on `A2`), and copying `A1`'s list in that case would write a |
| 1276 | // file with no audio track at all -- silently, which is the failure this | 1297 | // file with no audio track at all -- silently, which is the failure this |
| 1277 | // whole comment is about. | 1298 | // whole comment is about. |
| 1278 | - let lanes = project.audio_segments_from(0, meta.frame_rate); | 1299 | + let mut lanes = project.audio_segments_from(0, meta.frame_rate); |
| 1300 | + // ...cut to what the picture writes ([`export_spans`]). The play list is | ||
| 1301 | + // the *timeline's* ([`Project::timeline_frames`]), so a caption hanging past | ||
| 1302 | + // the last picture ends every audio lane with a trailing gap -- silence this | ||
| 1303 | + // path would copy out past the last frame, three seconds of it on the film | ||
| 1304 | + // this was found on. Only a gap can be out there (`media_frames` is the last | ||
| 1305 | + // clip's own end), and the mixed paths already resize to the same length | ||
| 1306 | + // ([`encode_audio`]); a copy carries source packets and has no resize. | ||
| 1307 | + let over = f64::from(project.timeline_frames() - project.media_frames()) / meta.frame_rate; | ||
| 1308 | + for lane in &mut lanes { | ||
| 1309 | + match lane.last_mut() { | ||
| 1310 | + Some((None, _, len)) if *len > over => *len -= over, | ||
| 1311 | + Some((None, _, _)) if over > 0.0 => { | ||
| 1312 | + lane.pop(); | ||
| 1313 | + } | ||
| 1314 | + _ => {} | ||
| 1315 | + } | ||
| 1316 | + } | ||
| 1279 | let [segments] = &lanes[..] else { | 1317 | let [segments] = &lanes[..] else { |
| 1280 | return encode_audio(project, meta, kbps, mkv, shared); | 1318 | return encode_audio(project, meta, kbps, mkv, shared); |
| 1281 | }; | 1319 | }; |
| @@ -1414,7 +1452,7 @@ impl CopyPlan { | |||
| 1414 | let mut sources: Vec<Option<MkvDemuxer>> = (0..entries.len()).map(|_| None).collect(); | 1452 | let mut sources: Vec<Option<MkvDemuxer>> = (0..entries.len()).map(|_| None).collect(); |
| 1415 | let mut declared: Option<(Vec<u8>, ColorDescription)> = None; | 1453 | let mut declared: Option<(Vec<u8>, ColorDescription)> = None; |
| 1416 | let mut regions: Vec<CopyRegion> = Vec::new(); | 1454 | let mut regions: Vec<CopyRegion> = Vec::new(); |
| 1417 | - for span in project.composite_spans_from(0) { | 1455 | + for span in export_spans(project) { |
| 1418 | // A gap is black frames, which only an encoder makes. | 1456 | // A gap is black frames, which only an encoder makes. |
| 1419 | let (source, in_frame) = span.from?; | 1457 | let (source, in_frame) = span.from?; |
| 1420 | if !span.speed.is_normal() { | 1458 | if !span.speed.is_normal() { |
| @@ -2230,10 +2268,12 @@ fn run( | |||
| 2230 | let black = Black::new(meta); | 2268 | let black = Black::new(meta); |
| 2231 | // Spans, not clips: a gap in the video is part of the timeline and gets | 2269 | // Spans, not clips: a gap in the video is part of the timeline and gets |
| 2232 | // encoded too, as black frames. The picture count is therefore | 2270 | // encoded too, as black frames. The picture count is therefore |
| 2233 | - // `timeline_frames` however the lanes are arranged -- and *which* lane a | 2271 | + // `media_frames` however the lanes are arranged -- [`export_spans`] is that |
| 2234 | - // span comes from is `composite_spans_from`'s answer, the same one playback | 2272 | + // walk cut to it, so `done` ends on the `total` above instead of running |
| 2235 | - // shows, so an export is what was watched. | 2273 | + // past it under a trailing caption -- and *which* lane a span comes from is |
| 2236 | - for span in project.composite_spans_from(0) { | 2274 | + // `composite_spans_from`'s answer, the same one playback shows, so an |
| 2275 | + // export is what was watched. | ||
| 2276 | + for span in export_spans(project) { | ||
| 2237 | // Every clip reopens its own source file at its own in point; the | 2277 | // Every clip reopens its own source file at its own in point; the |
| 2238 | // encoder is *not* reopened, so the export is one continuous stream | 2278 | // encoder is *not* reopened, so the export is one continuous stream |
| 2239 | // whose GOP boundaries need not line up with the cuts -- nor with the | 2279 | // whose GOP boundaries need not line up with the cuts -- nor with the |
crates/engine/tests/export_subs.rsmodified+89 -0
| @@ -156,6 +156,10 @@ fn exported_want( | |||
| 156 | }, | 156 | }, |
| 157 | ); | 157 | ); |
| 158 | wait(&handle, Duration::from_secs(300)).expect("the export finishes"); | 158 | wait(&handle, Duration::from_secs(300)).expect("the export finishes"); |
| 159 | + // The bar ends where it was told it would: the picture loop's own count and | ||
| 160 | + // the total published for it are one number ([`export::export_spans`]), so a | ||
| 161 | + // walk that ran past the media would show up here as well as in the file. | ||
| 162 | + assert_eq!(handle.progress(), 1.0, "finished at full progress"); | ||
| 159 | let tracks = subtitle::of_matroska(&out).expect("read the export back"); | 163 | let tracks = subtitle::of_matroska(&out).expect("read the export back"); |
| 160 | assert_eq!(tracks.len(), want, "exactly the tracks asked for: {tracks:?}"); | 164 | assert_eq!(tracks.len(), want, "exactly the tracks asked for: {tracks:?}"); |
| 161 | for track in &tracks { | 165 | for track in &tracks { |
| @@ -218,6 +222,54 @@ fn ffmpeg_streams(path: &Path) -> Option<String> { | |||
| 218 | Some(listing) | 222 | Some(listing) |
| 219 | } | 223 | } |
| 220 | 224 | ||
| 225 | +/// The average luma of the **last** picture in `path`, as ffmpeg's | ||
| 226 | +/// `signalstats` measures it: what says a file's tail is a picture and not the | ||
| 227 | +/// encoded black a walk past the media would leave. `None` where there is no | ||
| 228 | +/// ffmpeg on the box, the same posture the readers above take. | ||
| 229 | +fn ffmpeg_last_luma(path: &Path) -> Option<f64> { | ||
| 230 | + let out = Command::new("ffmpeg") | ||
| 231 | + .args(["-v", "error", "-i"]) | ||
| 232 | + .arg(path) | ||
| 233 | + .args([ | ||
| 234 | + "-vf", | ||
| 235 | + "signalstats,metadata=print:key=lavfi.signalstats.YAVG:file=-", | ||
| 236 | + "-f", | ||
| 237 | + "null", | ||
| 238 | + "-", | ||
| 239 | + ]) | ||
| 240 | + .output() | ||
| 241 | + .ok()?; | ||
| 242 | + assert!( | ||
| 243 | + out.status.success(), | ||
| 244 | + "ffmpeg refused the exported picture: {}", | ||
| 245 | + String::from_utf8_lossy(&out.stderr) | ||
| 246 | + ); | ||
| 247 | + let printed = String::from_utf8_lossy(&out.stdout).into_owned(); | ||
| 248 | + let last = printed | ||
| 249 | + .lines() | ||
| 250 | + .filter_map(|line| line.split_once("YAVG=").map(|(_, v)| v.trim().to_string())) | ||
| 251 | + .next_back() | ||
| 252 | + .expect("signalstats printed a YAVG per frame"); | ||
| 253 | + println!("last written picture: YAVG {last}"); | ||
| 254 | + Some(last.parse().expect("YAVG is a number")) | ||
| 255 | +} | ||
| 256 | + | ||
| 257 | +/// When the file's **last sound** is, as ffprobe reads the packets: the sound is | ||
| 258 | +/// the other end that has to stop with the picture -- a copied lane's trailing | ||
| 259 | +/// gap is silence written past the last frame. `None` where there is no ffprobe. | ||
| 260 | +fn ffprobe_last_audio_pts(path: &Path) -> Option<f64> { | ||
| 261 | + let out = Command::new("ffprobe") | ||
| 262 | + .args(["-v", "error", "-select_streams", "a:0"]) | ||
| 263 | + .args(["-show_entries", "packet=pts_time", "-of", "csv=p=0"]) | ||
| 264 | + .arg(path) | ||
| 265 | + .output() | ||
| 266 | + .ok()?; | ||
| 267 | + let printed = String::from_utf8_lossy(&out.stdout).into_owned(); | ||
| 268 | + let last = printed.lines().filter(|l| !l.trim().is_empty()).next_back()?; | ||
| 269 | + println!("last audio packet: {last} s"); | ||
| 270 | + last.trim_end_matches(',').parse().ok() | ||
| 271 | +} | ||
| 272 | + | ||
| 221 | fn near(got: i64, want: i64, what: &str) { | 273 | fn near(got: i64, want: i64, what: &str) { |
| 222 | assert!( | 274 | assert!( |
| 223 | (got - want).abs() <= SLACK_US, | 275 | (got - want).abs() <= SLACK_US, |
| @@ -968,6 +1020,43 @@ fn a_caption_past_the_last_picture_is_cut_off_with_the_file() { | |||
| 968 | assert_eq!(cues.len(), 1, "the one cue the window holds: {cues:?}"); | 1020 | assert_eq!(cues.len(), 1, "the one cue the window holds: {cues:?}"); |
| 969 | near(cues[0].start_us, 4_500_000, "where the placement puts it"); | 1021 | near(cues[0].start_us, 4_500_000, "where the placement puts it"); |
| 970 | near(cues[0].end_us, 5_000_000, "cut off with the last picture"); | 1022 | near(cues[0].end_us, 5_000_000, "cut off with the last picture"); |
| 1023 | + | ||
| 1024 | + // ...and the *file* is that long, which is what makes the sentence above | ||
| 1025 | + // true rather than merely written: the picture stops with the media and the | ||
| 1026 | + // 60 frames the caption hangs over it are not encoded black. | ||
| 1027 | + let (written, _) = engine::demux::Demuxer::open(&out).expect("reopen the export"); | ||
| 1028 | + assert_eq!( | ||
| 1029 | + written.frame_count, FRAMES, | ||
| 1030 | + "the pictures end with the media, not with the caption" | ||
| 1031 | + ); | ||
| 1032 | + // A count alone would pass on a file whose tail is black, so the last | ||
| 1033 | + // picture is *looked at*: a padded frame is 16/128/128 and reads back at | ||
| 1034 | + // YAVG 16. ffmpeg does the looking -- this file is HEVC and there is no | ||
| 1035 | + // software HEVC decoder here to read it back with. | ||
| 1036 | + match ffmpeg_last_luma(&out) { | ||
| 1037 | + Some(luma) => assert!( | ||
| 1038 | + luma > 20.0, | ||
| 1039 | + "the last written picture is black (YAVG {luma:.2}): the walk ran past the media" | ||
| 1040 | + ), | ||
| 1041 | + None => println!("no ffmpeg on this box: the frame count is the only witness"), | ||
| 1042 | + } | ||
| 1043 | + // ...and the sound stops with the picture: the copied lane's trailing gap | ||
| 1044 | + // is the same overhang, written as silence rather than as black. | ||
| 1045 | + let duration_s = f64::from(written.frame_count) / written.frame_rate; | ||
| 1046 | + match ffprobe_last_audio_pts(&out) { | ||
| 1047 | + Some(pts) => assert!( | ||
| 1048 | + pts <= duration_s, | ||
| 1049 | + "the sound runs to {pts:.3} s, past the picture's {duration_s:.3} s" | ||
| 1050 | + ), | ||
| 1051 | + None => println!("no ffprobe on this box: the picture is the only witness"), | ||
| 1052 | + } | ||
| 1053 | + // The cue is inside the file it was clipped to, by the file's own clock. | ||
| 1054 | + let duration_us = (duration_s * 1e6).round() as i64; | ||
| 1055 | + assert!( | ||
| 1056 | + cues[0].end_us <= duration_us + SLACK_US, | ||
| 1057 | + "the cue ends at {} us, past the file's {duration_us} us", | ||
| 1058 | + cues[0].end_us | ||
| 1059 | + ); | ||
| 971 | std::fs::remove_file(&out).unwrap(); | 1060 | std::fs::remove_file(&out).unwrap(); |
| 972 | } | 1061 | } |
| 973 | 1062 | ||