E3: conversione delle foto, rimux in MP4 e fotogrammi esatti nel nucleo

Prima parte della tappa E3 (formati e fotogrammi), solo nel nucleo: il
pianificatore, il copiatore e l'archivio delle esportazioni restano alla
parte successiva.

- Foto: ImageConverter.Check dice prima che cosa succede (copia identica,
  conversione, copia così com'è con il motivo: HEIC, AVIF, raw, animazioni,
  file danneggiati, CMYK e oltre 8 bit verso PNG/WEBP, oltre il limite di
  pixel); la conversione riporta come note ciò che non può portare invece
  di fermarsi, e la trasparenza verso JPG diventa bianco. I lettori
  segnalano animazioni, CMYK e segmenti non letti. VP8L usa un logaritmo
  in virgola fissa, così lo stesso file dà gli stessi byte su ogni sistema.
- Video: i lettori ISO e Matroska leggono elenco dei montaggi, lingue,
  tipo di AAC, MP3 e i vecchi codici AAC di Matroska; Mp4Writer prepara
  la disposizione e ne conosce la lunghezza prima di scrivere; il nuovo
  Mp4Remuxer porta in MP4 senza ricodificare, sempre con gli stessi byte.
- Fotogrammi: VideoFrameReader trova il campione mostrato a un istante
  come lo mostra il lettore, lo decodifica esatto a piena risoluzione e lo
  raddrizza; ExportFrames calcola istanti, suffissi e etichette.
- Nomi: il suffisso del fotogramma non si accorcia mai e la numerazione
  dei nomi liberi viene dopo di lui; SafeFileWriter scrive anche contenuti
  prodotti mentre li scrive.
- MediaConverter e ConversionThrottle preparano una conversione senza
  sapere dove andrà, una alla volta in tutto il processo.

Nuove prove per determinismo, istanti, rotazione, rimux e nomi; tutte le
prove di Febe.Tests passano.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-27 22:30:23 +01:00
1 parent 7faf92e667
commit 704c1ed9cf
28 files changed
+2830 -144

No files matched your search

