Video trash moves across volumes commit once the verified copy has its name and leave an undeletable source whole behind a .leftover marker that the next start settles; deletes rehash under the lease. Restores honour the names renders are writing, and encoders never overwrite an output. Routing floors at the stored settings' format, kindless stills read as stills, star trails restart per loop, and settings the encoder ignored become render warnings. The Videos section fits beside the player, plays downloaded server copies, names blocked videos and says why actions are off; the admin panel offers no download for sequences or missing files and names a filtered sequence from its record. Refs #21 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
161 lines
4.9 KiB
C#
161 lines
4.9 KiB
C#
using Chronos.Core.Imaging;
|
||
|
||
namespace Chronos.Core.Video;
|
||
|
||
public enum VideoCodec
|
||
{
|
||
H264,
|
||
Hevc,
|
||
Av1,
|
||
Mjpeg,
|
||
JpegSequence,
|
||
PngSequence,
|
||
Tiff16Sequence,
|
||
LinearDngSequence,
|
||
Gif,
|
||
Apng,
|
||
}
|
||
|
||
public enum ContainerFormat
|
||
{
|
||
Mp4,
|
||
Avi,
|
||
ImageSequence,
|
||
Gif,
|
||
Apng,
|
||
}
|
||
|
||
public enum RateControl
|
||
{
|
||
Vbr,
|
||
Cbr,
|
||
Quality,
|
||
}
|
||
|
||
public enum CodecProfile
|
||
{
|
||
Auto,
|
||
H264Baseline,
|
||
H264Main,
|
||
H264High,
|
||
HevcMain,
|
||
HevcMain10,
|
||
Av1Main,
|
||
}
|
||
|
||
public static class VideoCodecInfo
|
||
{
|
||
public static ContainerFormat DefaultContainer(VideoCodec codec) => codec switch
|
||
{
|
||
VideoCodec.H264 or VideoCodec.Hevc or VideoCodec.Av1 => ContainerFormat.Mp4,
|
||
VideoCodec.Mjpeg => ContainerFormat.Avi,
|
||
VideoCodec.Gif => ContainerFormat.Gif,
|
||
VideoCodec.Apng => ContainerFormat.Apng,
|
||
_ => ContainerFormat.ImageSequence,
|
||
};
|
||
|
||
/// <summary>True for codecs every platform can produce without an OS encoder.</summary>
|
||
public static bool IsManaged(VideoCodec codec) => codec is not (VideoCodec.H264 or VideoCodec.Hevc or VideoCodec.Av1);
|
||
|
||
public static string FileExtension(VideoCodec codec) => codec switch
|
||
{
|
||
VideoCodec.H264 or VideoCodec.Hevc or VideoCodec.Av1 => ".mp4",
|
||
VideoCodec.Mjpeg => ".avi",
|
||
VideoCodec.Gif => ".gif",
|
||
VideoCodec.Apng => ".png",
|
||
VideoCodec.JpegSequence => ".jpg",
|
||
VideoCodec.PngSequence => ".png",
|
||
VideoCodec.Tiff16Sequence => ".tif",
|
||
VideoCodec.LinearDngSequence => ".dng",
|
||
_ => ".bin",
|
||
};
|
||
}
|
||
|
||
/// <summary>What an encoder on this machine can do (shown in Settings › Rendering).</summary>
|
||
public sealed record EncoderCapability(
|
||
VideoCodec Codec,
|
||
string Name,
|
||
bool Hardware,
|
||
int MaxWidth,
|
||
int MaxHeight,
|
||
bool SupportsTenBit,
|
||
string? Vendor = null);
|
||
|
||
/// <summary>Complete parameters of one encode.</summary>
|
||
public sealed record EncodeSettings
|
||
{
|
||
public required string OutputPath { get; init; }
|
||
public required VideoCodec Codec { get; init; }
|
||
public required int Width { get; init; }
|
||
public required int Height { get; init; }
|
||
public required Rational FrameRate { get; init; }
|
||
public CodecProfile Profile { get; init; } = CodecProfile.Auto;
|
||
public int BitDepth { get; init; } = 8;
|
||
public RateControl RateControl { get; init; } = RateControl.Vbr;
|
||
|
||
/// <summary>Average bit rate in bits per second (VBR/CBR).</summary>
|
||
public long BitRate { get; init; }
|
||
|
||
/// <summary>0–100 quality for <see cref="RateControl.Quality"/>, JPEG/MJPEG and GIF.</summary>
|
||
public int Quality { get; init; } = 90;
|
||
|
||
/// <summary>Distance between key frames in frames; 0 = encoder default (≈ 1 s).</summary>
|
||
public int GopLength { get; init; }
|
||
|
||
public int BFrames { get; init; } = DefaultBFrames;
|
||
|
||
/// <summary>The B-frames an encode asks for unless told otherwise.</summary>
|
||
public const int DefaultBFrames = 2;
|
||
|
||
public bool PreferHardware { get; init; } = true;
|
||
|
||
/// <summary>Optional specific encoder name from <see cref="EncoderCapability.Name"/>.</summary>
|
||
public string? EncoderName { get; init; }
|
||
|
||
/// <summary>
|
||
/// One frame saved as one image file at <see cref="OutputPath"/> (owner review 1, R8, "Save frame") instead of a numbered
|
||
/// sequence in a folder: the image-sequence codecs PNG, JPEG and TIFF only, and exactly one frame.
|
||
/// </summary>
|
||
public bool SingleImage { get; init; }
|
||
}
|
||
|
||
/// <summary>One open encode. Frames are linear-light buffers at the output size.</summary>
|
||
public interface IVideoEncoderSession : IDisposable
|
||
{
|
||
/// <summary>Encoder actually used ("NVIDIA NVENC H.264", "Managed MJPEG").</summary>
|
||
string EncoderName { get; }
|
||
|
||
bool IsHardware { get; }
|
||
|
||
/// <summary>Appends a frame shown for <paramref name="duration90k"/> ticks of a 90 kHz clock.</summary>
|
||
void WriteFrame(ImageBuffer frame, long duration90k);
|
||
|
||
/// <summary>Flushes and closes the output. Without it, <see cref="IDisposable.Dispose"/> deletes the partial file.</summary>
|
||
void Complete();
|
||
|
||
/// <summary>
|
||
/// The settings asked for that the encoder does not support or refused, so the output was encoded with its own instead;
|
||
/// empty when it took every one (the managed encoders, which have none of them to refuse).
|
||
/// </summary>
|
||
IReadOnlyList<EncoderSetting> Ignored => [];
|
||
}
|
||
|
||
/// <summary>An encoding setting an encoder may refuse (<see cref="IVideoEncoderSession.Ignored"/>).</summary>
|
||
public enum EncoderSetting
|
||
{
|
||
RateControl,
|
||
Quality,
|
||
BitRate,
|
||
KeyframeInterval,
|
||
BFrames,
|
||
}
|
||
|
||
/// <summary>A family of encoders (Media Foundation on Windows, managed everywhere).</summary>
|
||
public interface IVideoEncoderProvider
|
||
{
|
||
string Name { get; }
|
||
IReadOnlyList<EncoderCapability> Capabilities { get; }
|
||
bool CanEncode(EncodeSettings settings);
|
||
IVideoEncoderSession Open(EncodeSettings settings);
|
||
}
|