VideoService.cs 9.1 KB

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