MigrateEncodingOptions.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using System.Xml.Serialization;
  5. using Emby.Server.Implementations;
  6. using MediaBrowser.Model.Configuration;
  7. using MediaBrowser.Model.Entities;
  8. using Microsoft.Extensions.Logging;
  9. namespace Jellyfin.Server.Migrations.PreStartupRoutines;
  10. /// <inheritdoc />
  11. #pragma warning disable CS0618 // Type or member is obsolete
  12. [JellyfinMigration("2025-04-20T03:00:00", nameof(MigrateEncodingOptions), "A8E61960-7726-4450-8F3D-82C12DAABBCB", Stage = Stages.JellyfinMigrationStageTypes.PreInitialisation)]
  13. public class MigrateEncodingOptions : IMigrationRoutine
  14. #pragma warning restore CS0618 // Type or member is obsolete
  15. {
  16. private readonly ServerApplicationPaths _applicationPaths;
  17. private readonly ILogger<MigrateEncodingOptions> _logger;
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="MigrateEncodingOptions"/> class.
  20. /// </summary>
  21. /// <param name="applicationPaths">An instance of <see cref="ServerApplicationPaths"/>.</param>
  22. /// <param name="loggerFactory">An instance of the <see cref="ILoggerFactory"/> interface.</param>
  23. public MigrateEncodingOptions(ServerApplicationPaths applicationPaths, ILoggerFactory loggerFactory)
  24. {
  25. _applicationPaths = applicationPaths;
  26. _logger = loggerFactory.CreateLogger<MigrateEncodingOptions>();
  27. }
  28. /// <inheritdoc />
  29. public void Perform()
  30. {
  31. string path = Path.Combine(_applicationPaths.ConfigurationDirectoryPath, "encoding.xml");
  32. var oldSerializer = new XmlSerializer(typeof(OldEncodingOptions), new XmlRootAttribute("EncodingOptions"));
  33. OldEncodingOptions? oldConfig = null;
  34. try
  35. {
  36. using var xmlReader = XmlReader.Create(path);
  37. oldConfig = (OldEncodingOptions?)oldSerializer.Deserialize(xmlReader);
  38. }
  39. catch (InvalidOperationException ex)
  40. {
  41. _logger.LogError(ex, "Migrate EncodingOptions deserialize Invalid Operation error");
  42. }
  43. catch (Exception ex)
  44. {
  45. _logger.LogError(ex, "Migrate EncodingOptions deserialize error");
  46. }
  47. if (oldConfig is null)
  48. {
  49. return;
  50. }
  51. var hardwareAccelerationType = HardwareAccelerationType.none;
  52. if (Enum.TryParse<HardwareAccelerationType>(oldConfig.HardwareAccelerationType, true, out var parsedHardwareAccelerationType))
  53. {
  54. hardwareAccelerationType = parsedHardwareAccelerationType;
  55. }
  56. var tonemappingAlgorithm = TonemappingAlgorithm.none;
  57. if (Enum.TryParse<TonemappingAlgorithm>(oldConfig.TonemappingAlgorithm, true, out var parsedTonemappingAlgorithm))
  58. {
  59. tonemappingAlgorithm = parsedTonemappingAlgorithm;
  60. }
  61. var tonemappingMode = TonemappingMode.auto;
  62. if (Enum.TryParse<TonemappingMode>(oldConfig.TonemappingMode, true, out var parsedTonemappingMode))
  63. {
  64. tonemappingMode = parsedTonemappingMode;
  65. }
  66. var tonemappingRange = TonemappingRange.auto;
  67. if (Enum.TryParse<TonemappingRange>(oldConfig.TonemappingRange, true, out var parsedTonemappingRange))
  68. {
  69. tonemappingRange = parsedTonemappingRange;
  70. }
  71. var encoderPreset = EncoderPreset.superfast;
  72. if (Enum.TryParse<EncoderPreset>(oldConfig.TonemappingRange, true, out var parsedEncoderPreset))
  73. {
  74. encoderPreset = parsedEncoderPreset;
  75. }
  76. var deinterlaceMethod = DeinterlaceMethod.yadif;
  77. if (Enum.TryParse<DeinterlaceMethod>(oldConfig.TonemappingRange, true, out var parsedDeinterlaceMethod))
  78. {
  79. deinterlaceMethod = parsedDeinterlaceMethod;
  80. }
  81. var encodingOptions = new EncodingOptions()
  82. {
  83. EncodingThreadCount = oldConfig.EncodingThreadCount,
  84. TranscodingTempPath = oldConfig.TranscodingTempPath,
  85. FallbackFontPath = oldConfig.FallbackFontPath,
  86. EnableFallbackFont = oldConfig.EnableFallbackFont,
  87. EnableAudioVbr = oldConfig.EnableAudioVbr,
  88. DownMixAudioBoost = oldConfig.DownMixAudioBoost,
  89. DownMixStereoAlgorithm = oldConfig.DownMixStereoAlgorithm,
  90. MaxMuxingQueueSize = oldConfig.MaxMuxingQueueSize,
  91. EnableThrottling = oldConfig.EnableThrottling,
  92. ThrottleDelaySeconds = oldConfig.ThrottleDelaySeconds,
  93. EnableSegmentDeletion = oldConfig.EnableSegmentDeletion,
  94. SegmentKeepSeconds = oldConfig.SegmentKeepSeconds,
  95. HardwareAccelerationType = hardwareAccelerationType,
  96. EncoderAppPath = oldConfig.EncoderAppPath,
  97. EncoderAppPathDisplay = oldConfig.EncoderAppPathDisplay,
  98. VaapiDevice = oldConfig.VaapiDevice,
  99. EnableTonemapping = oldConfig.EnableTonemapping,
  100. EnableVppTonemapping = oldConfig.EnableVppTonemapping,
  101. EnableVideoToolboxTonemapping = oldConfig.EnableVideoToolboxTonemapping,
  102. TonemappingAlgorithm = tonemappingAlgorithm,
  103. TonemappingMode = tonemappingMode,
  104. TonemappingRange = tonemappingRange,
  105. TonemappingDesat = oldConfig.TonemappingDesat,
  106. TonemappingPeak = oldConfig.TonemappingPeak,
  107. TonemappingParam = oldConfig.TonemappingParam,
  108. VppTonemappingBrightness = oldConfig.VppTonemappingBrightness,
  109. VppTonemappingContrast = oldConfig.VppTonemappingContrast,
  110. H264Crf = oldConfig.H264Crf,
  111. H265Crf = oldConfig.H265Crf,
  112. EncoderPreset = encoderPreset,
  113. DeinterlaceDoubleRate = oldConfig.DeinterlaceDoubleRate,
  114. DeinterlaceMethod = deinterlaceMethod,
  115. EnableDecodingColorDepth10Hevc = oldConfig.EnableDecodingColorDepth10Hevc,
  116. EnableDecodingColorDepth10Vp9 = oldConfig.EnableDecodingColorDepth10Vp9,
  117. EnableEnhancedNvdecDecoder = oldConfig.EnableEnhancedNvdecDecoder,
  118. PreferSystemNativeHwDecoder = oldConfig.PreferSystemNativeHwDecoder,
  119. EnableIntelLowPowerH264HwEncoder = oldConfig.EnableIntelLowPowerH264HwEncoder,
  120. EnableIntelLowPowerHevcHwEncoder = oldConfig.EnableIntelLowPowerHevcHwEncoder,
  121. EnableHardwareEncoding = oldConfig.EnableHardwareEncoding,
  122. AllowHevcEncoding = oldConfig.AllowHevcEncoding,
  123. AllowAv1Encoding = oldConfig.AllowAv1Encoding,
  124. EnableSubtitleExtraction = oldConfig.EnableSubtitleExtraction,
  125. HardwareDecodingCodecs = oldConfig.HardwareDecodingCodecs,
  126. AllowOnDemandMetadataBasedKeyframeExtractionForExtensions = oldConfig.AllowOnDemandMetadataBasedKeyframeExtractionForExtensions
  127. };
  128. var newSerializer = new XmlSerializer(typeof(EncodingOptions));
  129. var xmlWriterSettings = new XmlWriterSettings { Indent = true };
  130. using var xmlWriter = XmlWriter.Create(path, xmlWriterSettings);
  131. newSerializer.Serialize(xmlWriter, encodingOptions);
  132. }
  133. #pragma warning disable
  134. public sealed class OldEncodingOptions
  135. {
  136. public int EncodingThreadCount { get; set; }
  137. public string TranscodingTempPath { get; set; }
  138. public string FallbackFontPath { get; set; }
  139. public bool EnableFallbackFont { get; set; }
  140. public bool EnableAudioVbr { get; set; }
  141. public double DownMixAudioBoost { get; set; }
  142. public DownMixStereoAlgorithms DownMixStereoAlgorithm { get; set; }
  143. public int MaxMuxingQueueSize { get; set; }
  144. public bool EnableThrottling { get; set; }
  145. public int ThrottleDelaySeconds { get; set; }
  146. public bool EnableSegmentDeletion { get; set; }
  147. public int SegmentKeepSeconds { get; set; }
  148. public string HardwareAccelerationType { get; set; }
  149. public string EncoderAppPath { get; set; }
  150. public string EncoderAppPathDisplay { get; set; }
  151. public string VaapiDevice { get; set; }
  152. public bool EnableTonemapping { get; set; }
  153. public bool EnableVppTonemapping { get; set; }
  154. public bool EnableVideoToolboxTonemapping { get; set; }
  155. public string TonemappingAlgorithm { get; set; }
  156. public string TonemappingMode { get; set; }
  157. public string TonemappingRange { get; set; }
  158. public double TonemappingDesat { get; set; }
  159. public double TonemappingPeak { get; set; }
  160. public double TonemappingParam { get; set; }
  161. public double VppTonemappingBrightness { get; set; }
  162. public double VppTonemappingContrast { get; set; }
  163. public int H264Crf { get; set; }
  164. public int H265Crf { get; set; }
  165. public string EncoderPreset { get; set; }
  166. public bool DeinterlaceDoubleRate { get; set; }
  167. public string DeinterlaceMethod { get; set; }
  168. public bool EnableDecodingColorDepth10Hevc { get; set; }
  169. public bool EnableDecodingColorDepth10Vp9 { get; set; }
  170. public bool EnableEnhancedNvdecDecoder { get; set; }
  171. public bool PreferSystemNativeHwDecoder { get; set; }
  172. public bool EnableIntelLowPowerH264HwEncoder { get; set; }
  173. public bool EnableIntelLowPowerHevcHwEncoder { get; set; }
  174. public bool EnableHardwareEncoding { get; set; }
  175. public bool AllowHevcEncoding { get; set; }
  176. public bool AllowAv1Encoding { get; set; }
  177. public bool EnableSubtitleExtraction { get; set; }
  178. public string[] HardwareDecodingCodecs { get; set; }
  179. public string[] AllowOnDemandMetadataBasedKeyframeExtractionForExtensions { get; set; }
  180. }
  181. }