E3: correzioni della revisione di formati e fotogrammi
Le 16 osservazioni confermate della revisione, ognuna con la sua prova: - JPEG: il controllo stima la memoria di lavoro del decodificatore (progressivo), così un file che non si decodifica esce «troppo grande» e non «illeggibile». - Un EXIF che non si tiene lascia un EXIF minimo con l'orientamento, perché la foto convertita non esca girata. - Fotogrammi: istanti in millisecondi interi; la copia trova l'indice del nome dall'istante, non dall'ordine delle righe. - Video di durata sconosciuta misurati prima di registrare: contano nei limiti di 500 per richiesta e 5000 per video. - Rimuxaggio: la durata del segmento di elst passa nell'MP4, la fine tagliata resta nascosta (anche nei fotogrammi). - H.264: il punto di recupero vale come IDR solo con exact_match_flag. - MP3 in MP4: objectTypeIndication 0x69 fino a 24 kHz, 0x6B oltre. - Conversione della libreria: il lavoro toglie le righe di un formato spento o cambiato; spegnere toglie anche i «Non riuscito»; «Riprova» solo i formati di adesso; pausa per spazio prima di rileggere l'originale. - «Anche i N» conta e mette in coda solo il tipo che si cambia; le impostazioni dell'esportazione si salvano una per volta. - Eliminazione e «Tieni il più grande» scelgono i convertiti nella stessa transazione: nessun convertito orfano. - Un file arrivato tardi in un elemento vecchio mette in coda solo sé (file nuovo = salvato da quando il formato è scelto). - Zip dei fotogrammi a pezzi di 64 KB: un browser lento che legge non si interrompe, uno fermo sì. Decisioni in docs/decisioni.md (28/11), §4.10 della specifica precisato. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
eb79b05fda
commit
958cb71cd6
38 files changed
+1276
-222
No files matched your search
@@ -603,13 +603,26 @@ public sealed class ExportCopier(ExportEnvironment env)
|
||||
|
||||
await using (stream.ConfigureAwait(false))
|
||||
{
|
||||
var (reader, info) = await Task.Run(
|
||||
var knownLength = library.Media.Duration;
|
||||
var (reader, info, length) = await Task.Run(
|
||||
() =>
|
||||
{
|
||||
// «N in tutto» names a frame by its position among the N instants, from the length the job used
|
||||
// (the library's, or the one read from the file when the library has none: ExportService).
|
||||
var probed = knownLength;
|
||||
if (mode == FrameMode.Count && probed is null)
|
||||
{
|
||||
probed = VideoFrameReader.ProbeDuration(stream);
|
||||
stream.Position = 0;
|
||||
}
|
||||
|
||||
var opened = VideoFrameReader.Open(stream, out var found);
|
||||
return (opened, found);
|
||||
return (opened, found, probed);
|
||||
},
|
||||
ct).ConfigureAwait(false);
|
||||
IReadOnlyList<TimeSpan> spread = mode == FrameMode.Count && length is { } d && d > TimeSpan.Zero && count is >= 1 and <= ExportFrames.MaxCount
|
||||
? ExportFrames.Spread(d, count)
|
||||
: [];
|
||||
using (reader)
|
||||
{
|
||||
var targets = info.Available ? reader.Locate([.. batch.Select(i => i.FrameAt ?? TimeSpan.Zero)]) : [];
|
||||
@@ -637,7 +650,11 @@ public sealed class ExportCopier(ExportEnvironment env)
|
||||
followers[k] = [];
|
||||
var at = TimeSpan.FromMilliseconds(ExportFrames.ActualMs(target.Actual));
|
||||
var identity = new ExportCopyIdentity(destination.Id, library.Account.Id, sha, ExportVariant.Frame, format, at);
|
||||
var index = mode == FrameMode.Count ? (int)Math.Clamp(batch[k].Id - firstId, 0, int.MaxValue) : k;
|
||||
// The position among the N instants (two on one millisecond are one item: ids skip nothing then);
|
||||
// by the ids only when the instant is not one of them (the library's length changed meanwhile).
|
||||
var index = mode == FrameMode.Count
|
||||
? ExportFrames.IndexOf(spread, batch[k].FrameAt ?? TimeSpan.Zero) ?? (int)Math.Clamp(batch[k].Id - firstId, 0, int.MaxValue)
|
||||
: k;
|
||||
var plan = new FramePlan(identity, ExportFrames.Suffix(mode, target.Actual, index, count), null);
|
||||
var current = await exports.FindCurrentCopyAsync(identity, ct).ConfigureAwait(false);
|
||||
if (current is { State: ExportCopyState.Copied })
|
||||
|
||||
@@ -72,13 +72,16 @@ public static class ExportFrames
|
||||
var instants = new TimeSpan[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
instants[i] = TimeSpan.FromTicks(step.Ticks * i);
|
||||
instants[i] = WholeMs(TimeSpan.FromTicks(step.Ticks * i));
|
||||
}
|
||||
|
||||
return instants;
|
||||
}
|
||||
|
||||
/// <summary>«N in tutto»: (i + ½) · duration / N for i = 0…N − 1, in 100 ns ticks rounded down.</summary>
|
||||
/// <summary>
|
||||
/// «N in tutto»: (i + ½) · duration / N for i = 0…N − 1, rounded up to the millisecond: the item, the preview and
|
||||
/// the download locate the same instant. On a video shorter than N milliseconds neighbours can share one.
|
||||
/// </summary>
|
||||
public static IReadOnlyList<TimeSpan> Spread(TimeSpan duration, int count)
|
||||
{
|
||||
if (count is < 1 or > MaxCount)
|
||||
@@ -90,7 +93,7 @@ public static class ExportFrames
|
||||
var instants = new TimeSpan[count];
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
instants[i] = TimeSpan.FromTicks((long)(((2 * i) + 1) * length / (2 * (Int128)count)));
|
||||
instants[i] = WholeMs(TimeSpan.FromTicks((long)(((2 * i) + 1) * length / (2 * (Int128)count))));
|
||||
}
|
||||
|
||||
return instants;
|
||||
@@ -128,6 +131,28 @@ public static class ExportFrames
|
||||
: label + "," + fraction.ToString("D3", CultureInfo.InvariantCulture).TrimEnd('0');
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The position of <paramref name="requested"/> among <paramref name="instants"/> (the first of those on the same
|
||||
/// millisecond, as the items keep them), or null when it is not one of them.
|
||||
/// </summary>
|
||||
public static int? IndexOf(IReadOnlyList<TimeSpan> instants, TimeSpan requested)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(instants);
|
||||
var ms = (long)requested.TotalMilliseconds;
|
||||
for (var i = 0; i < instants.Count; i++)
|
||||
{
|
||||
if ((long)instants[i].TotalMilliseconds == ms)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>An instant rounded up to the whole millisecond (never before the instant computed).</summary>
|
||||
private static TimeSpan WholeMs(TimeSpan at) => TimeSpan.FromMilliseconds(ActualMs(at));
|
||||
|
||||
/// <summary>The identity of a frame's instant: its shown time in milliseconds, rounded up.</summary>
|
||||
public static long ActualMs(TimeSpan shown) =>
|
||||
shown <= TimeSpan.Zero ? 0 : (shown.Ticks + TimeSpan.TicksPerMillisecond - 1) / TimeSpan.TicksPerMillisecond;
|
||||
|
||||
@@ -116,15 +116,20 @@ public sealed class ExportPlanner(ExportEnvironment env)
|
||||
/// once (a story and its highlight entry, two files with the same bytes); with «Una copia sola» the same file counts
|
||||
/// once across accounts.
|
||||
/// </summary>
|
||||
/// <param name="lengthOf">
|
||||
/// Frames job: the length of a video the library does not know, read from its file (null when unreadable); without
|
||||
/// it such a video is one planned file without instants (<see cref="PlannedFile.UnknownDuration"/>, the dialog).
|
||||
/// </param>
|
||||
internal async IAsyncEnumerable<PlannedFile> EnumerateAsync(
|
||||
ExportJobRequest request,
|
||||
IReadOnlyDictionary<long, EffectiveDestination> effective,
|
||||
ExportSettings settings,
|
||||
[EnumeratorCancellation] CancellationToken ct)
|
||||
[EnumeratorCancellation] CancellationToken ct,
|
||||
Func<ExportCandidate, CancellationToken, Task<TimeSpan?>>? lengthOf = null)
|
||||
{
|
||||
if (request.Frames is { } frames)
|
||||
{
|
||||
await foreach (var frame in EnumerateFramesAsync(request, frames, effective, ct).ConfigureAwait(false))
|
||||
await foreach (var frame in EnumerateFramesAsync(request, frames, effective, lengthOf, ct).ConfigureAwait(false))
|
||||
{
|
||||
yield return frame;
|
||||
}
|
||||
@@ -237,18 +242,21 @@ public sealed class ExportPlanner(ExportEnvironment env)
|
||||
|
||||
/// <summary>
|
||||
/// A frames job (D12, D14 of tappa E3): photos are skipped, videos not due are counted as in «Copia in…», each due
|
||||
/// video gives one planned frame per instant of the request. A video of unknown length gives one planned file
|
||||
/// without an instant (<see cref="PlannedFile.UnknownDuration"/>): its instants are found when the job is recorded.
|
||||
/// Frames are not checked for «già copiato» here: the copy looks at each one's exact instant.
|
||||
/// video gives one planned frame per instant of the request. A video of unknown length is measured by
|
||||
/// <paramref name="lengthOf"/> (the job: before anything is recorded, so that its frames count against the caps of
|
||||
/// D12) or, without it, gives one planned file without an instant (<see cref="PlannedFile.UnknownDuration"/>, the
|
||||
/// dialog). One that cannot be read gets one instant, and the copy says «Fotogramma non disponibile per questo
|
||||
/// video». Frames are not checked for «già copiato» here: the copy looks at each one's exact instant.
|
||||
/// </summary>
|
||||
private async IAsyncEnumerable<PlannedFile> EnumerateFramesAsync(
|
||||
ExportJobRequest request,
|
||||
ExportFramesRequest frames,
|
||||
IReadOnlyDictionary<long, EffectiveDestination> effective,
|
||||
Func<ExportCandidate, CancellationToken, Task<TimeSpan?>>? lengthOf,
|
||||
[EnumeratorCancellation] CancellationToken ct)
|
||||
{
|
||||
var exports = _env.Storage.Exports;
|
||||
var seen = new HashSet<(long Destination, long Account, string Sha256, long Ticks, ExportFormat Format)>();
|
||||
var seen = new HashSet<(long Destination, long Account, string Sha256, long Ms, ExportFormat Format)>();
|
||||
long after = 0;
|
||||
while (true)
|
||||
{
|
||||
@@ -286,13 +294,27 @@ public sealed class ExportPlanner(ExportEnvironment env)
|
||||
Prediction = ExportPrediction.Converted,
|
||||
FirstOfVideo = true,
|
||||
};
|
||||
if (frames.Mode != FrameMode.At && c.Duration is not { } known)
|
||||
var duration = c.Duration ?? TimeSpan.Zero;
|
||||
if (frames.Mode != FrameMode.At && c.Duration is null)
|
||||
{
|
||||
yield return frame with { UnknownDuration = true };
|
||||
continue;
|
||||
if (lengthOf is null)
|
||||
{
|
||||
yield return frame with { UnknownDuration = true };
|
||||
continue;
|
||||
}
|
||||
|
||||
if (await lengthOf(c, ct).ConfigureAwait(false) is not { } measured || measured <= TimeSpan.Zero)
|
||||
{
|
||||
var unreadable = frame with { FrameAt = TimeSpan.Zero };
|
||||
yield return seen.Add((destination, c.AccountId, c.Sha256!, 0, frames.Format))
|
||||
? unreadable
|
||||
: unreadable with { Action = PlannedCopy.AlreadyCopied };
|
||||
continue;
|
||||
}
|
||||
|
||||
duration = measured;
|
||||
}
|
||||
|
||||
var duration = c.Duration ?? TimeSpan.Zero;
|
||||
if (ExportFrames.FrameCount(duration, frames) > ExportFrames.MaxPerVideo)
|
||||
{
|
||||
yield return frame with { Action = PlannedCopy.OverCap };
|
||||
@@ -304,7 +326,8 @@ public sealed class ExportPlanner(ExportEnvironment env)
|
||||
{
|
||||
var planned = frame with { FrameAt = at, FirstOfVideo = first };
|
||||
first = false;
|
||||
if (!seen.Add((destination, c.AccountId, c.Sha256!, at.Ticks, frames.Format)))
|
||||
// By the millisecond the item keeps (ux_export_item_file): two instants on one are one item.
|
||||
if (!seen.Add((destination, c.AccountId, c.Sha256!, (long)at.TotalMilliseconds, frames.Format)))
|
||||
{
|
||||
planned = planned with { Action = PlannedCopy.AlreadyCopied };
|
||||
}
|
||||
|
||||
@@ -167,11 +167,26 @@ public sealed class ExportService : IAsyncDisposable
|
||||
var runs = new Dictionary<long, ExportRun>();
|
||||
var pending = new List<ExportItem>();
|
||||
var exports = _env.Storage.Exports;
|
||||
// Frames job: the videos whose length the library does not know are measured once, in the first pass, so that
|
||||
// their frames count against the caps; the recording pass reuses the lengths (they cannot disagree).
|
||||
var lengths = new Dictionary<long, TimeSpan?>();
|
||||
Func<ExportCandidate, CancellationToken, Task<TimeSpan?>>? lengthOf = request.Frames is null
|
||||
? null
|
||||
: async (candidate, token) =>
|
||||
{
|
||||
if (!lengths.TryGetValue(candidate.MediaId, out var length))
|
||||
{
|
||||
length = await ProbeLengthAsync(candidate, token).ConfigureAwait(false);
|
||||
lengths[candidate.MediaId] = length;
|
||||
}
|
||||
|
||||
return length;
|
||||
};
|
||||
if (request.Frames is not null)
|
||||
{
|
||||
// Refused before anything is recorded (D12 of tappa E3): a video over 1.000 frames, more than 20.000 in all.
|
||||
var count = new PlanTally();
|
||||
await foreach (var file in Planner.EnumerateAsync(request, effective, settings, ct).ConfigureAwait(false))
|
||||
await foreach (var file in Planner.EnumerateAsync(request, effective, settings, ct, lengthOf).ConfigureAwait(false))
|
||||
{
|
||||
count.Add(file);
|
||||
}
|
||||
@@ -187,9 +202,10 @@ public sealed class ExportService : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
var frameItems = 0;
|
||||
try
|
||||
{
|
||||
await foreach (var file in Planner.EnumerateAsync(request, effective, settings, ct).ConfigureAwait(false))
|
||||
await foreach (var file in Planner.EnumerateAsync(request, effective, settings, ct, lengthOf).ConfigureAwait(false))
|
||||
{
|
||||
tally.Add(file);
|
||||
if (file.DestinationId is not { } destination || file.Action is PlannedCopy.Missing or PlannedCopy.NotExported or PlannedCopy.PhotoSkipped or PlannedCopy.OverCap)
|
||||
@@ -207,24 +223,24 @@ public sealed class ExportService : IAsyncDisposable
|
||||
}
|
||||
|
||||
// Frame items of one video are added together, in instant order (the copy takes them as one batch).
|
||||
IReadOnlyList<TimeSpan?> instants = file.UnknownDuration && request.Frames is { } frames
|
||||
? await FrameInstantsAsync(file.Candidate, frames, ct).ConfigureAwait(false)
|
||||
: [file.FrameAt];
|
||||
foreach (var at in instants)
|
||||
// The first pass already refused more than 20.000: this only guards that it saw the same files.
|
||||
if (file.Variant == ExportVariant.Frame && file.Action == PlannedCopy.Write && ++frameItems > ExportFrames.MaxPerRequest)
|
||||
{
|
||||
pending.Add(new ExportItem
|
||||
{
|
||||
RunId = run.Id,
|
||||
MediaId = file.Candidate.MediaId,
|
||||
AccountId = file.Candidate.AccountId,
|
||||
SourceSha256 = file.Candidate.Sha256,
|
||||
Variant = file.Variant,
|
||||
Format = file.Format,
|
||||
FrameAt = at,
|
||||
State = ExportItemState.Pending,
|
||||
});
|
||||
throw new ArgumentException(TooManyFramesText, nameof(request));
|
||||
}
|
||||
|
||||
pending.Add(new ExportItem
|
||||
{
|
||||
RunId = run.Id,
|
||||
MediaId = file.Candidate.MediaId,
|
||||
AccountId = file.Candidate.AccountId,
|
||||
SourceSha256 = file.Candidate.Sha256,
|
||||
Variant = file.Variant,
|
||||
Format = file.Format,
|
||||
FrameAt = file.FrameAt,
|
||||
State = ExportItemState.Pending,
|
||||
});
|
||||
|
||||
if (pending.Count >= ItemBatch)
|
||||
{
|
||||
await exports.AddItemsAsync(pending, CancellationToken.None).ConfigureAwait(false);
|
||||
@@ -299,44 +315,32 @@ public sealed class ExportService : IAsyncDisposable
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The instants of a video whose length the library does not know: its headers are read now (only the library
|
||||
/// file, never the destination). A video that cannot be read gets one instant, and the copy says «Fotogramma non
|
||||
/// disponibile per questo video».
|
||||
/// The length of a video the library does not know, from its headers (only the library file, never the
|
||||
/// destination); null when it cannot be read now (the job then gives it one instant, and the copy tells).
|
||||
/// </summary>
|
||||
private async Task<IReadOnlyList<TimeSpan?>> FrameInstantsAsync(ExportCandidate candidate, ExportFramesRequest frames, CancellationToken ct)
|
||||
private async Task<TimeSpan?> ProbeLengthAsync(ExportCandidate candidate, CancellationToken ct)
|
||||
{
|
||||
TimeSpan? duration = null;
|
||||
var media = await _env.Storage.Media.GetAsync(candidate.MediaId, ct).ConfigureAwait(false);
|
||||
var account = await _env.Storage.Accounts.GetAsync(candidate.AccountId, ct).ConfigureAwait(false);
|
||||
if (media is not null && account is not null && _env.ResolveSourcePath(account, media) is { } path)
|
||||
if (media is null || account is null || _env.ResolveSourcePath(account, media) is not { } path)
|
||||
{
|
||||
try
|
||||
{
|
||||
duration = await Task.Run(
|
||||
() =>
|
||||
{
|
||||
using var stream = _env.FileSystem.OpenRandomAccess(path);
|
||||
return VideoFrameReader.ProbeDuration(stream);
|
||||
},
|
||||
ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
// Not readable now: one instant, the copy tells.
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (duration is not { } d || d <= TimeSpan.Zero)
|
||||
try
|
||||
{
|
||||
return [TimeSpan.Zero];
|
||||
return await Task.Run(
|
||||
() =>
|
||||
{
|
||||
using var stream = _env.FileSystem.OpenRandomAccess(path);
|
||||
return VideoFrameReader.ProbeDuration(stream);
|
||||
},
|
||||
ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
if (ExportFrames.FrameCount(d, frames) > ExportFrames.MaxPerVideo)
|
||||
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||
{
|
||||
throw new ArgumentException(ExportFrames.OverCapMessage, nameof(frames));
|
||||
return null; // not readable now: one instant, the copy tells
|
||||
}
|
||||
|
||||
return [.. ExportFrames.Instants(d, frames).Select(at => (TimeSpan?)at)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in new issue
Block a user