E4: video_analysis con istante del fotogramma e nota (migrazione 0005)
«Salva il fotogramma» deve salvare il fotogramma scelto dall'analisi (il più nitido, mai uno nero) e il visualizzatore deve dire perché un video è «Non analizzabile». La migrazione 0005 aggiunge a video_analysis frame_at_ms e note, con i CHECK nello schema: un «Fermo» ha sempre il suo istante e nessun altro esito ne ha uno, un «Non analizzabile» ha sempre la nota. Un indice parziale sugli esiti fermi serve al filtro «Video fermi». Le righe impossibili (scritte a mano: nessun Febe prima di E4 scrive la tabella) si tolgono prima, così la migrazione non si ferma. VideoAnalysis ha FrameAt e Note; VideoAnalysisNotes elenca i codici, compreso SingleColor per il video tutto di un colore (risposta 42 di Alberto: tutto nero o di un colore è «Non analizzabile»). L'archivio legge e scrive le colonne nuove e rifiuta le forme che lo schema rifiuta, e una nota che non sia fatta di codici. Prove: versione dello schema 5, aggiornamento dalla 4, la 0005 cambia solo video_analysis, vincoli e archivio. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b3c15da821
commit
0cd842a518
8 files changed
+278
-20
No files matched your search
@@ -343,8 +343,47 @@ public sealed record ExportItem
|
||||
public string? LastError { get; init; }
|
||||
}
|
||||
|
||||
/// <summary>Outcome of the still-video analysis of one algorithm for one SHA-256 (spec §8.3 <c>video_analysis</c>).</summary>
|
||||
public sealed record VideoAnalysis(string Sha256, int Algorithm, VideoAnalysisOutcome Outcome, DateTimeOffset AnalyzedAt);
|
||||
/// <summary>
|
||||
/// Outcome of the still-video analysis of one algorithm for one SHA-256 (spec §8.3 <c>video_analysis</c>, migration
|
||||
/// 0005 of tappa E4). <paramref name="FrameAt"/> is the shown instant of the frame «Salva il fotogramma» saves: set for
|
||||
/// <see cref="VideoAnalysisOutcome.Still"/> and only for it. <paramref name="Note"/> says why, as codes of
|
||||
/// <see cref="VideoAnalysisNotes"/> separated by spaces: always set for <see cref="VideoAnalysisOutcome.NotAnalyzable"/>.
|
||||
/// </summary>
|
||||
public sealed record VideoAnalysis(
|
||||
string Sha256,
|
||||
int Algorithm,
|
||||
VideoAnalysisOutcome Outcome,
|
||||
DateTimeOffset AnalyzedAt,
|
||||
TimeSpan? FrameAt = null,
|
||||
string? Note = null);
|
||||
|
||||
/// <summary>
|
||||
/// The codes of <c>video_analysis.note</c> (tappa E4), English, separated by spaces like <c>export_item.note</c>. They
|
||||
/// say why a video is «Non analizzabile».
|
||||
/// </summary>
|
||||
public static class VideoAnalysisNotes
|
||||
{
|
||||
/// <summary>The video has no frames Febe can decode (codec, profile or container outside spec §3, damaged headers).</summary>
|
||||
public const string FrameNotAvailable = "FrameNotAvailable";
|
||||
|
||||
/// <summary>The video has more than one frame and no known duration.</summary>
|
||||
public const string DurationUnknown = "DurationUnknown";
|
||||
|
||||
/// <summary>The frames of the video cannot be read (a damaged or truncated file).</summary>
|
||||
public const string Unreadable = "Unreadable";
|
||||
|
||||
/// <summary>The analysis took longer than its time limit (only an analysis from a selection stores it).</summary>
|
||||
public const string TooSlow = "TooSlow";
|
||||
|
||||
/// <summary>Every frame looked at is black or nearly black (answer 42 of Alberto, 29 November 2026).</summary>
|
||||
public const string AllDark = "AllDark";
|
||||
|
||||
/// <summary>Every frame looked at is all of one single colour, not dark (answer 42 of Alberto, 29 November 2026).</summary>
|
||||
public const string SingleColor = "SingleColor";
|
||||
|
||||
/// <summary>Every code of algorithm 1, in the order above.</summary>
|
||||
public static IReadOnlyList<string> All { get; } = [FrameNotAvailable, DurationUnknown, Unreadable, TooSlow, AllDark, SingleColor];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A converted file added to an entry by the conversion for the library (spec §4.10, §8.3 <c>media_conversion</c>),
|
||||
|
||||
@@ -4,7 +4,8 @@ namespace Febe.Core.Storage;
|
||||
|
||||
/// <summary>
|
||||
/// Outcomes of «È un video fermo?» by SHA-256 of the video and number of the algorithm (spec §4.10, §8.3
|
||||
/// <c>video_analysis</c>). No link to the library: an outcome outlives deletion and re-import of the same file.
|
||||
/// <c>video_analysis</c>, columns <c>frame_at_ms</c> and <c>note</c> from migration 0005). No link to the library: an
|
||||
/// outcome outlives deletion and re-import of the same file.
|
||||
/// </summary>
|
||||
public interface IVideoAnalysisStore
|
||||
{
|
||||
@@ -14,6 +15,11 @@ public interface IVideoAnalysisStore
|
||||
/// <summary>The analysis with the highest algorithm number for each SHA-256 that has one, keyed by lower-case SHA-256.</summary>
|
||||
Task<IReadOnlyDictionary<string, VideoAnalysis>> GetManyAsync(IReadOnlyCollection<string> sha256s, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Stores (or replaces) the outcome of one algorithm for one SHA-256.</summary>
|
||||
/// <summary>
|
||||
/// Stores (or replaces) the outcome of one algorithm for one SHA-256, with the instant of its frame and its note.
|
||||
/// Throws <see cref="ArgumentException"/> for a still video without an instant (or before the start), an instant on
|
||||
/// another outcome, a note that is not codes separated by spaces, or a «Non analizzabile» without a note. The instant
|
||||
/// is kept in whole milliseconds.
|
||||
/// </summary>
|
||||
Task SetAsync(VideoAnalysis analysis, CancellationToken ct = default);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
-- Febe, schema version 5: «Video fermi» (tappa E4 of 0.3.0, issue #58; spec §4.10, §8.3).
|
||||
-- Run by the migration runner inside a transaction; PRAGMA user_version is set by the runner.
|
||||
-- Two columns and one index on video_analysis: no other table, column or index changes, and no data of another table.
|
||||
|
||||
-- No version of Febe before tappa E4 wrote video_analysis, so the table is empty here. SQLite tests the CHECKs of a new
|
||||
-- column against the rows already there: should a row be there anyway (written by hand), a «Fermo» without the instant
|
||||
-- of its frame or a «Non analizzabile» without its reason cannot be kept, and it would stop the migration. Such a row
|
||||
-- goes, and «È un video fermo?» analyses that video again when asked; an «In movimento» stays as it is.
|
||||
DELETE FROM video_analysis WHERE outcome <> 'Moving';
|
||||
|
||||
-- The instant of the frame «Salva il fotogramma» saves, in milliseconds of shown time (as export_copy.frame_at_ms):
|
||||
-- the sharpest frame the analysis looked at, never a dark one of a fade (answer 45 of Alberto, 29 November 2026).
|
||||
-- Set for a «Fermo» outcome and only for it.
|
||||
ALTER TABLE video_analysis ADD COLUMN frame_at_ms INTEGER
|
||||
CHECK (frame_at_ms IS NULL OR frame_at_ms >= 0)
|
||||
CHECK ((outcome = 'Still') = (frame_at_ms IS NOT NULL));
|
||||
|
||||
-- Why the outcome is what it is: English codes separated by spaces, like export_item.note (FrameNotAvailable,
|
||||
-- DurationUnknown, Unreadable, TooSlow, AllDark, SingleColor). Always set for «Non analizzabile», so the viewer can
|
||||
-- say why; a video all black or all of one colour is «Non analizzabile» (answer 42 of Alberto, 29 November 2026).
|
||||
ALTER TABLE video_analysis ADD COLUMN note TEXT
|
||||
CHECK (note IS NULL OR length(note) > 0)
|
||||
CHECK (outcome <> 'NotAnalyzable' OR note IS NOT NULL);
|
||||
|
||||
-- The «Video fermi» filter of the library reads the still outcomes and joins them to the files by SHA-256
|
||||
-- (ix_media_sha256_entry, migration 0004); the primary key (sha256, algorithm) is in the index too.
|
||||
CREATE INDEX ix_video_analysis_still ON video_analysis (sha256) WHERE outcome = 'Still';
|
||||
@@ -8,8 +8,10 @@ internal sealed class VideoAnalysisStore(FebeDatabase db) : IVideoAnalysisStore
|
||||
{
|
||||
private const int KeysPerQuery = 500;
|
||||
|
||||
private const string Columns = "sha256, algorithm, outcome, analyzed_at, frame_at_ms, note";
|
||||
|
||||
private static VideoAnalysis Read(SqliteDataReader r) =>
|
||||
new(r.GetString(0), r.GetInt32(1), r.GetEnum<VideoAnalysisOutcome>(2), r.GetTime(3));
|
||||
new(r.GetString(0), r.GetInt32(1), r.GetEnum<VideoAnalysisOutcome>(2), r.GetTime(3), r.GetDurationOrNull(4), r.GetTextOrNull(5));
|
||||
|
||||
public Task<VideoAnalysis?> GetAsync(string sha256, CancellationToken ct = default)
|
||||
{
|
||||
@@ -17,8 +19,8 @@ internal sealed class VideoAnalysisStore(FebeDatabase db) : IVideoAnalysisStore
|
||||
return db.ReadAsync(
|
||||
c =>
|
||||
{
|
||||
using var r = c.Command("""
|
||||
SELECT sha256, algorithm, outcome, analyzed_at FROM video_analysis WHERE sha256 = @h ORDER BY algorithm DESC LIMIT 1
|
||||
using var r = c.Command($"""
|
||||
SELECT {Columns} FROM video_analysis WHERE sha256 = @h ORDER BY algorithm DESC LIMIT 1
|
||||
""").With("@h", sha).ExecuteReader();
|
||||
return r.Read() ? Read(r) : null;
|
||||
},
|
||||
@@ -36,7 +38,7 @@ internal sealed class VideoAnalysisStore(FebeDatabase db) : IVideoAnalysisStore
|
||||
foreach (var chunk in keys.Chunk(KeysPerQuery))
|
||||
{
|
||||
using var command = c.Command(string.Empty);
|
||||
command.CommandText = $"SELECT sha256, algorithm, outcome, analyzed_at FROM video_analysis WHERE sha256 IN ({command.AddList("h", chunk)})";
|
||||
command.CommandText = $"SELECT {Columns} FROM video_analysis WHERE sha256 IN ({command.AddList("h", chunk)})";
|
||||
using var r = command.ExecuteReader();
|
||||
while (r.Read())
|
||||
{
|
||||
@@ -58,12 +60,55 @@ internal sealed class VideoAnalysisStore(FebeDatabase db) : IVideoAnalysisStore
|
||||
ArgumentNullException.ThrowIfNull(analysis);
|
||||
var sha = ExportValues.Sha(analysis.Sha256, nameof(analysis));
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(analysis.Algorithm, 1, nameof(analysis));
|
||||
ValidateFrame(analysis.Outcome, analysis.FrameAt, nameof(analysis));
|
||||
var note = Note(analysis.Outcome, analysis.Note, nameof(analysis));
|
||||
return db.WriteAsync(
|
||||
w => w.Command("""
|
||||
INSERT INTO video_analysis (sha256, algorithm, outcome, analyzed_at) VALUES (@h, @alg, @o, @at)
|
||||
ON CONFLICT (sha256, algorithm) DO UPDATE SET outcome = excluded.outcome, analyzed_at = excluded.analyzed_at
|
||||
INSERT INTO video_analysis (sha256, algorithm, outcome, analyzed_at, frame_at_ms, note) VALUES (@h, @alg, @o, @at, @fr, @n)
|
||||
ON CONFLICT (sha256, algorithm) DO UPDATE SET
|
||||
outcome = excluded.outcome, analyzed_at = excluded.analyzed_at, frame_at_ms = excluded.frame_at_ms, note = excluded.note
|
||||
""").With("@h", sha).With("@alg", (long)analysis.Algorithm).With("@o", Sql.Enum(analysis.Outcome)).With("@at", Sql.Time(analysis.AnalyzedAt))
|
||||
.With("@fr", Sql.Duration(analysis.FrameAt)).With("@n", (object?)note ?? DBNull.Value)
|
||||
.ExecuteNonQuery(),
|
||||
ct);
|
||||
}
|
||||
|
||||
/// <summary>A still video has the instant of its frame, at or after the start; no other outcome has one.</summary>
|
||||
private static void ValidateFrame(VideoAnalysisOutcome outcome, TimeSpan? frameAt, string name)
|
||||
{
|
||||
if (outcome == VideoAnalysisOutcome.Still)
|
||||
{
|
||||
if (frameAt is not { } f || f < TimeSpan.Zero)
|
||||
{
|
||||
throw new ArgumentException("Un video fermo ha l'istante del suo fotogramma, dall'inizio in poi.", name);
|
||||
}
|
||||
}
|
||||
else if (frameAt is not null)
|
||||
{
|
||||
throw new ArgumentException("Solo un video fermo ha l'istante di un fotogramma.", name);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The note as stored: codes of ASCII letters and digits starting upper case (a «Code:Arg» too) separated by single
|
||||
/// spaces, null for none. Never free text, so no path or message can end up in it; «Non analizzabile» always says why.
|
||||
/// </summary>
|
||||
private static string? Note(VideoAnalysisOutcome outcome, string? note, string name)
|
||||
{
|
||||
var codes = (note ?? string.Empty).Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
foreach (var code in codes)
|
||||
{
|
||||
if (!char.IsAsciiLetterUpper(code[0]) || !code.All(c => char.IsAsciiLetterOrDigit(c) || c == ':'))
|
||||
{
|
||||
throw new ArgumentException("La nota di un'analisi è fatta di codici separati da spazi.", name);
|
||||
}
|
||||
}
|
||||
|
||||
if (codes.Length == 0 && outcome == VideoAnalysisOutcome.NotAnalyzable)
|
||||
{
|
||||
throw new ArgumentException("Un video non analizzabile dice perché nella nota.", name);
|
||||
}
|
||||
|
||||
return codes.Length == 0 ? null : string.Join(' ', codes);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user