EncodingJobOptions.cs 14 KB

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