dash-mpd 0.20.2

Parse, serialize, download an MPD manifest for MPEG-DASH or WebM-DASH media streaming
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
// Tests for Period concatenation with multi-period manifests
//
// To run tests while enabling printing to stdout/stderr
//
//    cargo test --test concat -- --show-output


pub mod common;
use std::fs;
use std::env;
use std::time::Duration;
use ffprobe::ffprobe;
use file_format::FileFormat;
use dash_mpd::fetch::DashDownloader;
use common::{check_file_size_approx, check_media_duration, setup_logging};


#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_noaudio_ffmpeg() {
    setup_logging();
    if env::var("CI").is_ok() {
        return;
    }
    let mpd_url = "https://dash.akamaized.net/fokus/adinsertion-samples/xlink/twoperiodsOR.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-noaudio-ffmpeg.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "ffmpeg")
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 5_781_840);
    let meta = ffprobe(out).unwrap();
    assert_eq!(meta.streams.len(), 2);
    let audio = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("audio"))))
        .expect("finding audio stream");
    assert_eq!(audio.codec_name, Some(String::from("aac")));
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}


#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_noaudio_ffmpegdemuxer() {
    setup_logging();
    if env::var("CI").is_ok() {
        return;
    }
    let mpd_url = "https://dash.akamaized.net/fokus/adinsertion-samples/xlink/twoperiodsOR.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-noaudio-ffmpegdemuxer.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "ffmpegdemuxer")
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 5_781_840);
    let meta = ffprobe(out).unwrap();
    assert_eq!(meta.streams.len(), 2);
    let audio = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("audio"))))
        .expect("finding audio stream");
    assert_eq!(audio.codec_name, Some(String::from("aac")));
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}


// mkvmerge cannot concat this stream to MP4: failure with error message
//   Quicktime/MP4 reader: Could not read chunk number 48/62 with size 1060 from position 15936. Aborting.
#[tokio::test]
#[cfg(not(feature = "libav"))]
#[should_panic(expected = "all concat helpers failed")]
async fn test_concat_noaudio_mkvmerge_mp4() {
    setup_logging();
    if env::var("CI").is_ok() {
        panic!("all concat helpers failed");
    }
    let mpd_url = "https://dash.akamaized.net/fokus/adinsertion-samples/xlink/twoperiodsOR.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-noaudio-mkvmerge.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "mkvmerge")
        .download_to(&out).await
        .unwrap();
    let _ = fs::remove_dir_all(tmpd);
}

// mkvmerge fails to concat. Check that the fallback to ffmpeg as a concat helper works 
#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_noaudio_mkv_concat_fallback() {
    setup_logging();
    if env::var("CI").is_ok() {
        return;
    }
    let mpd_url = "https://dash.akamaized.net/fokus/adinsertion-samples/xlink/twoperiodsOR.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-noaudio-mkvmerge.mkv");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "mkvmerge")
        .with_concat_preference("mkv", "mkvmerge,ffmpeg")
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::MatroskaVideo);
    check_file_size_approx(&out, 7_258_379);
    let meta = ffprobe(out).unwrap();
    assert_eq!(meta.streams.len(), 2);
    let audio = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("audio"))))
        .expect("finding audio stream");
    assert_eq!(audio.codec_name, Some(String::from("vorbis")));
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}


#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_singleases_ffmpeg() {
    setup_logging();
    if env::var("CI").is_ok() {
        return;
    }
    let mpd_url = "https://dash.akamaized.net/fokus/adinsertion-samples/xlink/singleases.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-singleases-ffmpeg.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "ffmpeg")
        .minimum_period_duration(Duration::new(10, 0))
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 5_781_840);
    let meta = ffprobe(out).unwrap();
    assert_eq!(meta.streams.len(), 2);
    let audio = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("audio"))))
        .expect("finding audio stream");
    assert_eq!(audio.codec_name, Some(String::from("aac")));
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}

