EncodingJobOptions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using MediaBrowser.Model.Dlna;
  6. using MediaBrowser.Model.Services;
  7. namespace MediaBrowser.Controller.MediaEncoding
  8. {
  9. public class EncodingJobOptions : BaseEncodingJobOptions
  10. {
  11. public string OutputDirectory { get; set; }
  12. public string ItemId { get; set; }
  13. public string MediaSourceId { get; set; }
  14. public string AudioCodec { get; set; }
  15. public bool ReadInputAtNativeFramerate { get; set; }
  16. /// <summary>
  17. /// Gets a value indicating whether this instance has fixed resolution.
  18. /// </summary>
  19. /// <value><c>true</c> if this instance has fixed resolution; otherwise, <c>false</c>.</value>
  20. public bool HasFixedResolution => Width.HasValue || Height.HasValue;
  21. private readonly CultureInfo _usCulture = new CultureInfo("en-US");
  22. public EncodingJobOptions(StreamInfo info, DeviceProfile deviceProfile)
  23. {
  24. OutputContainer = info.Container;
  25. StartTimeTicks = info.StartPositionTicks;
  26. MaxWidth = info.MaxWidth;
  27. MaxHeight = info.MaxHeight;
  28. MaxFramerate = info.MaxFramerate;
  29. ItemId = info.ItemId.ToString("");
  30. MediaSourceId = info.MediaSourceId;
  31. AudioCodec = info.TargetAudioCodec.FirstOrDefault();
  32. MaxAudioChannels = info.GlobalMaxAudioChannels;
  33. AudioBitRate = info.AudioBitrate;
  34. AudioSampleRate = info.TargetAudioSampleRate;
  35. DeviceProfile = deviceProfile;
  36. VideoCodec = info.TargetVideoCodec.FirstOrDefault();
  37. VideoBitRate = info.VideoBitrate;
  38. AudioStreamIndex = info.AudioStreamIndex;
  39. SubtitleMethod = info.SubtitleDeliveryMethod;
  40. Context = info.Context;
  41. TranscodingMaxAudioChannels = info.TranscodingMaxAudioChannels;
  42. if (info.SubtitleDeliveryMethod != SubtitleDeliveryMethod.External)
  43. {
  44. SubtitleStreamIndex = info.SubtitleStreamIndex;
  45. }
  46. StreamOptions = info.StreamOptions;
  47. }
  48. }
  49. // For now until api and media encoding layers are unified
  50. public class BaseEncodingJobOptions
  51. {
  52. [ApiMember(Name = "EnableAutoStreamCopy", Description = "Whether or not to allow automatic stream copy if requested values match the original source. Defaults to true.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  53. public bool EnableAutoStreamCopy { get; set; }
  54. public bool AllowVideoStreamCopy { get; set; }
  55. public bool AllowAudioStreamCopy { get; set; }
  56. public bool BreakOnNonKeyFrames { get; set; }
  57. /// <summary>
  58. /// Gets or sets the audio sample rate.
  59. /// </summary>
  60. /// <value>The audio sample rate.</value>
  61. [ApiMember(Name = "AudioSampleRate", Description = "Optional. Specify a specific audio sample rate, e.g. 44100", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  62. public int? AudioSampleRate { get; set; }
  63. public int? MaxAudioBitDepth { get; set; }
  64. /// <summary>
  65. /// Gets or sets the audio bit rate.
  66. /// </summary>
  67. /// <value>The audio bit rate.</value>
  68. [ApiMember(Name = "AudioBitRate", Description = "Optional. Specify an audio bitrate to encode to, e.g. 128000. If omitted this will be left to encoder defaults.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  69. public int? AudioBitRate { get; set; }
  70. /// <summary>
  71. /// Gets or sets the audio channels.
  72. /// </summary>
  73. /// <value>The audio channels.</value>
  74. [ApiMember(Name = "AudioChannels", Description = "Optional. Specify a specific number of audio channels to encode to, e.g. 2", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  75. public int? AudioChannels { get; set; }
  76. [ApiMember(Name = "MaxAudioChannels", Description = "Optional. Specify a maximum number of audio channels to encode to, e.g. 2", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  77. public int? MaxAudioChannels { get; set; }
  78. [ApiMember(Name = "Static", Description = "Optional. If true, the original file will be streamed statically without any encoding. Use either no url extension or the original file extension. true/false", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  79. public bool Static { get; set; }
  80. /// <summary>
  81. /// Gets or sets the profile.
  82. /// </summary>
  83. /// <value>The profile.</value>
  84. [ApiMember(Name = "Profile", Description = "Optional. Specify a specific h264 profile, e.g. main, baseline, high.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  85. public string Profile { get; set; }
  86. /// <summary>
  87. /// Gets or sets the level.
  88. /// </summary>
  89. /// <value>The level.</value>
  90. [ApiMember(Name = "Level", Description = "Optional. Specify a level for the h264 profile, e.g. 3, 3.1.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  91. public string Level { get; set; }
  92. /// <summary>
  93. /// Gets or sets the framerate.
  94. /// </summary>
  95. /// <value>The framerate.</value>
  96. [ApiMember(Name = "Framerate", Description = "Optional. A specific video framerate to encode to, e.g. 23.976. Generally this should be omitted unless the device has specific requirements.", IsRequired = false, DataType = "double", ParameterType = "query", Verb = "GET")]
  97. public float? Framerate { get; set; }
  98. [ApiMember(Name = "MaxFramerate", Description = "Optional. A specific maximum video framerate to encode to, e.g. 23.976. Generally this should be omitted unless the device has specific requirements.", IsRequired = false, DataType = "double", ParameterType = "query", Verb = "GET")]
  99. public float? MaxFramerate { get; set; }
  100. [ApiMember(Name = "CopyTimestamps", Description = "Whether or not to copy timestamps when transcoding with an offset. Defaults to false.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
  101. public bool CopyTimestamps { get; set; }
  102. /// <summary>
  103. /// Gets or sets the start time ticks.
  104. /// </summary>
  105. /// <value>The start time ticks.</value>
  106. [ApiMember(Name = "StartTimeTicks", Description = "Optional. Specify a starting offset, in ticks. 1 tick = 10000 ms", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  107. public long? StartTimeTicks { get; set; }
  108. /// <summary>
  109. /// Gets or sets the width.
  110. /// </summary>
  111. /// <value>The width.</value>
  112. [ApiMember(Name = "Width", Description = "Optional. The fixed horizontal resolution of the encoded video.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  113. public int? Width { get; set; }
  114. /// <summary>
  115. /// Gets or sets the height.
  116. /// </summary>
  117. /// <value>The height.</value>
  118. [ApiMember(Name = "Height", Description = "Optional. The fixed vertical resolution of the encoded video.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  119. public int? Height { get; set; }
  120. /// <summary>
  121. /// Gets or sets the width of the max.
  122. /// </summary>
  123. /// <value>The width of the max.</value>
  124. [ApiMember(Name = "MaxWidth", Description = "Optional. The maximum horizontal resolution of the encoded video.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  125. public int? MaxWidth { get; set; }
  126. /// <summary>
  127. /// Gets or sets the height of the max.
  128. /// </summary>
  129. /// <value>The height of the max.</value>
  130. [ApiMember(Name = "MaxHeight", Description = "Optional. The maximum vertical resolution of the encoded video.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  131. public int? MaxHeight { get; set; }
  132. /// <summary>
  133. /// Gets or sets the video bit rate.
  134. /// </summary>
  135. /// <value>The video bit rate.</value>
  136. [ApiMember(Name = "VideoBitRate", Description = "Optional. Specify a video bitrate to encode to, e.g. 500000. If omitted this will be left to encoder defaults.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  137. public int? VideoBitRate { get; set; }
  138. /// <summary>
  139. /// Gets or sets the index of the subtitle stream.
  140. /// </summary>
  141. /// <value>The index of the subtitle stream.</value>
  142. [ApiMember(Name = "SubtitleStreamIndex", Description = "Optional. The index of the subtitle stream to use. If omitted no subtitles will be used.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  143. public int? SubtitleStreamIndex { get; set; }
  144. [ApiMember(Name = "SubtitleMethod", Description = "Optional. Specify the subtitle delivery method.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  145. public SubtitleDeliveryMethod SubtitleMethod { get; set; }
  146. [ApiMember(Name = "MaxRefFrames", Description = "Optional.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  147. public int? MaxRefFrames { get; set; }
  148. [ApiMember(Name = "MaxVideoBitDepth", Description = "Optional.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  149. public int? MaxVideoBitDepth { get; set; }
  150. public bool RequireAvc { get; set; }
  151. public bool DeInterlace { get; set; }
  152. public bool RequireNonAnamorphic { get; set; }
  153. public int? TranscodingMaxAudioChannels { get; set; }
  154. public int? CpuCoreLimit { get; set; }
  155. public string OutputContainer { get; set; }
  156. public string LiveStreamId { get; set; }
  157. public bool EnableMpegtsM2TsMode { get; set; }
  158. /// <summary>
  159. /// Gets or sets the video codec.
  160. /// </summary>
  161. /// <value>The video codec.</value>
  162. [ApiMember(Name = "VideoCodec", Description = "Optional. Specify a video codec to encode to, e.g. h264. If omitted the server will auto-select using the url's extension. Options: h264, mpeg4, theora, vpx, wmv.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  163. public string VideoCodec { get; set; }
  164. public string SubtitleCodec { get; set; }
  165. public string TranscodeReasons { get; set; }
  166. /// <summary>
  167. /// Gets or sets the index of the audio stream.
  168. /// </summary>
  169. /// <value>The index of the audio stream.</value>
  170. [ApiMember(Name = "AudioStreamIndex", Description = "Optional. The index of the audio stream to use. If omitted the first audio stream will be used.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  171. public int? AudioStreamIndex { get; set; }
  172. /// <summary>
  173. /// Gets or sets the index of the video stream.
  174. /// </summary>
  175. /// <value>The index of the video stream.</value>
  176. [ApiMember(Name = "VideoStreamIndex", Description = "Optional. The index of the video stream to use. If omitted the first video stream will be used.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  177. public int? VideoStreamIndex { get; set; }
  178. public EncodingContext Context { get; set; }
  179. public Dictionary<string, string> StreamOptions { get; set; }
  180. public string GetOption(string qualifier, string name)
  181. {
  182. var value = GetOption(qualifier + "-" + name);
  183. if (string.IsNullOrEmpty(value))
  184. {
  185. value = GetOption(name);
  186. }
  187. return value;
  188. }
  189. public string GetOption(string name)
  190. {
  191. if (StreamOptions.TryGetValue(name, out var value))
  192. {
  193. return value;
  194. }
  195. return null;
  196. }
  197. public BaseEncodingJobOptions()
  198. {
  199. EnableAutoStreamCopy = true;
  200. AllowVideoStreamCopy = true;
  201. AllowAudioStreamCopy = true;
  202. Context = EncodingContext.Streaming;
  203. StreamOptions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  204. }
  205. public string TempDirectory { get; set; }
  206. public string DeviceId { get; set; }
  207. public Guid Id { get; set; }
  208. public string Container { get; set; }
  209. public DeviceProfile DeviceProfile { get; set; }
  210. }
  211. }