WIP: contenitori, sonda dei metadati, fotogrammi e scrittore MP4 in casa; via ffmpeg e ffprobe (#58)
Lettori ISO BMFF (anche frammentato), Matroska/WebM e AVI; ContainerVideoMetadataProbe è la sonda predefinita; VideoFrameDecoder (H.264 in casa, BMP ruotato, riconoscimento dei video fermi) sostituisce VideoFrameExtractor; Mp4Writer con moov in testa. Tolti ffmpeg e ffprobe da Dockerfile, CI, script e prove. WIP perché la corsa completa di tests/Febe.Tests non era finita al momento del commit e l'ultima correzione (data qualsiasi nella lettura dei campioni) non è stata ricompilata né riprovata: la compilazione durante la corsa fallisce per DLL bloccate dalle prove in esecuzione. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
697de97ec3
commit
6ffcb1bb18
39 files changed
+4235
-469
No files matched your search
@@ -0,0 +1,70 @@
|
||||
namespace Febe.Core.Codecs.Containers;
|
||||
|
||||
/// <summary>Recognises the container of a video by its first bytes and reads it with the matching reader.</summary>
|
||||
public static class ContainerReader
|
||||
{
|
||||
/// <summary>The format of a file from its first bytes; null when it is none of those read here.</summary>
|
||||
public static ContainerFormat? Detect(ReadOnlySpan<byte> start)
|
||||
{
|
||||
if (MatroskaReader.Looks(start))
|
||||
{
|
||||
return ContainerFormat.Matroska;
|
||||
}
|
||||
|
||||
if (AviReader.Looks(start))
|
||||
{
|
||||
return ContainerFormat.Avi;
|
||||
}
|
||||
|
||||
return IsoBmffReader.Looks(start) ? ContainerFormat.IsoBmff : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a seekable stream; null when its format is not recognised. Damaged files raise
|
||||
/// <see cref="ContainerException"/> (or <see cref="EndOfStreamException"/> when truncated).
|
||||
/// </summary>
|
||||
public static MediaContainer? Read(Stream stream, DateTimeOffset now, bool samples = true)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(stream);
|
||||
Span<byte> start = stackalloc byte[16];
|
||||
stream.Position = 0;
|
||||
var read = stream.ReadAtLeast(start, start.Length, throwOnEndOfStream: false);
|
||||
stream.Position = 0;
|
||||
return Detect(start[..read]) switch
|
||||
{
|
||||
ContainerFormat.Matroska => MatroskaReader.Read(stream, now, samples),
|
||||
ContainerFormat.Avi => AviReader.Read(stream, now, samples),
|
||||
ContainerFormat.IsoBmff => IsoBmffReader.Read(stream, now, samples),
|
||||
_ => null,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>Reads a file (read-only, shared); null when its format is not recognised.</summary>
|
||||
public static MediaContainer? ReadFile(string path, DateTimeOffset now, bool samples = true)
|
||||
{
|
||||
using var stream = OpenRead(path);
|
||||
return Read(stream, now, samples);
|
||||
}
|
||||
|
||||
/// <summary>Opens a file for reading without blocking other readers or writers.</summary>
|
||||
public static FileStream OpenRead(string path) =>
|
||||
new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, 64 * 1024, FileOptions.RandomAccess);
|
||||
|
||||
/// <summary>The bytes of a sample, read into <paramref name="buffer"/> (grown when needed).</summary>
|
||||
public static ReadOnlySpan<byte> ReadSample(Stream stream, in MediaSample sample, ref byte[] buffer)
|
||||
{
|
||||
if (sample.Size < 0 || sample.Offset < 0 || sample.Offset + sample.Size > stream.Length)
|
||||
{
|
||||
throw new ContainerException("Campione fuori dal file.");
|
||||
}
|
||||
|
||||
if (buffer.Length < sample.Size)
|
||||
{
|
||||
buffer = new byte[Math.Max(sample.Size, buffer.Length * 2)];
|
||||
}
|
||||
|
||||
stream.Position = sample.Offset;
|
||||
stream.ReadExactly(buffer, 0, sample.Size);
|
||||
return buffer.AsSpan(0, sample.Size);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user