#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_singleases_ffmpegdemuxer() {
    setup_logging();
    if env::var("CI").is_ok() {
        return;
    }
    let mpd_url = "https://dash.akamaized.net/fokus/adinsertion-samples/xlink/singleases.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-singleases-ffmpeg.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "ffmpegdemuxer")
        .minimum_period_duration(Duration::new(10, 0))
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 5_781_840);
    let meta = ffprobe(out).unwrap();
    assert_eq!(meta.streams.len(), 2);
    let audio = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("audio"))))
        .expect("finding audio stream");
    assert_eq!(audio.codec_name, Some(String::from("aac")));
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}

// mkvmerge is unable to concatenate these streams: fails with an error message
//
//   The track number 0 from the file '/tmp/.tmpkgxD7n/concat-singleases-mkvmerge-p2.mp4' can
//   probably not be appended correctly to the track number 0 from the file
//   '/tmp/dashmpdrsbnMoM.mkv': The codec's private data does not match. Both have the same length
//   (41) but different content.
#[tokio::test]
#[cfg(not(feature = "libav"))]
#[should_panic(expected = "all concat helpers failed")]
async fn test_concat_singleases_mkvmerge() {
    setup_logging();
    if env::var("CI").is_ok() {
        panic!("all concat helpers failed");
    }
    let mpd_url = "https://dash.akamaized.net/fokus/adinsertion-samples/xlink/singleases.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-singleases-mkvmerge.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "mkvmerge")
        .minimum_period_duration(Duration::new(10, 0))
        .download_to(&out).await
        .unwrap();
    let _ = fs::remove_dir_all(tmpd);
}


#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_heliocentrism_ffmpeg_mp4() {
    setup_logging();
    let mpd_url = "https://storage.googleapis.com/shaka-demo-assets/heliocentrism/heliocentrism.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-helio-ffmpeg.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "ffmpeg")
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 36_829);
    let meta = ffprobe(&out).unwrap();
    // This manifest has no audio track.
    assert_eq!(meta.streams.len(), 1);
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    check_media_duration(&out, 4.9);
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}

#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_heliocentrism_ffmpegdemuxer_mp4() {
    setup_logging();
    let mpd_url = "https://storage.googleapis.com/shaka-demo-assets/heliocentrism/heliocentrism.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-helio-ffmpeg-demuxer.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "ffmpegdemuxer")
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    // FIXME problem here: on Windows we see a size of 16_384
    // check_file_size_approx(&out, 40_336);
    let meta = ffprobe(&out).unwrap();
    assert_eq!(meta.streams.len(), 1);
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    check_media_duration(&out, 4.9);
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}

// If ffmpeg is used as a concat helper for mkv files that were muxed using mkvmerge (our default
// muxer for that container format), we see concatenation errors. Using ffmpeg for both muxing and
// concatenation helps work around this problem.
#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_heliocentrism_ffmpeg_mkv() {
    setup_logging();
    let mpd_url = "https://storage.googleapis.com/shaka-demo-assets/heliocentrism/heliocentrism.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-helio-ffmpeg.mkv");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .verbosity(3)
        .with_muxer_preference("mkv", "ffmpeg")
        .with_concat_preference("mkv", "ffmpeg")
        .download_to(&out).await
        .unwrap();
    let fp = std::process::Command::new("ffprobe")
        .env("LANG", "C")
        .arg("-hide_banner")
        .arg(out.to_str().unwrap())
        .output()
        .expect("spawning ffprobe");
    let stdout = String::from_utf8_lossy(&fp.stdout);
    if !stdout.is_empty() {
        println!("ffprobe stdout> {stdout}");
    }
    let stderr = String::from_utf8_lossy(&fp.stderr);
    if !stderr.is_empty() {
        println!("ffprobe stderr> {stderr}");
    }
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::MatroskaVideo);
    check_file_size_approx(&out, 35_937);
    let meta = ffprobe(&out).unwrap();
    assert_eq!(meta.streams.len(), 1);
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    check_media_duration(&out, 4.9);
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}

