Add frame-accurate server-side replay editing prototype

- ReplayEditPlan turns removed ranges into kept ranges (merge, clamp,
  reject empty, out-of-range, total or excessive removals).
- FfmpegPodcastReplayEditor renders kept ranges with one accurate-seek
  FFmpeg input per range, concat and H.264/AAC re-encoding (faststart).
- Share the FFmpeg runner (FfmpegProcess) with the replay remuxer.
- Tests check exact kept frames, audio/video durations, faststart and
  quota, with and without a start offset.
- Document the server prototype results in doc/video-edition.md.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Paul Schneider 2026-11-16 14:01:24 +00:00
commit d29124c527
Signed by: notazof
GPG key ID: AD4B9B545FBD7D12
8 changed files with 258 additions and 55 deletions

View file

@ -1,4 +1,3 @@
using System.Diagnostics;
using Microsoft.Extensions.Options;
namespace Yavsc.Services;
@ -15,64 +14,17 @@ public sealed class FfmpegPodcastReplayRemuxer(IOptions<PodcastOptions> options)
{
await File.WriteAllLinesAsync(list,
Enumerable.Range(0, segmentCount).Select(i => $"{i:D8}.ts"), ct);
var start = new ProcessStartInfo(options.Value.FfmpegPath)
{
UseShellExecute = false,
RedirectStandardError = true,
CreateNoWindow = true,
WorkingDirectory = directory
};
foreach (var argument in new[]
{
await FfmpegProcess.RunAsync(options.Value.FfmpegPath,
[
"-nostdin", "-hide_banner", "-loglevel", "error", "-y",
"-protocol_whitelist", "file,concatf", "-f", "mpegts", "-i", $"concatf:{list}",
"-map", "0:v:0", "-map", "0:a:0?", "-c", "copy", "-bsf:a", "aac_adtstoasc",
"-movflags", "+faststart", "-f", "mp4", outputPath
})
start.ArgumentList.Add(argument);
using var process = Process.Start(start)
?? throw new IOException("Impossible de demarrer FFmpeg.");
var errors = DrainErrorsAsync(process.StandardError, CancellationToken.None);
try
{
var exited = process.WaitForExitAsync(ct);
// -fs can report success for a truncated replay. Abort instead of publishing partial media.
while (!exited.IsCompleted)
{
await Task.WhenAny(exited, Task.Delay(50, ct));
ct.ThrowIfCancellationRequested();
if (File.Exists(outputPath) && new FileInfo(outputPath).Length > maxBytes)
throw new IOException("Le MP4 depasse le quota de stockage disponible.");
}
await exited;
var error = await errors;
if (process.ExitCode != 0)
throw new InvalidDataException($"FFmpeg a echoue ({process.ExitCode}) : {error}");
var length = new FileInfo(outputPath).Length;
if (length == 0 || length > maxBytes)
throw new IOException("Le MP4 depasse le quota de stockage disponible ou est vide.");
}
catch
{
if (!process.HasExited) process.Kill(entireProcessTree: true);
await process.WaitForExitAsync(CancellationToken.None);
await errors;
throw;
}
], directory, outputPath, maxBytes, ct);
}
finally
{
File.Delete(list);
}
}
private static async Task<string> DrainErrorsAsync(StreamReader reader, CancellationToken ct)
{
var text = new System.Text.StringBuilder();
var buffer = new char[1024];
int count;
while ((count = await reader.ReadAsync(buffer.AsMemory(), ct)) != 0)
if (text.Length < 4096) text.Append(buffer, 0, Math.Min(count, 4096 - text.Length));
return text.ToString();
}
}