yavsc/src/Yavsc.Server/Services/FfmpegPodcastReplayRemuxer.cs
Paul Schneider d29124c527
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>
2026-11-16 14:01:24 +00:00

30 lines
1.3 KiB
C#

using Microsoft.Extensions.Options;
namespace Yavsc.Services;
public sealed class FfmpegPodcastReplayRemuxer(IOptions<PodcastOptions> options) : IPodcastReplayRemuxer
{
public async Task RemuxAsync(string directory, int segmentCount, string outputPath, long maxBytes, CancellationToken ct)
{
if (segmentCount < 1) throw new InvalidDataException("Le podcast ne contient aucun segment.");
var list = Path.Combine(directory, "replay-concat.txt");
// Concatenate TS bytes without resetting timestamps or guessing durations between segments.
// Only generated local basenames enter the protocol, never uploaded metadata.
try
{
await File.WriteAllLinesAsync(list,
Enumerable.Range(0, segmentCount).Select(i => $"{i:D8}.ts"), ct);
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
], directory, outputPath, maxBytes, ct);
}
finally
{
File.Delete(list);
}
}
}