#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_heliocentrism_ffmpegdemuxer_mkv() {
    setup_logging();
    let mpd_url = "https://storage.googleapis.com/shaka-demo-assets/heliocentrism/heliocentrism.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-helio-ffmpeg.mkv");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .verbosity(2)
        .with_muxer_preference("mkv", "ffmpeg")
        .with_concat_preference("mkv", "ffmpegdemuxer")
        .download_to(&out).await
        .unwrap();
    let fp = std::process::Command::new("ffprobe")
        .env("LANG", "C")
        .arg("-hide_banner")
        .arg(out.to_str().unwrap())
        .output()
        .expect("spawning ffprobe");
    let stdout = String::from_utf8_lossy(&fp.stdout);
    if !stdout.is_empty() {
        println!("ffprobe stdout> {stdout}");
    }
    let stderr = String::from_utf8_lossy(&fp.stderr);
    if !stderr.is_empty() {
        println!("ffprobe stderr> {stderr}");
    }
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::MatroskaVideo);
    // On Windows we are seeing the size 37005 instead of 42_060
    // check_file_size_approx(&out, 37_005);
    let meta = ffprobe(&out).unwrap();
    assert_eq!(meta.streams.len(), 1);
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    check_media_duration(&out, 4.9);
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}

// 2025-06: mkvmerge fails to concatenate this stream with error
//
//   Quicktime/MP4 reader: Could not read chunk number 48/62 with size 1060 from position 15936. Aborting.
//
// 2026-01: mkvmerge 97.0 no longer fails to concatenate, but produces a file with Matroska content,
// instead of MPEG4 as requested, so we disable this test.
#[ignore]
#[tokio::test]
#[cfg(not(feature = "libav"))]
#[should_panic(expected = "all concat helpers failed")]
async fn test_concat_heliocentrism_mkvmerge_mp4() {
    setup_logging();
    let mpd_url = "https://storage.googleapis.com/shaka-demo-assets/heliocentrism/heliocentrism.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-helio-mkvmerge.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .verbosity(2)
        .with_concat_preference("mp4", "mkvmerge")
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 42_060);
    let meta = ffprobe(&out).unwrap();
    assert_eq!(meta.streams.len(), 2);
    let audio = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("audio"))))
        .expect("finding audio stream");
    assert_eq!(audio.codec_name, Some(String::from("aac")));
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    check_media_duration(&out, 4.9);
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}


#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_heliocentrism_mkvmerge_mkv() {
    setup_logging();
    let mpd_url = "https://storage.googleapis.com/shaka-demo-assets/heliocentrism/heliocentrism.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-helio-mkvmerge.mkv");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .verbosity(3)
        .with_concat_preference("mkv", "mkvmerge")
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::MatroskaVideo);
    // File size here is unreliable, is quite different on Microsoft Windows for example (30_699)
    // check_file_size_approx(&out, 42_060);
    let meta = ffprobe(&out).unwrap();
    // mkvmerge notices that there is no audio stream, so only includes the video stream in the
    // output file (ffmpeg generates a container with an audio and a video stream).
    assert_eq!(meta.streams.len(), 1);
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    // FIXME this duration test is failing with an identified duration of 4.104s, though it looks
    // like the three segments are being downloaded correctly and merged. Perhaps an mkvmerge bug?
    // check_media_duration(&out, 4.9);
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}


#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_heliocentrism_p1p2() {
    setup_logging();
    let mpd_url = "https://storage.googleapis.com/shaka-demo-assets/heliocentrism/heliocentrism.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-helio_p1p2.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "ffmpeg")
        // here we should be dropping period #3 (id=2) whose duration is 0.701s
        .minimum_period_duration(Duration::new(2, 0))
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 30_496);
    let meta = ffprobe(out).unwrap();
    assert_eq!(meta.streams.len(), 1);
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}


