VideoService.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using System.IO;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.MediaInfo;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Entities;
  6. using System;
  7. using MediaBrowser.Controller.Library;
  8. using ServiceStack.ServiceHost;
  9. namespace MediaBrowser.Api.Playback.Progressive
  10. {
  11. /// <summary>
  12. /// Class GetAudioStream
  13. /// </summary>
  14. [Route("/Videos/{Id}/stream.ts", "GET")]
  15. [Route("/Videos/{Id}/stream.webm", "GET")]
  16. [Route("/Videos/{Id}/stream.asf", "GET")]
  17. [Route("/Videos/{Id}/stream.wmv", "GET")]
  18. [Route("/Videos/{Id}/stream.ogv", "GET")]
  19. [Route("/Videos/{Id}/stream.mp4", "GET")]
  20. [Route("/Videos/{Id}/stream.m4v", "GET")]
  21. [Route("/Videos/{Id}/stream.mkv", "GET")]
  22. [Route("/Videos/{Id}/stream.mpeg", "GET")]
  23. [Route("/Videos/{Id}/stream.avi", "GET")]
  24. [Route("/Videos/{Id}/stream.m2ts", "GET")]
  25. [Route("/Videos/{Id}/stream.3gp", "GET")]
  26. [Route("/Videos/{Id}/stream.wmv", "GET")]
  27. [Route("/Videos/{Id}/stream", "GET")]
  28. [Route("/Videos/{Id}/stream.ts", "HEAD")]
  29. [Route("/Videos/{Id}/stream.webm", "HEAD")]
  30. [Route("/Videos/{Id}/stream.asf", "HEAD")]
  31. [Route("/Videos/{Id}/stream.wmv", "HEAD")]
  32. [Route("/Videos/{Id}/stream.ogv", "HEAD")]
  33. [Route("/Videos/{Id}/stream.mp4", "HEAD")]
  34. [Route("/Videos/{Id}/stream.m4v", "HEAD")]
  35. [Route("/Videos/{Id}/stream.mkv", "HEAD")]
  36. [Route("/Videos/{Id}/stream.mpeg", "HEAD")]
  37. [Route("/Videos/{Id}/stream.avi", "HEAD")]
  38. [Route("/Videos/{Id}/stream.3gp", "HEAD")]
  39. [Route("/Videos/{Id}/stream.wmv", "HEAD")]
  40. [Route("/Videos/{Id}/stream.m2ts", "HEAD")]
  41. [Route("/Videos/{Id}/stream", "HEAD")]
  42. [Api(Description = "Gets a video stream")]
  43. public class GetVideoStream : VideoStreamRequest
  44. {
  45. }
  46. /// <summary>
  47. /// Class VideoService
  48. /// </summary>
  49. public class VideoService : BaseProgressiveStreamingService
  50. {
  51. public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder)
  52. : base(appPaths, userManager, libraryManager, isoManager, mediaEncoder)
  53. {
  54. }
  55. /// <summary>
  56. /// Gets the specified request.
  57. /// </summary>
  58. /// <param name="request">The request.</param>
  59. /// <returns>System.Object.</returns>
  60. public object Get(GetVideoStream request)
  61. {
  62. return ProcessRequest(request, false);
  63. }
  64. public object Head(GetVideoStream request)
  65. {
  66. return ProcessRequest(request, true);
  67. }
  68. /// <summary>
  69. /// Gets the command line arguments.
  70. /// </summary>
  71. /// <param name="outputPath">The output path.</param>
  72. /// <param name="state">The state.</param>
  73. /// <returns>System.String.</returns>
  74. protected override string GetCommandLineArguments(string outputPath, StreamState state)
  75. {
  76. var video = (Video)state.Item;
  77. var probeSize = GetProbeSizeArgument(state.Item);
  78. // Get the output codec name
  79. var videoCodec = GetVideoCodec(state.VideoRequest);
  80. var format = string.Empty;
  81. var keyFrame = string.Empty;
  82. if (string.Equals(Path.GetExtension(outputPath), ".mp4", StringComparison.OrdinalIgnoreCase))
  83. {
  84. format = " -f mp4 -movflags frag_keyframe+empty_moov";
  85. }
  86. return string.Format("{0} {1} -i {2}{3}{4} -threads 0 {5} {6} {7}{8} \"{9}\"",
  87. probeSize,
  88. GetFastSeekCommandLineParameter(state.Request),
  89. GetInputArgument(video, state.IsoMount),
  90. GetSlowSeekCommandLineParameter(state.Request),
  91. keyFrame,
  92. GetMapArgs(state),
  93. GetVideoArguments(state, videoCodec),
  94. GetAudioArguments(state),
  95. format,
  96. outputPath
  97. ).Trim();
  98. }
  99. /// <summary>
  100. /// Gets video arguments to pass to ffmpeg
  101. /// </summary>
  102. /// <param name="state">The state.</param>
  103. /// <param name="codec">The video codec.</param>
  104. /// <returns>System.String.</returns>
  105. private string GetVideoArguments(StreamState state, string codec)
  106. {
  107. var args = "-vcodec " + codec;
  108. // See if we can save come cpu cycles by avoiding encoding
  109. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  110. {
  111. return IsH264(state.VideoStream) ? args + " -bsf h264_mp4toannexb" : args;
  112. }
  113. const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+2))";
  114. args += keyFrameArg;
  115. var request = state.VideoRequest;
  116. // Add resolution params, if specified
  117. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  118. {
  119. args += GetOutputSizeParam(state, codec);
  120. }
  121. if (request.Framerate.HasValue)
  122. {
  123. args += string.Format(" -r {0}", request.Framerate.Value);
  124. }
  125. // Add the audio bitrate
  126. var qualityParam = GetVideoQualityParam(request, codec);
  127. if (!string.IsNullOrEmpty(qualityParam))
  128. {
  129. args += " " + qualityParam;
  130. }
  131. args += " -vsync vfr";
  132. if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
  133. {
  134. args += " -profile:v " + state.VideoRequest.Profile;
  135. }
  136. if (!string.IsNullOrEmpty(state.VideoRequest.Level))
  137. {
  138. args += " -level " + state.VideoRequest.Level;
  139. }
  140. if (state.SubtitleStream != null)
  141. {
  142. // This is for internal graphical subs
  143. if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
  144. {
  145. args += GetInternalGraphicalSubtitleParam(state, codec);
  146. }
  147. }
  148. return args;
  149. }
  150. /// <summary>
  151. /// Gets audio arguments to pass to ffmpeg
  152. /// </summary>
  153. /// <param name="state">The state.</param>
  154. /// <returns>System.String.</returns>
  155. private string GetAudioArguments(StreamState state)
  156. {
  157. // If the video doesn't have an audio stream, return a default.
  158. if (state.AudioStream == null)
  159. {
  160. return string.Empty;
  161. }
  162. var request = state.Request;
  163. // Get the output codec name
  164. var codec = GetAudioCodec(request);
  165. if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  166. {
  167. return "-acodec copy";
  168. }
  169. var args = "-acodec " + codec;
  170. // Add the number of audio channels
  171. var channels = GetNumAudioChannelsParam(request, state.AudioStream);
  172. if (channels.HasValue)
  173. {
  174. args += " -ac " + channels.Value;
  175. }
  176. if (request.AudioSampleRate.HasValue)
  177. {
  178. args += " -ar " + request.AudioSampleRate.Value;
  179. }
  180. if (request.AudioBitRate.HasValue)
  181. {
  182. args += " -ab " + request.AudioBitRate.Value;
  183. }
  184. var volParam = string.Empty;
  185. // Boost volume to 200% when downsampling from 6ch to 2ch
  186. if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
  187. {
  188. volParam = ",volume=2.000000";
  189. }
  190. args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
  191. return args;
  192. }
  193. /// <summary>
  194. /// Gets the video bitrate to specify on the command line
  195. /// </summary>
  196. /// <param name="request">The request.</param>
  197. /// <param name="videoCodec">The video codec.</param>
  198. /// <returns>System.String.</returns>
  199. private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec)
  200. {
  201. var args = string.Empty;
  202. // webm
  203. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  204. {
  205. args = "-quality realtime -profile:v 1 -slices 4";
  206. }
  207. // asf/wmv
  208. else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  209. {
  210. args = "-g 100 -qmax 15";
  211. }
  212. else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  213. {
  214. args = "-preset superfast";
  215. }
  216. else if (videoCodec.Equals("mpeg4", StringComparison.OrdinalIgnoreCase))
  217. {
  218. args = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
  219. }
  220. if (request.VideoBitRate.HasValue)
  221. {
  222. args += " -b:v " + request.VideoBitRate;
  223. }
  224. return args.Trim();
  225. }
  226. }
  227. }