@@ -0,0 +1,122 @@
using System.Security.Cryptography;
using Febe.Core.Codecs.Images;
using Febe.Core.Domain;
using Febe.Core.Export;
using Febe.Core.Library;
using Febe.Core.Library.Conversion;
using Febe.Tests.Codecs.Containers;
namespace Febe.Tests.Export;
/// <summary>
/// Instants, suffixes and labels of frames (tappa E3, D12 and D13), the protected suffix of the shortening and of the
/// numbered names (WP1.8), and the environment-free media conversion (WP1.6b).
/// </summary>
[TestClass]
public sealed class ExportFrameNamesTests
{
[TestMethod]
public void EveryKSecondsStartsAtZeroAndStopsBeforeTheEnd()
{
var instants = ExportFrames.Every(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(500));
CollectionAssert.AreEqual(
new[] { 0, 500, 1000, 1500 }.Select(ms => TimeSpan.FromMilliseconds(ms)).ToArray(),
instants.ToArray());
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => ExportFrames.Every(TimeSpan.FromSeconds(2), TimeSpan.FromMilliseconds(50)));
var over = Assert.ThrowsExactly<ArgumentException>(() => ExportFrames.Every(TimeSpan.FromHours(1), TimeSpan.FromSeconds(1)));
StringAssert.StartsWith(over.Message, ExportFrames.OverCapMessage);
}
[TestMethod]
public void NInAllAreCentred()
{
var instants = ExportFrames.Spread(TimeSpan.FromSeconds(4), 4);
CollectionAssert.AreEqual(
new[] { 500, 1500, 2500, 3500 }.Select(ms => TimeSpan.FromMilliseconds(ms)).ToArray(),
instants.ToArray());
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => ExportFrames.Spread(TimeSpan.FromSeconds(4), 1001));
}
[TestMethod]
public void SuffixesAndLabels()
{
var actual = TimeSpan.FromTicks((12_340 * TimeSpan.TicksPerMillisecond) - 3); // rounded up to 12.340 s
Assert.AreEqual("_t0m12s340", ExportFrames.Suffix(FrameMode.At, actual, 0, 1));
Assert.AreEqual("_t1m05s000", ExportFrames.Suffix(FrameMode.Every, TimeSpan.FromSeconds(65), 3, 9));
Assert.AreEqual("_f001", ExportFrames.Suffix(FrameMode.Count, actual, 0, 12));
Assert.AreEqual("_f0042", ExportFrames.Suffix(FrameMode.Count, actual, 41, 1000));
Assert.AreEqual("0:01", ExportFrames.Label(TimeSpan.FromSeconds(1)));
Assert.AreEqual("0:01,25", ExportFrames.Label(TimeSpan.FromMilliseconds(1250)));
Assert.AreEqual("2:03,007", ExportFrames.Label(TimeSpan.FromMilliseconds(123_007)));
}
[TestMethod]
public void AKeptSuffixIsNotCutAndNumberingComesAfterIt()
{
var root = "/destinazione/" + new string('d', 120);
var name = new string('n', 200) + "_t0m12s340.png";
var fitted = WindowsFileNames.FitPath(root, ["post", name], keepSuffix: "_t0m12s340");
Assert.EndsWith("_t0m12s340.png", fitted[^1]);
Assert.IsLessThanOrEqualTo(WindowsFileNames.MaxPathLength, root.Length + fitted.Sum(s => s.Length + 1));
CollectionAssert.AreEqual(
WindowsFileNames.FitPath(root, ["post", name]).ToArray(),
WindowsFileNames.FitPath(root, ["post", name], keepSuffix: null).ToArray(),
"Senza suffisso nulla cambia.");
var numbered = ExportPaths.NumberedName(root, "post/" + name, 2, keepSuffix: "_t0m12s340");
Assert.EndsWith("_t0m12s340 (2).png", numbered);
Assert.AreEqual("post/a_f001 (3).png", ExportPaths.NumberedName("/d", "post/a_f001.png", 3, keepSuffix: "_f001"));
}
[TestMethod]
public async Task APhotoIsConvertedWithItsShaAndAChangedSourceIsRefused()
{
using var picture = Imaging.TestImages.Gradient(40, 30, seed: 5);
var png = ImageConverter.Encode(picture, ImageFormat.Png, null);
var sha = Convert.ToHexStringLower(SHA256.HashData(png));
using var jpg = await MediaConverter.PrepareAsync(() => new MemoryStream(png), "a/b.png", MediaKind.Photo,
ExportFormat.Jpg, sha, new ConversionThrottle(), CancellationToken.None);
using var output = new MemoryStream();
await jpg.Produce(output, CancellationToken.None);
Assert.AreEqual(ExportVariant.Converted, jpg.Variant);
Assert.AreEqual(".jpg", jpg.Extension);
Assert.AreEqual(jpg.Length, output.Length);
Assert.AreEqual(jpg.Sha256, Convert.ToHexStringLower(SHA256.HashData(output.ToArray())));
CollectionAssert.Contains(jpg.Notes.ToList(), ImageConversionLosses.TransparencyWhite);
using var same = await MediaConverter.PrepareAsync(() => new MemoryStream(png), "a/b.png", MediaKind.Photo,
ExportFormat.Png, sha, ConversionThrottle.Shared, CancellationToken.None);
Assert.AreEqual((ExportVariant.Whole, ExportFormat.Original, sha, ".png"), (same.Variant, same.Format, same.Sha256, same.Extension));
await Assert.ThrowsExactlyAsync<ContentChangedException>(() => MediaConverter.PrepareAsync(() => new MemoryStream(png),
"a/b.png", MediaKind.Photo, ExportFormat.Jpg, new string('0', 64), ConversionThrottle.Shared, CancellationToken.None));
}
[TestMethod]
public async Task AVideoIsRemuxedOrCopiedWithTheReason()
{
var es = ElementaryStream.Load("BA1_Sony_D");
var mkv = TestVideos.Matroska(es);
var sha = Convert.ToHexStringLower(SHA256.HashData(mkv));
using var remux = await MediaConverter.PrepareAsync(() => new MemoryStream(mkv), "v.mkv", MediaKind.Video,
ExportFormat.Mp4, sha, ConversionThrottle.Shared, CancellationToken.None);
using var output = new MemoryStream();
await remux.Produce(output, CancellationToken.None);
Assert.AreEqual((ExportVariant.Converted, ".mp4", (string?)null), (remux.Variant, remux.Extension, remux.Sha256));
Assert.AreEqual(remux.Length, output.Length);
var garbage = new byte[2048];
using var asIs = await MediaConverter.PrepareAsync(() => new MemoryStream(garbage), "v.mov", MediaKind.Video,
ExportFormat.Mp4, sha, ConversionThrottle.Shared, CancellationToken.None);
Assert.AreEqual(ExportVariant.Whole, asIs.Variant);
Assert.HasCount(1, asIs.Notes);
StringAssert.StartsWith(asIs.Notes[0], MediaConversionResult.AsIsNotRemuxable + ":");
}
}