| | 1 | | #pragma warning disable CS1591 |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Runtime.Versioning; |
| | 9 | | using System.Text.RegularExpressions; |
| | 10 | | using MediaBrowser.Controller.MediaEncoding; |
| | 11 | | using Microsoft.Extensions.Logging; |
| | 12 | |
|
| | 13 | | namespace MediaBrowser.MediaEncoding.Encoder |
| | 14 | | { |
| | 15 | | public partial class EncoderValidator |
| | 16 | | { |
| 1 | 17 | | private static readonly string[] _requiredDecoders = new[] |
| 1 | 18 | | { |
| 1 | 19 | | "h264", |
| 1 | 20 | | "hevc", |
| 1 | 21 | | "vp8", |
| 1 | 22 | | "libvpx", |
| 1 | 23 | | "vp9", |
| 1 | 24 | | "libvpx-vp9", |
| 1 | 25 | | "av1", |
| 1 | 26 | | "libdav1d", |
| 1 | 27 | | "mpeg2video", |
| 1 | 28 | | "mpeg4", |
| 1 | 29 | | "msmpeg4", |
| 1 | 30 | | "dca", |
| 1 | 31 | | "ac3", |
| 1 | 32 | | "ac4", |
| 1 | 33 | | "aac", |
| 1 | 34 | | "mp3", |
| 1 | 35 | | "flac", |
| 1 | 36 | | "truehd", |
| 1 | 37 | | "h264_qsv", |
| 1 | 38 | | "hevc_qsv", |
| 1 | 39 | | "mpeg2_qsv", |
| 1 | 40 | | "vc1_qsv", |
| 1 | 41 | | "vp8_qsv", |
| 1 | 42 | | "vp9_qsv", |
| 1 | 43 | | "av1_qsv", |
| 1 | 44 | | "h264_cuvid", |
| 1 | 45 | | "hevc_cuvid", |
| 1 | 46 | | "mpeg2_cuvid", |
| 1 | 47 | | "vc1_cuvid", |
| 1 | 48 | | "mpeg4_cuvid", |
| 1 | 49 | | "vp8_cuvid", |
| 1 | 50 | | "vp9_cuvid", |
| 1 | 51 | | "av1_cuvid", |
| 1 | 52 | | "h264_rkmpp", |
| 1 | 53 | | "hevc_rkmpp", |
| 1 | 54 | | "mpeg1_rkmpp", |
| 1 | 55 | | "mpeg2_rkmpp", |
| 1 | 56 | | "mpeg4_rkmpp", |
| 1 | 57 | | "vp8_rkmpp", |
| 1 | 58 | | "vp9_rkmpp", |
| 1 | 59 | | "av1_rkmpp" |
| 1 | 60 | | }; |
| | 61 | |
|
| 1 | 62 | | private static readonly string[] _requiredEncoders = new[] |
| 1 | 63 | | { |
| 1 | 64 | | "libx264", |
| 1 | 65 | | "libx265", |
| 1 | 66 | | "libsvtav1", |
| 1 | 67 | | "aac", |
| 1 | 68 | | "aac_at", |
| 1 | 69 | | "libfdk_aac", |
| 1 | 70 | | "ac3", |
| 1 | 71 | | "alac", |
| 1 | 72 | | "dca", |
| 1 | 73 | | "libmp3lame", |
| 1 | 74 | | "libopus", |
| 1 | 75 | | "libvorbis", |
| 1 | 76 | | "flac", |
| 1 | 77 | | "truehd", |
| 1 | 78 | | "srt", |
| 1 | 79 | | "h264_amf", |
| 1 | 80 | | "hevc_amf", |
| 1 | 81 | | "av1_amf", |
| 1 | 82 | | "h264_qsv", |
| 1 | 83 | | "hevc_qsv", |
| 1 | 84 | | "mjpeg_qsv", |
| 1 | 85 | | "av1_qsv", |
| 1 | 86 | | "h264_nvenc", |
| 1 | 87 | | "hevc_nvenc", |
| 1 | 88 | | "av1_nvenc", |
| 1 | 89 | | "h264_vaapi", |
| 1 | 90 | | "hevc_vaapi", |
| 1 | 91 | | "av1_vaapi", |
| 1 | 92 | | "mjpeg_vaapi", |
| 1 | 93 | | "h264_v4l2m2m", |
| 1 | 94 | | "h264_videotoolbox", |
| 1 | 95 | | "hevc_videotoolbox", |
| 1 | 96 | | "mjpeg_videotoolbox", |
| 1 | 97 | | "h264_rkmpp", |
| 1 | 98 | | "hevc_rkmpp", |
| 1 | 99 | | "mjpeg_rkmpp" |
| 1 | 100 | | }; |
| | 101 | |
|
| 1 | 102 | | private static readonly string[] _requiredFilters = new[] |
| 1 | 103 | | { |
| 1 | 104 | | // sw |
| 1 | 105 | | "alphasrc", |
| 1 | 106 | | "zscale", |
| 1 | 107 | | "tonemapx", |
| 1 | 108 | | // qsv |
| 1 | 109 | | "scale_qsv", |
| 1 | 110 | | "vpp_qsv", |
| 1 | 111 | | "deinterlace_qsv", |
| 1 | 112 | | "overlay_qsv", |
| 1 | 113 | | // cuda |
| 1 | 114 | | "scale_cuda", |
| 1 | 115 | | "yadif_cuda", |
| 1 | 116 | | "bwdif_cuda", |
| 1 | 117 | | "tonemap_cuda", |
| 1 | 118 | | "overlay_cuda", |
| 1 | 119 | | "transpose_cuda", |
| 1 | 120 | | "hwupload_cuda", |
| 1 | 121 | | // opencl |
| 1 | 122 | | "scale_opencl", |
| 1 | 123 | | "tonemap_opencl", |
| 1 | 124 | | "overlay_opencl", |
| 1 | 125 | | "transpose_opencl", |
| 1 | 126 | | // vaapi |
| 1 | 127 | | "scale_vaapi", |
| 1 | 128 | | "deinterlace_vaapi", |
| 1 | 129 | | "tonemap_vaapi", |
| 1 | 130 | | "procamp_vaapi", |
| 1 | 131 | | "overlay_vaapi", |
| 1 | 132 | | "transpose_vaapi", |
| 1 | 133 | | "hwupload_vaapi", |
| 1 | 134 | | // vulkan |
| 1 | 135 | | "libplacebo", |
| 1 | 136 | | "scale_vulkan", |
| 1 | 137 | | "overlay_vulkan", |
| 1 | 138 | | "transpose_vulkan", |
| 1 | 139 | | "flip_vulkan", |
| 1 | 140 | | // videotoolbox |
| 1 | 141 | | "yadif_videotoolbox", |
| 1 | 142 | | "bwdif_videotoolbox", |
| 1 | 143 | | "scale_vt", |
| 1 | 144 | | "transpose_vt", |
| 1 | 145 | | "overlay_videotoolbox", |
| 1 | 146 | | "tonemap_videotoolbox", |
| 1 | 147 | | // rkrga |
| 1 | 148 | | "scale_rkrga", |
| 1 | 149 | | "vpp_rkrga", |
| 1 | 150 | | "overlay_rkrga" |
| 1 | 151 | | }; |
| | 152 | |
|
| 1 | 153 | | private static readonly Dictionary<int, string[]> _filterOptionsDict = new Dictionary<int, string[]> |
| 1 | 154 | | { |
| 1 | 155 | | { 0, new string[] { "scale_cuda", "format" } }, |
| 1 | 156 | | { 1, new string[] { "tonemap_cuda", "GPU accelerated HDR to SDR tonemapping" } }, |
| 1 | 157 | | { 2, new string[] { "tonemap_opencl", "bt2390" } }, |
| 1 | 158 | | { 3, new string[] { "overlay_opencl", "Action to take when encountering EOF from secondary input" } }, |
| 1 | 159 | | { 4, new string[] { "overlay_vaapi", "Action to take when encountering EOF from secondary input" } }, |
| 1 | 160 | | { 5, new string[] { "overlay_vulkan", "Action to take when encountering EOF from secondary input" } }, |
| 1 | 161 | | { 6, new string[] { "transpose_opencl", "rotate by half-turn" } } |
| 1 | 162 | | }; |
| | 163 | |
|
| 1 | 164 | | private static readonly Dictionary<BitStreamFilterOptionType, (string, string)> _bsfOptionsDict = new Dictionary |
| 1 | 165 | | { |
| 1 | 166 | | { BitStreamFilterOptionType.HevcMetadataRemoveDovi, ("hevc_metadata", "remove_dovi") }, |
| 1 | 167 | | { BitStreamFilterOptionType.HevcMetadataRemoveHdr10Plus, ("hevc_metadata", "remove_hdr10plus") }, |
| 1 | 168 | | { BitStreamFilterOptionType.Av1MetadataRemoveDovi, ("av1_metadata", "remove_dovi") }, |
| 1 | 169 | | { BitStreamFilterOptionType.Av1MetadataRemoveHdr10Plus, ("av1_metadata", "remove_hdr10plus") }, |
| 1 | 170 | | { BitStreamFilterOptionType.DoviRpuStrip, ("dovi_rpu", "strip") } |
| 1 | 171 | | }; |
| | 172 | |
|
| | 173 | | // These are the library versions that corresponds to our minimum ffmpeg version 4.4 according to the version ta |
| | 174 | | // Refers to the versions in https://ffmpeg.org/download.html |
| 1 | 175 | | private static readonly Dictionary<string, Version> _ffmpegMinimumLibraryVersions = new Dictionary<string, Versi |
| 1 | 176 | | { |
| 1 | 177 | | { "libavutil", new Version(56, 70) }, |
| 1 | 178 | | { "libavcodec", new Version(58, 134) }, |
| 1 | 179 | | { "libavformat", new Version(58, 76) }, |
| 1 | 180 | | { "libavdevice", new Version(58, 13) }, |
| 1 | 181 | | { "libavfilter", new Version(7, 110) }, |
| 1 | 182 | | { "libswscale", new Version(5, 9) }, |
| 1 | 183 | | { "libswresample", new Version(3, 9) }, |
| 1 | 184 | | { "libpostproc", new Version(55, 9) } |
| 1 | 185 | | }; |
| | 186 | |
|
| | 187 | | private readonly ILogger _logger; |
| | 188 | |
|
| | 189 | | private readonly string _encoderPath; |
| | 190 | |
|
| 16 | 191 | | private readonly Version _minFFmpegMultiThreadedCli = new Version(7, 0); |
| | 192 | |
|
| | 193 | | public EncoderValidator(ILogger logger, string encoderPath) |
| | 194 | | { |
| 16 | 195 | | _logger = logger; |
| 16 | 196 | | _encoderPath = encoderPath; |
| 16 | 197 | | } |
| | 198 | |
|
| | 199 | | private enum Codec |
| | 200 | | { |
| | 201 | | Encoder, |
| | 202 | | Decoder |
| | 203 | | } |
| | 204 | |
|
| | 205 | | // When changing this, also change the minimum library versions in _ffmpegMinimumLibraryVersions |
| 1 | 206 | | public static Version MinVersion { get; } = new Version(4, 4); |
| | 207 | |
|
| 1 | 208 | | public static Version? MaxVersion { get; } = null; |
| | 209 | |
|
| | 210 | | [GeneratedRegex(@"^ffmpeg version n?((?:[0-9]+\.?)+)")] |
| | 211 | | private static partial Regex FfmpegVersionRegex(); |
| | 212 | |
|
| | 213 | | [GeneratedRegex(@"((?<name>lib\w+)\s+(?<major>[0-9]+)\.\s*(?<minor>[0-9]+))", RegexOptions.Multiline)] |
| | 214 | | private static partial Regex LibraryRegex(); |
| | 215 | |
|
| | 216 | | public bool ValidateVersion() |
| | 217 | | { |
| | 218 | | string output; |
| | 219 | | try |
| | 220 | | { |
| 0 | 221 | | output = GetProcessOutput(_encoderPath, "-version", false, null); |
| 0 | 222 | | } |
| 0 | 223 | | catch (Exception ex) |
| | 224 | | { |
| 0 | 225 | | _logger.LogError(ex, "Error validating encoder"); |
| 0 | 226 | | return false; |
| | 227 | | } |
| | 228 | |
|
| 0 | 229 | | if (string.IsNullOrWhiteSpace(output)) |
| | 230 | | { |
| 0 | 231 | | _logger.LogError("FFmpeg validation: The process returned no result"); |
| 0 | 232 | | return false; |
| | 233 | | } |
| | 234 | |
|
| 0 | 235 | | _logger.LogDebug("ffmpeg output: {Output}", output); |
| | 236 | |
|
| 0 | 237 | | return ValidateVersionInternal(output); |
| 0 | 238 | | } |
| | 239 | |
|
| | 240 | | internal bool ValidateVersionInternal(string versionOutput) |
| | 241 | | { |
| 8 | 242 | | if (versionOutput.Contains("Libav developers", StringComparison.OrdinalIgnoreCase)) |
| | 243 | | { |
| 0 | 244 | | _logger.LogError("FFmpeg validation: avconv instead of ffmpeg is not supported"); |
| 0 | 245 | | return false; |
| | 246 | | } |
| | 247 | |
|
| | 248 | | // Work out what the version under test is |
| 8 | 249 | | var version = GetFFmpegVersionInternal(versionOutput); |
| | 250 | |
|
| 8 | 251 | | _logger.LogInformation("Found ffmpeg version {Version}", version is not null ? version.ToString() : "unknown |
| | 252 | |
|
| 8 | 253 | | if (version is null) |
| | 254 | | { |
| 1 | 255 | | if (MaxVersion is not null) // Version is unknown |
| | 256 | | { |
| 0 | 257 | | if (MinVersion == MaxVersion) |
| | 258 | | { |
| 0 | 259 | | _logger.LogWarning("FFmpeg validation: We recommend version {MinVersion}", MinVersion); |
| | 260 | | } |
| | 261 | | else |
| | 262 | | { |
| 0 | 263 | | _logger.LogWarning("FFmpeg validation: We recommend a minimum of {MinVersion} and maximum of {Ma |
| | 264 | | } |
| | 265 | | } |
| | 266 | | else |
| | 267 | | { |
| 1 | 268 | | _logger.LogWarning("FFmpeg validation: We recommend minimum version {MinVersion}", MinVersion); |
| | 269 | | } |
| | 270 | |
|
| 1 | 271 | | return false; |
| | 272 | | } |
| | 273 | |
|
| 7 | 274 | | if (version < MinVersion) // Version is below what we recommend |
| | 275 | | { |
| 1 | 276 | | _logger.LogWarning("FFmpeg validation: The minimum recommended version is {MinVersion}", MinVersion); |
| 1 | 277 | | return false; |
| | 278 | | } |
| | 279 | |
|
| 6 | 280 | | if (MaxVersion is not null && version > MaxVersion) // Version is above what we recommend |
| | 281 | | { |
| 0 | 282 | | _logger.LogWarning("FFmpeg validation: The maximum recommended version is {MaxVersion}", MaxVersion); |
| 0 | 283 | | return false; |
| | 284 | | } |
| | 285 | |
|
| 6 | 286 | | return true; |
| | 287 | | } |
| | 288 | |
|
| 0 | 289 | | public IEnumerable<string> GetDecoders() => GetCodecs(Codec.Decoder); |
| | 290 | |
|
| 0 | 291 | | public IEnumerable<string> GetEncoders() => GetCodecs(Codec.Encoder); |
| | 292 | |
|
| 0 | 293 | | public IEnumerable<string> GetHwaccels() => GetHwaccelTypes(); |
| | 294 | |
|
| 0 | 295 | | public IEnumerable<string> GetFilters() => GetFFmpegFilters(); |
| | 296 | |
|
| 0 | 297 | | public IDictionary<int, bool> GetFiltersWithOption() => GetFFmpegFiltersWithOption(); |
| | 298 | |
|
| 0 | 299 | | public IDictionary<BitStreamFilterOptionType, bool> GetBitStreamFiltersWithOption() => _bsfOptionsDict |
| 0 | 300 | | .ToDictionary(item => item.Key, item => CheckBitStreamFilterWithOption(item.Value.Item1, item.Value.Item2)); |
| | 301 | |
|
| | 302 | | public Version? GetFFmpegVersion() |
| | 303 | | { |
| | 304 | | string output; |
| | 305 | | try |
| | 306 | | { |
| 0 | 307 | | output = GetProcessOutput(_encoderPath, "-version", false, null); |
| 0 | 308 | | } |
| 0 | 309 | | catch (Exception ex) |
| | 310 | | { |
| 0 | 311 | | _logger.LogError(ex, "Error validating encoder"); |
| 0 | 312 | | return null; |
| | 313 | | } |
| | 314 | |
|
| 0 | 315 | | if (string.IsNullOrWhiteSpace(output)) |
| | 316 | | { |
| 0 | 317 | | _logger.LogError("FFmpeg validation: The process returned no result"); |
| 0 | 318 | | return null; |
| | 319 | | } |
| | 320 | |
|
| 0 | 321 | | _logger.LogDebug("ffmpeg output: {Output}", output); |
| | 322 | |
|
| 0 | 323 | | return GetFFmpegVersionInternal(output); |
| 0 | 324 | | } |
| | 325 | |
|
| | 326 | | /// <summary> |
| | 327 | | /// Using the output from "ffmpeg -version" work out the FFmpeg version. |
| | 328 | | /// For pre-built binaries the first line should contain a string like "ffmpeg version x.y", which is easy |
| | 329 | | /// to parse. If this is not available, then we try to match known library versions to FFmpeg versions. |
| | 330 | | /// If that fails then we test the libraries to determine if they're newer than our minimum versions. |
| | 331 | | /// </summary> |
| | 332 | | /// <param name="output">The output from "ffmpeg -version".</param> |
| | 333 | | /// <returns>The FFmpeg version.</returns> |
| | 334 | | internal Version? GetFFmpegVersionInternal(string output) |
| | 335 | | { |
| | 336 | | // For pre-built binaries the FFmpeg version should be mentioned at the very start of the output |
| 16 | 337 | | var match = FfmpegVersionRegex().Match(output); |
| | 338 | |
|
| 16 | 339 | | if (match.Success) |
| | 340 | | { |
| 12 | 341 | | if (Version.TryParse(match.Groups[1].ValueSpan, out var result)) |
| | 342 | | { |
| 12 | 343 | | return result; |
| | 344 | | } |
| | 345 | | } |
| | 346 | |
|
| 4 | 347 | | var versionMap = GetFFmpegLibraryVersions(output); |
| | 348 | |
|
| 4 | 349 | | var allVersionsValidated = true; |
| | 350 | |
|
| 72 | 351 | | foreach (var minimumVersion in _ffmpegMinimumLibraryVersions) |
| | 352 | | { |
| 32 | 353 | | if (versionMap.TryGetValue(minimumVersion.Key, out var foundVersion)) |
| | 354 | | { |
| 32 | 355 | | if (foundVersion >= minimumVersion.Value) |
| | 356 | | { |
| 16 | 357 | | _logger.LogInformation("Found {Library} version {FoundVersion} ({MinimumVersion})", minimumVersi |
| | 358 | | } |
| | 359 | | else |
| | 360 | | { |
| 16 | 361 | | _logger.LogWarning("Found {Library} version {FoundVersion} lower than recommended version {Minim |
| 16 | 362 | | allVersionsValidated = false; |
| | 363 | | } |
| | 364 | | } |
| | 365 | | else |
| | 366 | | { |
| 0 | 367 | | _logger.LogError("{Library} version not found", minimumVersion.Key); |
| 0 | 368 | | allVersionsValidated = false; |
| | 369 | | } |
| | 370 | | } |
| | 371 | |
|
| 4 | 372 | | return allVersionsValidated ? MinVersion : null; |
| | 373 | | } |
| | 374 | |
|
| | 375 | | /// <summary> |
| | 376 | | /// Grabs the library names and major.minor version numbers from the 'ffmpeg -version' output |
| | 377 | | /// and condenses them on to one line. Output format is "name1=major.minor,name2=major.minor,etc.". |
| | 378 | | /// </summary> |
| | 379 | | /// <param name="output">The 'ffmpeg -version' output.</param> |
| | 380 | | /// <returns>The library names and major.minor version numbers.</returns> |
| | 381 | | private static Dictionary<string, Version> GetFFmpegLibraryVersions(string output) |
| | 382 | | { |
| 4 | 383 | | var map = new Dictionary<string, Version>(); |
| | 384 | |
|
| 72 | 385 | | foreach (Match match in LibraryRegex().Matches(output)) |
| | 386 | | { |
| 32 | 387 | | var version = new Version( |
| 32 | 388 | | int.Parse(match.Groups["major"].ValueSpan, CultureInfo.InvariantCulture), |
| 32 | 389 | | int.Parse(match.Groups["minor"].ValueSpan, CultureInfo.InvariantCulture)); |
| | 390 | |
|
| 32 | 391 | | map.Add(match.Groups["name"].Value, version); |
| | 392 | | } |
| | 393 | |
|
| 4 | 394 | | return map; |
| | 395 | | } |
| | 396 | |
|
| | 397 | | public bool CheckVaapiDeviceByDriverName(string driverName, string renderNodePath) |
| | 398 | | { |
| 0 | 399 | | if (!OperatingSystem.IsLinux()) |
| | 400 | | { |
| 0 | 401 | | return false; |
| | 402 | | } |
| | 403 | |
|
| 0 | 404 | | if (string.IsNullOrEmpty(driverName) || string.IsNullOrEmpty(renderNodePath)) |
| | 405 | | { |
| 0 | 406 | | return false; |
| | 407 | | } |
| | 408 | |
|
| | 409 | | try |
| | 410 | | { |
| 0 | 411 | | var output = GetProcessOutput(_encoderPath, "-v verbose -hide_banner -init_hw_device vaapi=va:" + render |
| 0 | 412 | | return output.Contains(driverName, StringComparison.Ordinal); |
| | 413 | | } |
| 0 | 414 | | catch (Exception ex) |
| | 415 | | { |
| 0 | 416 | | _logger.LogError(ex, "Error detecting the given vaapi render node path"); |
| 0 | 417 | | return false; |
| | 418 | | } |
| 0 | 419 | | } |
| | 420 | |
|
| | 421 | | public bool CheckVulkanDrmDeviceByExtensionName(string renderNodePath, string[] vulkanExtensions) |
| | 422 | | { |
| 0 | 423 | | if (!OperatingSystem.IsLinux()) |
| | 424 | | { |
| 0 | 425 | | return false; |
| | 426 | | } |
| | 427 | |
|
| 0 | 428 | | if (string.IsNullOrEmpty(renderNodePath)) |
| | 429 | | { |
| 0 | 430 | | return false; |
| | 431 | | } |
| | 432 | |
|
| | 433 | | try |
| | 434 | | { |
| 0 | 435 | | var command = "-v verbose -hide_banner -init_hw_device drm=dr:" + renderNodePath + " -init_hw_device vul |
| 0 | 436 | | var output = GetProcessOutput(_encoderPath, command, true, null); |
| 0 | 437 | | foreach (string ext in vulkanExtensions) |
| | 438 | | { |
| 0 | 439 | | if (!output.Contains(ext, StringComparison.Ordinal)) |
| | 440 | | { |
| 0 | 441 | | return false; |
| | 442 | | } |
| | 443 | | } |
| | 444 | |
|
| 0 | 445 | | return true; |
| | 446 | | } |
| 0 | 447 | | catch (Exception ex) |
| | 448 | | { |
| 0 | 449 | | _logger.LogError(ex, "Error detecting the given drm render node path"); |
| 0 | 450 | | return false; |
| | 451 | | } |
| 0 | 452 | | } |
| | 453 | |
|
| | 454 | | [SupportedOSPlatform("macos")] |
| | 455 | | public bool CheckIsVideoToolboxAv1DecodeAvailable() |
| | 456 | | { |
| 0 | 457 | | return ApplePlatformHelper.HasAv1HardwareAccel(_logger); |
| | 458 | | } |
| | 459 | |
|
| | 460 | | private IEnumerable<string> GetHwaccelTypes() |
| | 461 | | { |
| 0 | 462 | | string? output = null; |
| | 463 | | try |
| | 464 | | { |
| 0 | 465 | | output = GetProcessOutput(_encoderPath, "-hwaccels", false, null); |
| 0 | 466 | | } |
| 0 | 467 | | catch (Exception ex) |
| | 468 | | { |
| 0 | 469 | | _logger.LogError(ex, "Error detecting available hwaccel types"); |
| 0 | 470 | | } |
| | 471 | |
|
| 0 | 472 | | if (string.IsNullOrWhiteSpace(output)) |
| | 473 | | { |
| 0 | 474 | | return Enumerable.Empty<string>(); |
| | 475 | | } |
| | 476 | |
|
| 0 | 477 | | var found = output.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Skip(1).Distinct( |
| 0 | 478 | | _logger.LogInformation("Available hwaccel types: {Types}", found); |
| | 479 | |
|
| 0 | 480 | | return found; |
| | 481 | | } |
| | 482 | |
|
| | 483 | | public bool CheckFilterWithOption(string filter, string option) |
| | 484 | | { |
| 0 | 485 | | if (string.IsNullOrEmpty(filter) || string.IsNullOrEmpty(option)) |
| | 486 | | { |
| 0 | 487 | | return false; |
| | 488 | | } |
| | 489 | |
|
| | 490 | | string output; |
| | 491 | | try |
| | 492 | | { |
| 0 | 493 | | output = GetProcessOutput(_encoderPath, "-h filter=" + filter, false, null); |
| 0 | 494 | | } |
| 0 | 495 | | catch (Exception ex) |
| | 496 | | { |
| 0 | 497 | | _logger.LogError(ex, "Error detecting the given filter"); |
| 0 | 498 | | return false; |
| | 499 | | } |
| | 500 | |
|
| 0 | 501 | | if (output.Contains("Filter " + filter, StringComparison.Ordinal)) |
| | 502 | | { |
| 0 | 503 | | return output.Contains(option, StringComparison.Ordinal); |
| | 504 | | } |
| | 505 | |
|
| 0 | 506 | | _logger.LogWarning("Filter: {Name} with option {Option} is not available", filter, option); |
| | 507 | |
|
| 0 | 508 | | return false; |
| 0 | 509 | | } |
| | 510 | |
|
| | 511 | | public bool CheckBitStreamFilterWithOption(string filter, string option) |
| | 512 | | { |
| 0 | 513 | | if (string.IsNullOrEmpty(filter) || string.IsNullOrEmpty(option)) |
| | 514 | | { |
| 0 | 515 | | return false; |
| | 516 | | } |
| | 517 | |
|
| | 518 | | string output; |
| | 519 | | try |
| | 520 | | { |
| 0 | 521 | | output = GetProcessOutput(_encoderPath, "-h bsf=" + filter, false, null); |
| 0 | 522 | | } |
| 0 | 523 | | catch (Exception ex) |
| | 524 | | { |
| 0 | 525 | | _logger.LogError(ex, "Error detecting the given bit stream filter"); |
| 0 | 526 | | return false; |
| | 527 | | } |
| | 528 | |
|
| 0 | 529 | | if (output.Contains("Bit stream filter " + filter, StringComparison.Ordinal)) |
| | 530 | | { |
| 0 | 531 | | return output.Contains(option, StringComparison.Ordinal); |
| | 532 | | } |
| | 533 | |
|
| 0 | 534 | | _logger.LogWarning("Bit stream filter: {Name} with option {Option} is not available", filter, option); |
| | 535 | |
|
| 0 | 536 | | return false; |
| 0 | 537 | | } |
| | 538 | |
|
| | 539 | | public bool CheckSupportedRuntimeKey(string keyDesc, Version? ffmpegVersion) |
| | 540 | | { |
| 0 | 541 | | if (string.IsNullOrEmpty(keyDesc)) |
| | 542 | | { |
| 0 | 543 | | return false; |
| | 544 | | } |
| | 545 | |
|
| | 546 | | string output; |
| | 547 | | try |
| | 548 | | { |
| | 549 | | // With multi-threaded cli support, FFmpeg 7 is less sensitive to keyboard input |
| 0 | 550 | | var duration = ffmpegVersion >= _minFFmpegMultiThreadedCli ? 10000 : 1000; |
| 0 | 551 | | output = GetProcessOutput(_encoderPath, $"-hide_banner -f lavfi -i nullsrc=s=1x1:d={duration} -f null -" |
| 0 | 552 | | } |
| 0 | 553 | | catch (Exception ex) |
| | 554 | | { |
| 0 | 555 | | _logger.LogError(ex, "Error checking supported runtime key"); |
| 0 | 556 | | return false; |
| | 557 | | } |
| | 558 | |
|
| 0 | 559 | | return output.Contains(keyDesc, StringComparison.Ordinal); |
| 0 | 560 | | } |
| | 561 | |
|
| | 562 | | public bool CheckSupportedHwaccelFlag(string flag) |
| | 563 | | { |
| 0 | 564 | | return !string.IsNullOrEmpty(flag) && GetProcessExitCode(_encoderPath, $"-loglevel quiet -hwaccel_flags +{fl |
| | 565 | | } |
| | 566 | |
|
| | 567 | | public bool CheckSupportedProberOption(string option, string proberPath) |
| | 568 | | { |
| 0 | 569 | | return !string.IsNullOrEmpty(option) && GetProcessExitCode(proberPath, $"-loglevel quiet -f lavfi -i nullsrc |
| | 570 | | } |
| | 571 | |
|
| | 572 | | private IEnumerable<string> GetCodecs(Codec codec) |
| | 573 | | { |
| 0 | 574 | | string codecstr = codec == Codec.Encoder ? "encoders" : "decoders"; |
| | 575 | | string output; |
| | 576 | | try |
| | 577 | | { |
| 0 | 578 | | output = GetProcessOutput(_encoderPath, "-" + codecstr, false, null); |
| 0 | 579 | | } |
| 0 | 580 | | catch (Exception ex) |
| | 581 | | { |
| 0 | 582 | | _logger.LogError(ex, "Error detecting available {Codec}", codecstr); |
| 0 | 583 | | return Enumerable.Empty<string>(); |
| | 584 | | } |
| | 585 | |
|
| 0 | 586 | | if (string.IsNullOrWhiteSpace(output)) |
| | 587 | | { |
| 0 | 588 | | return Enumerable.Empty<string>(); |
| | 589 | | } |
| | 590 | |
|
| 0 | 591 | | var required = codec == Codec.Encoder ? _requiredEncoders : _requiredDecoders; |
| | 592 | |
|
| 0 | 593 | | var found = CodecRegex() |
| 0 | 594 | | .Matches(output) |
| 0 | 595 | | .Select(x => x.Groups["codec"].Value) |
| 0 | 596 | | .Where(x => required.Contains(x)); |
| | 597 | |
|
| 0 | 598 | | _logger.LogInformation("Available {Codec}: {Codecs}", codecstr, found); |
| | 599 | |
|
| 0 | 600 | | return found; |
| 0 | 601 | | } |
| | 602 | |
|
| | 603 | | private IEnumerable<string> GetFFmpegFilters() |
| | 604 | | { |
| | 605 | | string output; |
| | 606 | | try |
| | 607 | | { |
| 0 | 608 | | output = GetProcessOutput(_encoderPath, "-filters", false, null); |
| 0 | 609 | | } |
| 0 | 610 | | catch (Exception ex) |
| | 611 | | { |
| 0 | 612 | | _logger.LogError(ex, "Error detecting available filters"); |
| 0 | 613 | | return Enumerable.Empty<string>(); |
| | 614 | | } |
| | 615 | |
|
| 0 | 616 | | if (string.IsNullOrWhiteSpace(output)) |
| | 617 | | { |
| 0 | 618 | | return Enumerable.Empty<string>(); |
| | 619 | | } |
| | 620 | |
|
| 0 | 621 | | var found = FilterRegex() |
| 0 | 622 | | .Matches(output) |
| 0 | 623 | | .Select(x => x.Groups["filter"].Value) |
| 0 | 624 | | .Where(x => _requiredFilters.Contains(x)); |
| | 625 | |
|
| 0 | 626 | | _logger.LogInformation("Available filters: {Filters}", found); |
| | 627 | |
|
| 0 | 628 | | return found; |
| 0 | 629 | | } |
| | 630 | |
|
| | 631 | | private Dictionary<int, bool> GetFFmpegFiltersWithOption() |
| | 632 | | { |
| 0 | 633 | | Dictionary<int, bool> dict = new Dictionary<int, bool>(); |
| 0 | 634 | | for (int i = 0; i < _filterOptionsDict.Count; i++) |
| | 635 | | { |
| 0 | 636 | | if (_filterOptionsDict.TryGetValue(i, out var val) && val.Length == 2) |
| | 637 | | { |
| 0 | 638 | | dict.Add(i, CheckFilterWithOption(val[0], val[1])); |
| | 639 | | } |
| | 640 | | } |
| | 641 | |
|
| 0 | 642 | | return dict; |
| | 643 | | } |
| | 644 | |
|
| | 645 | | private string GetProcessOutput(string path, string arguments, bool readStdErr, string? testKey) |
| | 646 | | { |
| 0 | 647 | | var redirectStandardIn = !string.IsNullOrEmpty(testKey); |
| 0 | 648 | | using (var process = new Process |
| 0 | 649 | | { |
| 0 | 650 | | StartInfo = new ProcessStartInfo(path, arguments) |
| 0 | 651 | | { |
| 0 | 652 | | CreateNoWindow = true, |
| 0 | 653 | | UseShellExecute = false, |
| 0 | 654 | | WindowStyle = ProcessWindowStyle.Hidden, |
| 0 | 655 | | ErrorDialog = false, |
| 0 | 656 | | RedirectStandardInput = redirectStandardIn, |
| 0 | 657 | | RedirectStandardOutput = true, |
| 0 | 658 | | RedirectStandardError = true |
| 0 | 659 | | } |
| 0 | 660 | | }) |
| | 661 | | { |
| 0 | 662 | | _logger.LogDebug("Running {Path} {Arguments}", path, arguments); |
| | 663 | |
|
| 0 | 664 | | process.Start(); |
| | 665 | |
|
| 0 | 666 | | if (redirectStandardIn) |
| | 667 | | { |
| 0 | 668 | | using var writer = process.StandardInput; |
| 0 | 669 | | writer.Write(testKey); |
| | 670 | | } |
| | 671 | |
|
| 0 | 672 | | using var reader = readStdErr ? process.StandardError : process.StandardOutput; |
| 0 | 673 | | return reader.ReadToEnd(); |
| | 674 | | } |
| 0 | 675 | | } |
| | 676 | |
|
| | 677 | | private bool GetProcessExitCode(string path, string arguments) |
| | 678 | | { |
| 0 | 679 | | using var process = new Process(); |
| 0 | 680 | | process.StartInfo = new ProcessStartInfo(path, arguments) |
| 0 | 681 | | { |
| 0 | 682 | | CreateNoWindow = true, |
| 0 | 683 | | UseShellExecute = false, |
| 0 | 684 | | WindowStyle = ProcessWindowStyle.Hidden, |
| 0 | 685 | | ErrorDialog = false |
| 0 | 686 | | }; |
| 0 | 687 | | _logger.LogDebug("Running {Path} {Arguments}", path, arguments); |
| | 688 | |
|
| | 689 | | try |
| | 690 | | { |
| 0 | 691 | | process.Start(); |
| 0 | 692 | | process.WaitForExit(); |
| 0 | 693 | | return process.ExitCode == 0; |
| | 694 | | } |
| 0 | 695 | | catch (Exception ex) |
| | 696 | | { |
| 0 | 697 | | _logger.LogError("Running {Path} {Arguments} failed with exception {Exception}", path, arguments, ex.Mes |
| 0 | 698 | | return false; |
| | 699 | | } |
| 0 | 700 | | } |
| | 701 | |
|
| | 702 | | [GeneratedRegex("^\\s\\S{6}\\s(?<codec>[\\w|-]+)\\s+.+$", RegexOptions.Multiline)] |
| | 703 | | private static partial Regex CodecRegex(); |
| | 704 | |
|
| | 705 | | [GeneratedRegex("^\\s\\S{3}\\s(?<filter>[\\w|-]+)\\s+.+$", RegexOptions.Multiline)] |
| | 706 | | private static partial Regex FilterRegex(); |
| | 707 | | } |
| | 708 | | } |