-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodecMediaFacadeTest.java
More file actions
760 lines (662 loc) · 29.8 KB
/
CodecMediaFacadeTest.java
File metadata and controls
760 lines (662 loc) · 29.8 KB
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
package me.tamkungz.codecmedia;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Stream;
import javax.imageio.ImageIO;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
class CodecMediaFacadeTest {
private record ExampleFixtureExpectation(
String fileName,
String mimeType,
String extension,
me.tamkungz.codecmedia.model.MediaType mediaType
) {
}
@Test
void createDefault_shouldReturnEngine() {
CodecMediaEngine engine = CodecMedia.createDefault();
assertNotNull(engine);
}
@ParameterizedTest(name = "probe real fixture: {0}")
@MethodSource("exampleFixtures")
void probe_shouldDetectRealExampleFixtures(ExampleFixtureExpectation fixture) throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path input = Path.of("src/test/resources/example", fixture.fileName());
assertTrue(Files.exists(input));
var result = engine.probe(input);
assertEquals(fixture.mimeType(), result.mimeType());
assertEquals(fixture.extension(), result.extension());
assertEquals(fixture.mediaType(), result.mediaType());
assertTrue(result.tags().containsKey("sizeBytes"));
}
@ParameterizedTest(name = "validate real fixture: {0}")
@MethodSource("exampleFixtures")
void validate_strictShouldAcceptRealExampleFixtures(ExampleFixtureExpectation fixture) throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path input = Path.of("src/test/resources/example", fixture.fileName());
assertTrue(Files.exists(input));
var result = engine.validate(input, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
}
@Test
void probe_shouldDetectMp3ByExtension() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempFile = Files.createTempFile("codecmedia-", ".mp3");
try {
var result = engine.probe(tempFile);
assertEquals("audio/mpeg", result.mimeType());
assertEquals("mp3", result.extension());
} finally {
Files.deleteIfExists(tempFile);
}
}
@Test
void validate_shouldFailWhenFileMissing() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path missing = Path.of("this-file-should-not-exist-12345.mp4");
var result = engine.validate(missing, null);
assertTrue(result.errors().stream().anyMatch(e -> e.contains("does not exist")));
}
@Test
void writeAndReadMetadata_shouldRoundTripViaSidecar() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp3 = createTempFileWithResource("c-major-scale_test_audacity.mp3", ".mp3");
try {
engine.writeMetadata(tempMp3, new me.tamkungz.codecmedia.model.Metadata(Map.of(
"title", "C Major Scale",
"artist", "CodecMedia Test"
)));
var metadata = engine.readMetadata(tempMp3);
assertEquals("C Major Scale", metadata.entries().get("title"));
assertEquals("CodecMedia Test", metadata.entries().get("artist"));
assertEquals("audio/mpeg", metadata.entries().get("mimeType"));
} finally {
Files.deleteIfExists(tempMp3.resolveSibling(tempMp3.getFileName() + ".codecmedia.properties"));
Files.deleteIfExists(tempMp3);
}
}
@Test
void extractAudio_shouldCreateOutputFile() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp3 = createTempFileWithResource("c-major-scale_test_web-convert_mono.mp3", ".mp3");
Path outputDir = Files.createTempDirectory("codecmedia-extract-");
try {
var result = engine.extractAudio(
tempMp3,
outputDir,
new me.tamkungz.codecmedia.options.AudioExtractOptions("mp3", 192, 0)
);
assertNotNull(result.outputFile());
assertTrue(Files.exists(result.outputFile()));
assertEquals("mp3", result.format());
} finally {
deleteDirectory(outputDir);
Files.deleteIfExists(tempMp3);
}
}
@Test
void convert_shouldRespectOverwriteFlag() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp3 = createTempFileWithResource("c-major-scale_test_audacity.mp3", ".mp3");
Path output = Files.createTempFile("codecmedia-convert-", ".mp3");
try {
var converted = engine.convert(tempMp3, output, new me.tamkungz.codecmedia.options.ConversionOptions("mp3", "balanced", true));
assertTrue(Files.size(output) > 0);
assertFalse(converted.reencoded());
boolean threw = false;
try {
engine.convert(tempMp3, output, new me.tamkungz.codecmedia.options.ConversionOptions("mp3", "balanced", false));
} catch (CodecMediaException expected) {
threw = true;
}
assertTrue(threw);
} finally {
Files.deleteIfExists(output);
Files.deleteIfExists(tempMp3);
}
}
@Test
void convert_shouldRejectVideoToAudioRouteForNow() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp4 = createTempFileWithResource("mp4_test.mp4", ".mp4");
Path outputMp3 = Files.createTempFile("codecmedia-video-to-audio-", ".mp3");
try {
boolean threw = false;
try {
engine.convert(tempMp4, outputMp3, new me.tamkungz.codecmedia.options.ConversionOptions("mp3", "balanced", true));
} catch (CodecMediaException expected) {
threw = expected.getMessage().contains("video->audio");
}
assertTrue(threw);
} finally {
Files.deleteIfExists(outputMp3);
Files.deleteIfExists(tempMp4);
}
}
@Test
void convert_shouldRejectAudioToImageRouteForNow() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp3 = createTempFileWithResource("c-major-scale_test_audacity.mp3", ".mp3");
Path outputPng = Files.createTempFile("codecmedia-audio-to-image-", ".png");
try {
boolean threw = false;
try {
engine.convert(tempMp3, outputPng, new me.tamkungz.codecmedia.options.ConversionOptions("png", "balanced", true));
} catch (CodecMediaException expected) {
threw = expected.getMessage().contains("audio->image");
}
assertTrue(threw);
} finally {
Files.deleteIfExists(outputPng);
Files.deleteIfExists(tempMp3);
}
}
@Test
void convert_shouldTranscodePngToJpg() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempPng = createTempFileWithResource("png_test.png", ".png");
Path outputJpg = Files.createTempFile("codecmedia-png-to-jpg-", ".jpg");
try {
var converted = engine.convert(tempPng, outputJpg, new me.tamkungz.codecmedia.options.ConversionOptions("jpg", "balanced", true));
assertEquals("jpg", converted.format());
assertTrue(converted.reencoded());
assertTrue(Files.exists(outputJpg));
assertTrue(Files.size(outputJpg) > 0);
var probed = engine.probe(outputJpg);
assertEquals("image/jpeg", probed.mimeType());
assertEquals("jpg", probed.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.IMAGE, probed.mediaType());
} finally {
Files.deleteIfExists(outputJpg);
Files.deleteIfExists(tempPng);
}
}
@Test
void convert_shouldTranscodeJpgToPng() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempJpg = createTempJpegFixture(".jpg");
Path outputPng = Files.createTempFile("codecmedia-jpg-to-png-", ".png");
try {
var converted = engine.convert(tempJpg, outputPng, new me.tamkungz.codecmedia.options.ConversionOptions("png", "balanced", true));
assertEquals("png", converted.format());
assertTrue(converted.reencoded());
assertTrue(Files.exists(outputPng));
assertTrue(Files.size(outputPng) > 0);
var probed = engine.probe(outputPng);
assertEquals("image/png", probed.mimeType());
assertEquals("png", probed.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.IMAGE, probed.mediaType());
} finally {
Files.deleteIfExists(outputPng);
Files.deleteIfExists(tempJpg);
}
}
@Test
void convert_shouldRejectVideoToVideoTranscodeForNow() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp4 = createTempFileWithResource("mp4_test.mp4", ".mp4");
Path outputMkv = Files.createTempFile("codecmedia-video-to-video-", ".mkv");
try {
boolean threw = false;
try {
engine.convert(tempMp4, outputMkv, new me.tamkungz.codecmedia.options.ConversionOptions("mkv", "balanced", true));
} catch (CodecMediaException expected) {
threw = expected.getMessage().contains("video->video");
}
assertTrue(threw);
} finally {
Files.deleteIfExists(outputMkv);
Files.deleteIfExists(tempMp4);
}
}
@Test
void convert_shouldRejectAudioToAudioTranscodeForNow() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp3 = createTempFileWithResource("c-major-scale_test_audacity.mp3", ".mp3");
Path outputWav = Files.createTempFile("codecmedia-audio-to-audio-", ".wav");
try {
boolean threw = false;
try {
engine.convert(tempMp3, outputWav, new me.tamkungz.codecmedia.options.ConversionOptions("wav", "balanced", true));
} catch (CodecMediaException expected) {
threw = expected.getMessage().contains("audio->audio");
}
assertTrue(threw);
} finally {
Files.deleteIfExists(outputWav);
Files.deleteIfExists(tempMp3);
}
}
@Test
void convert_shouldAllowWavToPcmStubRoute() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempWav = createTempFileWithResource("c-major-scale_test_ableton-live.wav", ".wav");
Path outputPcm = Files.createTempFile("codecmedia-wav-to-pcm-", ".pcm");
try {
var converted = engine.convert(tempWav, outputPcm, new me.tamkungz.codecmedia.options.ConversionOptions("pcm", "balanced", true));
assertEquals("pcm", converted.format());
assertFalse(converted.reencoded());
assertTrue(Files.exists(outputPcm));
assertTrue(Files.size(outputPcm) > 0);
} finally {
Files.deleteIfExists(outputPcm);
Files.deleteIfExists(tempWav);
}
}
@Test
void validate_shouldRespectMaxBytesOption() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp3 = createTempFileWithResource("c-major-scale_test_audacity.mp3", ".mp3");
try {
long fileSize = Files.size(tempMp3);
var result = engine.validate(tempMp3, new me.tamkungz.codecmedia.options.ValidationOptions(false, fileSize - 1));
assertFalse(result.valid());
assertTrue(result.errors().stream().anyMatch(e -> e.contains("exceeds maxBytes")));
} finally {
Files.deleteIfExists(tempMp3);
}
}
@Test
void validate_strictShouldAcceptValidWavFixture() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempWav = createTempFileWithResource("c-major-scale_test_ableton-live.wav", ".wav");
try {
var result = engine.validate(tempWav, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
} finally {
Files.deleteIfExists(tempWav);
}
}
@Test
void probe_shouldDetectPngBySignature() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempPng = createTempFileWithResource("png_test.png", ".png");
try {
var result = engine.probe(tempPng);
assertEquals("image/png", result.mimeType());
assertEquals("png", result.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.IMAGE, result.mediaType());
assertFalse(result.streams().isEmpty());
assertNotNull(result.streams().get(0).width());
assertNotNull(result.streams().get(0).height());
assertTrue(result.streams().get(0).width() > 0);
assertTrue(result.streams().get(0).height() > 0);
} finally {
Files.deleteIfExists(tempPng);
}
}
@Test
void validate_strictShouldAcceptValidPngFixture() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempPng = createTempFileWithResource("png_test.png", ".png");
try {
var result = engine.validate(tempPng, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
} finally {
Files.deleteIfExists(tempPng);
}
}
@Test
void probe_shouldDetectJpegBySignature() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempJpeg = createTempJpegFixture(".jpg");
try {
var result = engine.probe(tempJpeg);
assertEquals("image/jpeg", result.mimeType());
assertEquals("jpg", result.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.IMAGE, result.mediaType());
assertFalse(result.streams().isEmpty());
assertEquals(1, result.streams().get(0).width());
assertEquals(1, result.streams().get(0).height());
} finally {
Files.deleteIfExists(tempJpeg);
}
}
@Test
void probe_shouldRespectJpegExtensionVariant() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempJpeg = createTempJpegFixture(".jpeg");
try {
var result = engine.probe(tempJpeg);
assertEquals("image/jpeg", result.mimeType());
assertEquals("jpeg", result.extension());
} finally {
Files.deleteIfExists(tempJpeg);
}
}
@Test
void validate_strictShouldAcceptValidJpegFixture() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempJpeg = createTempJpegFixture(".jpg");
try {
var result = engine.validate(tempJpeg, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
} finally {
Files.deleteIfExists(tempJpeg);
}
}
@Test
void probe_shouldDetectMp4AndProvideBasicVideoInfo() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp4 = createTempFileWithResource("mp4_test.mp4", ".mp4");
try {
var result = engine.probe(tempMp4);
assertEquals("video/mp4", result.mimeType());
assertEquals("mp4", result.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.VIDEO, result.mediaType());
assertNotNull(result.tags());
assertTrue(result.tags().containsKey("sizeBytes"));
assertTrue(result.tags().containsKey("majorBrand"));
if (result.durationMillis() != null) {
assertTrue(result.durationMillis() > 0);
}
if (!result.streams().isEmpty()) {
assertNotNull(result.streams().get(0).width());
assertNotNull(result.streams().get(0).height());
assertTrue(result.streams().get(0).width() > 0);
assertTrue(result.streams().get(0).height() > 0);
}
} finally {
Files.deleteIfExists(tempMp4);
}
}
@Test
void validate_strictShouldAcceptValidMp4Fixture() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp4 = createTempFileWithResource("mp4_test.mp4", ".mp4");
try {
var result = engine.validate(tempMp4, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
} finally {
Files.deleteIfExists(tempMp4);
}
}
@Test
void probe_shouldDetectM4aAsAudioMp4() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempM4a = createTempM4aFixture();
try {
var result = engine.probe(tempM4a);
assertEquals("audio/mp4", result.mimeType());
assertEquals("m4a", result.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.AUDIO, result.mediaType());
assertTrue(result.tags().containsKey("sizeBytes"));
} finally {
Files.deleteIfExists(tempM4a);
}
}
@Test
void validate_strictShouldAcceptValidM4aFixture() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempM4a = createTempM4aFixture();
try {
var result = engine.validate(tempM4a, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
} finally {
Files.deleteIfExists(tempM4a);
}
}
@Test
void probe_shouldDetectFlacBySignature() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempFlac = createTempFlacFixture();
try {
var result = engine.probe(tempFlac);
assertEquals("audio/flac", result.mimeType());
assertEquals("flac", result.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.AUDIO, result.mediaType());
assertTrue(result.tags().containsKey("sizeBytes"));
assertTrue(result.tags().containsKey("bitsPerSample"));
assertFalse(result.streams().isEmpty());
assertEquals("flac", result.streams().get(0).codec());
} finally {
Files.deleteIfExists(tempFlac);
}
}
@Test
void validate_strictShouldAcceptValidFlacFixture() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempFlac = createTempFlacFixture();
try {
var result = engine.validate(tempFlac, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
} finally {
Files.deleteIfExists(tempFlac);
}
}
@Test
void probe_shouldDetectMovByContainerHeader() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMov = createTempMovFixture();
try {
var result = engine.probe(tempMov);
assertEquals("video/quicktime", result.mimeType());
assertEquals("mov", result.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.VIDEO, result.mediaType());
assertTrue(result.tags().containsKey("sizeBytes"));
assertTrue(result.tags().containsKey("majorBrand"));
} finally {
Files.deleteIfExists(tempMov);
}
}
@Test
void validate_strictShouldAcceptValidMovFixture() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMov = createTempMovFixture();
try {
var result = engine.validate(tempMov, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
} finally {
Files.deleteIfExists(tempMov);
}
}
@Test
void probe_shouldDetectWebmByContainerHeader() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempWebm = createTempWebmFixture();
try {
var result = engine.probe(tempWebm);
assertEquals("video/webm", result.mimeType());
assertEquals("webm", result.extension());
assertEquals(me.tamkungz.codecmedia.model.MediaType.VIDEO, result.mediaType());
assertTrue(result.tags().containsKey("sizeBytes"));
} finally {
Files.deleteIfExists(tempWebm);
}
}
@Test
void validate_strictShouldAcceptValidWebmFixture() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempWebm = createTempWebmFixture();
try {
var result = engine.validate(tempWebm, new me.tamkungz.codecmedia.options.ValidationOptions(true, 0));
assertTrue(result.valid());
assertTrue(result.errors().isEmpty());
} finally {
Files.deleteIfExists(tempWebm);
}
}
@Test
void convert_shouldRejectMovToWebmVideoToVideoRouteForNow() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMov = createTempMovFixture();
Path outputWebm = Files.createTempFile("codecmedia-mov-to-webm-", ".webm");
try {
boolean threw = false;
try {
engine.convert(tempMov, outputWebm, new me.tamkungz.codecmedia.options.ConversionOptions("webm", "balanced", true));
} catch (CodecMediaException expected) {
threw = expected.getMessage().contains("video->video");
}
assertTrue(threw);
} finally {
Files.deleteIfExists(outputWebm);
Files.deleteIfExists(tempMov);
}
}
@Test
void get_shouldAliasProbe() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp3 = createTempFileWithResource("c-major-scale_test_audacity.mp3", ".mp3");
try {
var a = engine.probe(tempMp3);
var b = engine.get(tempMp3);
assertEquals(a.mimeType(), b.mimeType());
assertEquals(a.extension(), b.extension());
assertEquals(a.mediaType(), b.mediaType());
} finally {
Files.deleteIfExists(tempMp3);
}
}
@Test
void play_shouldSupportDryRunForAudioImageVideo() throws Exception {
CodecMediaEngine engine = CodecMedia.createDefault();
Path tempMp3 = createTempFileWithResource("c-major-scale_test_audacity.mp3", ".mp3");
Path tempPng = createTempFileWithResource("png_test.png", ".png");
Path tempMp4 = createTempFileWithResource("mp4_test.mp4", ".mp4");
try {
var audioPlay = engine.play(tempMp3, new me.tamkungz.codecmedia.options.PlaybackOptions(true, false));
assertTrue(audioPlay.started());
assertEquals(me.tamkungz.codecmedia.model.MediaType.AUDIO, audioPlay.mediaType());
var imagePlay = engine.play(tempPng, new me.tamkungz.codecmedia.options.PlaybackOptions(true, false));
assertTrue(imagePlay.started());
assertEquals(me.tamkungz.codecmedia.model.MediaType.IMAGE, imagePlay.mediaType());
var videoPlay = engine.play(tempMp4, new me.tamkungz.codecmedia.options.PlaybackOptions(true, false));
assertTrue(videoPlay.started());
assertEquals(me.tamkungz.codecmedia.model.MediaType.VIDEO, videoPlay.mediaType());
} finally {
Files.deleteIfExists(tempMp3);
Files.deleteIfExists(tempPng);
Files.deleteIfExists(tempMp4);
}
}
private static Path createTempFileWithResource(String resourceName, String suffix) throws IOException {
Path resource = Path.of("src/test/resources", resourceName);
Path temp = Files.createTempFile("codecmedia-fixture-", suffix);
Files.copy(resource, temp, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
return temp;
}
private static Path createTempJpegFixture(String suffix) throws IOException {
Path temp = Files.createTempFile("codecmedia-jpeg-", suffix);
BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
image.setRGB(0, 0, 0xFFFFFF);
boolean ok = ImageIO.write(image, "jpg", temp.toFile());
if (!ok) {
throw new IOException("No JPEG writer available in ImageIO runtime");
}
return temp;
}
private static Path createTempMovFixture() throws IOException {
Path temp = Files.createTempFile("codecmedia-mov-", ".mov");
Files.write(temp, minimalMovBytes());
return temp;
}
private static Path createTempM4aFixture() throws IOException {
Path temp = Files.createTempFile("codecmedia-m4a-", ".m4a");
Files.write(temp, minimalM4aBytes());
return temp;
}
private static Path createTempFlacFixture() throws IOException {
Path temp = Files.createTempFile("codecmedia-flac-", ".flac");
Files.write(temp, minimalFlacBytes());
return temp;
}
private static Path createTempWebmFixture() throws IOException {
Path temp = Files.createTempFile("codecmedia-webm-", ".webm");
Files.write(temp, minimalWebmBytes());
return temp;
}
private static byte[] minimalMovBytes() {
// 20-byte ftyp box: size(4) + type(4) + major_brand(4) + minor(4) + compatible_brand(4)
return new byte[] {
0x00, 0x00, 0x00, 0x14,
'f', 't', 'y', 'p',
'q', 't', ' ', ' ',
0x00, 0x00, 0x00, 0x00,
'q', 't', ' ', ' '
};
}
private static byte[] minimalM4aBytes() {
return new byte[] {
0x00, 0x00, 0x00, 0x14,
'f', 't', 'y', 'p',
'M', '4', 'A', ' ',
0x00, 0x00, 0x00, 0x00,
'i', 's', 'o', 'm'
};
}
private static byte[] minimalFlacBytes() {
byte[] bytes = new byte[4 + 4 + 34];
bytes[0] = 'f'; bytes[1] = 'L'; bytes[2] = 'a'; bytes[3] = 'C';
bytes[4] = (byte) 0x80;
bytes[5] = 0x00;
bytes[6] = 0x00;
bytes[7] = 34;
int sampleRate = 44100;
int channels = 2;
int bitsPerSample = 16;
long totalSamples = 44100L;
long packed = ((long) sampleRate & 0xFFFFFL) << 44;
packed |= ((long) (channels - 1) & 0x7L) << 41;
packed |= ((long) (bitsPerSample - 1) & 0x1FL) << 36;
packed |= (totalSamples & 0xFFFFFFFFFL);
int streamInfoOffset = 8;
for (int i = 0; i < 8; i++) {
bytes[streamInfoOffset + 10 + i] = (byte) ((packed >>> (56 - (i * 8))) & 0xFF);
}
return bytes;
}
private static byte[] minimalWebmBytes() {
// Minimal EBML header + DocType=webm segment that parser accepts for strict validation.
return new byte[] {
0x1A, 0x45, (byte) 0xDF, (byte) 0xA3,
(byte) 0x88,
0x42, (byte) 0x82, (byte) 0x84,
'w', 'e', 'b', 'm',
0x00, 0x00, 0x00, 0x00
};
}
private static void deleteDirectory(Path dir) throws IOException {
if (!Files.exists(dir)) {
return;
}
try (var walk = Files.walk(dir)) {
walk.sorted((a, b) -> b.compareTo(a)).forEach(path -> {
try {
Files.deleteIfExists(path);
} catch (IOException ignored) {
// ignore cleanup errors in tests
}
});
}
}
private static Stream<ExampleFixtureExpectation> exampleFixtures() {
return Stream.of(
new ExampleFixtureExpectation("file_example_MP3_1MG.mp3", "audio/mpeg", "mp3", me.tamkungz.codecmedia.model.MediaType.AUDIO),
new ExampleFixtureExpectation("file_example_MP3_700KB.mp3", "audio/mpeg", "mp3", me.tamkungz.codecmedia.model.MediaType.AUDIO),
new ExampleFixtureExpectation("file_example_MP4_480_1_5MG.mp4", "video/mp4", "mp4", me.tamkungz.codecmedia.model.MediaType.VIDEO),
new ExampleFixtureExpectation("file_example_MP4_640_3MG.mp4", "video/mp4", "mp4", me.tamkungz.codecmedia.model.MediaType.VIDEO),
new ExampleFixtureExpectation("file_example_PNG_1MB.png", "image/png", "png", me.tamkungz.codecmedia.model.MediaType.IMAGE),
new ExampleFixtureExpectation("file_example_PNG_500kB.png", "image/png", "png", me.tamkungz.codecmedia.model.MediaType.IMAGE),
new ExampleFixtureExpectation("file_example_WEBM_480_900KB.webm", "video/webm", "webm", me.tamkungz.codecmedia.model.MediaType.VIDEO),
new ExampleFixtureExpectation("file_example_WEBM_640_1_4MB.webm", "video/webm", "webm", me.tamkungz.codecmedia.model.MediaType.VIDEO)
);
}
}