EncodingJobOptions.cs 13 KB

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