using FFMpegCore.Extend; using System.Collections.Generic; using System.Drawing; using System.Linq; namespace FFMpegCore.Arguments { public class SubtitleHardBurnArgument : IVideoFilterArgument { private readonly SubtitleHardBurnOptions _subtitleHardBurnOptions; public SubtitleHardBurnArgument(SubtitleHardBurnOptions subtitleHardBurnOptions) { _subtitleHardBurnOptions = subtitleHardBurnOptions; } public string Key => "subtitles"; public string Value => _subtitleHardBurnOptions.TextInternal; } public class SubtitleHardBurnOptions { private readonly string _subtitle; public readonly Dictionary Parameters = new Dictionary(); /// /// Create a new using a provided subtitle file or a video file /// containing one. /// /// /// /// Only support .srt and .ass files, and subrip and ssa subtitle streams public static SubtitleHardBurnOptions Create(string subtitlePath) { return new SubtitleHardBurnOptions(subtitlePath); } private SubtitleHardBurnOptions(string subtitle) { _subtitle = subtitle; } /// /// Specify the size of the original video, the video for which the ASS file was composed. /// /// /// /// public SubtitleHardBurnOptions SetOriginalSize(int width, int height) { return WithParameter("original_size", $"{width}x{height}"); } /// /// Specify the size of the original video, the video for which the ASS file was composed. /// /// /// public SubtitleHardBurnOptions SetOriginalSize(Size size) { return SetOriginalSize(size.Width, size.Height); } /// /// Set subtitles stream index. /// /// /// /// /// Used when the provided subtitle is an stream of a video file (ex. .mkv) with multiple subtitles. /// Represent the index of the subtitle not the stream, them the first subtitle index is 0 and second is 1 /// public SubtitleHardBurnOptions SetSubtitleIndex(int index) { return WithParameter("stream_index", index.ToString()); } /// /// Set subtitles input character encoding. Only useful if not UTF-8 /// /// Charset encoding /// public SubtitleHardBurnOptions SetCharacterEncoding(string encode) { return WithParameter("charenc", encode); } /// /// Override default style or script info parameters of the subtitles /// /// /// public SubtitleHardBurnOptions WithStyle(StyleOptions styleOptions) { return WithParameter("force_style", styleOptions.TextInternal); } public SubtitleHardBurnOptions WithParameter(string key, string value) { Parameters.Add(key, value); return this; } internal string TextInternal => string.Join(":", new[] { _subtitle.EncloseIfContainsSpace() }.Concat(Parameters.Select(parameter => parameter.FormatArgumentPair(enclose: true)))); } public class StyleOptions { public readonly Dictionary Parameters = new Dictionary(); public static StyleOptions Create() { return new StyleOptions(); } /// /// Used to override default style or script info parameters of the subtitles. It accepts ASS style format /// /// /// /// public StyleOptions WithParameter(string key, string value) { Parameters.Add(key, value); return this; } internal string TextInternal => string.Join(",", Parameters.Select(parameter => parameter.FormatArgumentPair(enclose: false))); } }