#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_dashif_5bnomor2() {
    setup_logging();
    if env::var("CI").is_ok() {
        return;
    }
    // From https://testassets.dashif.org/#feature/details/586fb3879ae9045678eab587
    let mpd_url = "https://dash.akamaized.net/dash264/TestCases/5b/nomor/2.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-5bnomor2.mp4");
    DashDownloader::new(mpd_url)
        .worst_quality()
        .sandbox(true)
        .with_concat_preference("mp4", "ffmpegdemuxer")
        .download_to(&out).await
        .unwrap();
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 119_710_971);
    let meta = ffprobe(&out).unwrap();
    assert_eq!(meta.streams.len(), 2);
    let audio = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("audio"))))
        .expect("finding audio stream");
    assert_eq!(audio.codec_name, Some(String::from("aac")));
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    check_media_duration(&out, 710.0);
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}

#[tokio::test]
#[cfg(not(feature = "libav"))]
async fn test_concat_axinom_multiperiod() {
    setup_logging();
    if env::var("CI").is_ok() {
        return;
    }
    // Keys obtained from https://github.com/Axinom/public-test-vectors/blob/master/TestVectors-v7-v8.md
    let mpd_url = "https://media.axprod.net/TestVectors/v7-MultiDRM-MultiKey-MultiPeriod/Manifest.mpd";
    let tmpd = tempfile::tempdir().unwrap();
    let out = tmpd.path().join("concat-axinom-multiperiod.mp4");
    DashDownloader::new(mpd_url)
        .verbosity(2)
        .sandbox(true)
        .worst_quality()
        .add_decryption_key(String::from("0872786ef9e7465fa3a24e5b0ef8fa45"),
                            String::from("c3261179bab61eeec979d2d4069511cf"))
        .add_decryption_key(String::from("29f05e8fa1ae46e480e922dcd44cd7a1"),
                            String::from("0711b17c84a90cbb41097264c901b732"))
        .with_concat_preference("mp4", "ffmpegdemuxer")
        .download_to(&out).await
        .unwrap();
    let fp = std::process::Command::new("ffprobe")
        .env("LANG", "C")
        .arg("-hide_banner")
        .arg(out.to_str().unwrap())
        .output()
        .expect("spawning ffprobe");
    let stdout = String::from_utf8_lossy(&fp.stdout);
    if !stdout.is_empty() {
        println!("ffprobe stdout> {stdout}");
    }
    let stderr = String::from_utf8_lossy(&fp.stderr);
    if !stderr.is_empty() {
        println!("ffprobe stderr> {stderr}");
    }
    let mi = std::process::Command::new("mediainfo")
        .env("LANG", "C")
        .arg(out.to_str().unwrap())
        .output()
        .expect("spawning mediainfo");
    let stdout = String::from_utf8_lossy(&mi.stdout);
    if !stdout.is_empty() {
        println!("mediainfo stdout> {stdout}");
    }
    let stderr = String::from_utf8_lossy(&mi.stderr);
    if !stderr.is_empty() {
        println!("mediainfo stderr> {stderr}");
    }
    let format = FileFormat::from_file(&out).unwrap();
    assert_eq!(format, FileFormat::Mpeg4Part14Video);
    check_file_size_approx(&out, 83_015_660);
    let meta = ffprobe(&out).unwrap();
    assert_eq!(meta.streams.len(), 2);
    let audio = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("audio"))))
        .expect("finding audio stream");
    assert_eq!(audio.codec_name, Some(String::from("aac")));
    let video = meta.streams.iter()
        .find(|s| s.codec_type.eq(&Some(String::from("video"))))
        .expect("finding video stream");
    assert_eq!(video.codec_name, Some(String::from("h264")));
    assert!(video.width.is_some());
    // On Linux, we were seeing 1468.5 seconds; on Windows for some crazy reason we see a duration
    // of 10751.3 (2h29), displayed both by ffprobe and by mediainfo.
    if ! env::consts::OS.eq("windows") {
        check_media_duration(&out, 1468.5);
    }
    let entries = fs::read_dir(tmpd.path()).unwrap();
    let count = entries.count();
    assert_eq!(count, 1, "Expecting a single output file, got {count}");
    let _ = fs::remove_dir_all(tmpd);
}