VideoService.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller;
  3. using MediaBrowser.Controller.Entities;
  4. using System;
  5. using MediaBrowser.Controller.Library;
  6. using ServiceStack.ServiceHost;
  7. namespace MediaBrowser.Api.Playback.Progressive
  8. {
  9. /// <summary>
  10. /// Class GetAudioStream
  11. /// </summary>
  12. [Route("/Videos/{Id}/stream.ts", "GET")]
  13. [Route("/Videos/{Id}/stream.webm", "GET")]
  14. [Route("/Videos/{Id}/stream.asf", "GET")]
  15. [Route("/Videos/{Id}/stream.wmv", "GET")]
  16. [Route("/Videos/{Id}/stream.ogv", "GET")]
  17. [Route("/Videos/{Id}/stream.mp4", "GET")]
  18. [Route("/Videos/{Id}/stream.m4v", "GET")]
  19. [Route("/Videos/{Id}/stream.mkv", "GET")]
  20. [Route("/Videos/{Id}/stream.mpeg", "GET")]
  21. [Route("/Videos/{Id}/stream.avi", "GET")]
  22. [Route("/Videos/{Id}/stream.m2ts", "GET")]
  23. [Route("/Videos/{Id}/stream", "GET")]
  24. [ServiceStack.ServiceHost.Api(Description = "Gets a video stream")]
  25. public class GetVideoStream : VideoStreamRequest
  26. {
  27. }
  28. /// <summary>
  29. /// Class VideoService
  30. /// </summary>
  31. public class VideoService : BaseProgressiveStreamingService
  32. {
  33. public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager)
  34. : base(appPaths, userManager, libraryManager, isoManager)
  35. {
  36. }
  37. /// <summary>
  38. /// Gets the specified request.
  39. /// </summary>
  40. /// <param name="request">The request.</param>
  41. /// <returns>System.Object.</returns>
  42. public object Get(GetVideoStream request)
  43. {
  44. return ProcessRequest(request);
  45. }
  46. /// <summary>
  47. /// Gets the command line arguments.
  48. /// </summary>
  49. /// <param name="outputPath">The output path.</param>
  50. /// <param name="state">The state.</param>
  51. /// <returns>System.String.</returns>
  52. protected override string GetCommandLineArguments(string outputPath, StreamState state)
  53. {
  54. var video = (Video)state.Item;
  55. var probeSize = Kernel.Instance.FFMpegManager.GetProbeSizeArgument(video.VideoType, video.IsoType);
  56. // Get the output codec name
  57. var videoCodec = GetVideoCodec(state.VideoRequest);
  58. var graphicalSubtitleParam = string.Empty;
  59. if (state.SubtitleStream != null)
  60. {
  61. // This is for internal graphical subs
  62. if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
  63. {
  64. graphicalSubtitleParam = GetInternalGraphicalSubtitleParam(state, videoCodec);
  65. }
  66. }
  67. var format = string.Empty;
  68. if (string.Equals("wmv2", videoCodec, StringComparison.OrdinalIgnoreCase))
  69. {
  70. format = " -f asf ";
  71. }
  72. return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5}{6} {7}{8} \"{9}\"",
  73. probeSize,
  74. GetFastSeekCommandLineParameter(state.Request),
  75. GetInputArgument(video, state.IsoMount),
  76. GetSlowSeekCommandLineParameter(state.Request),
  77. GetMapArgs(state),
  78. GetVideoArguments(state, videoCodec),
  79. graphicalSubtitleParam,
  80. GetAudioArguments(state),
  81. format,
  82. outputPath
  83. ).Trim();
  84. }
  85. /// <summary>
  86. /// Gets video arguments to pass to ffmpeg
  87. /// </summary>
  88. /// <param name="state">The state.</param>
  89. /// <param name="videoCodec">The video codec.</param>
  90. /// <returns>System.String.</returns>
  91. private string GetVideoArguments(StreamState state, string videoCodec)
  92. {
  93. var args = "-vcodec " + videoCodec;
  94. var request = state.VideoRequest;
  95. // If we're encoding video, add additional params
  96. if (!videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  97. {
  98. // Add resolution params, if specified
  99. if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
  100. {
  101. args += GetOutputSizeParam(state, videoCodec);
  102. }
  103. if (request.Framerate.HasValue)
  104. {
  105. args += string.Format(" -r {0}", request.Framerate.Value);
  106. }
  107. // Add the audio bitrate
  108. var qualityParam = GetVideoQualityParam(request, videoCodec);
  109. if (!string.IsNullOrEmpty(qualityParam))
  110. {
  111. args += " " + qualityParam;
  112. }
  113. }
  114. else if (IsH264(state.VideoStream))
  115. {
  116. args += " -bsf h264_mp4toannexb";
  117. }
  118. return args;
  119. }
  120. /// <summary>
  121. /// Gets audio arguments to pass to ffmpeg
  122. /// </summary>
  123. /// <param name="state">The state.</param>
  124. /// <returns>System.String.</returns>
  125. private string GetAudioArguments(StreamState state)
  126. {
  127. // If the video doesn't have an audio stream, return a default.
  128. if (state.AudioStream == null)
  129. {
  130. return string.Empty;
  131. }
  132. var request = state.Request;
  133. // Get the output codec name
  134. var codec = GetAudioCodec(request);
  135. var args = "-acodec " + codec;
  136. // If we're encoding audio, add additional params
  137. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  138. {
  139. // Add the number of audio channels
  140. var channels = GetNumAudioChannelsParam(request, state.AudioStream);
  141. if (channels.HasValue)
  142. {
  143. args += " -ac " + channels.Value;
  144. }
  145. if (request.AudioSampleRate.HasValue)
  146. {
  147. args += " -ar " + request.AudioSampleRate.Value;
  148. }
  149. if (request.AudioBitRate.HasValue)
  150. {
  151. args += " -ab " + request.AudioBitRate.Value;
  152. }
  153. }
  154. return args;
  155. }
  156. /// <summary>
  157. /// Gets the video bitrate to specify on the command line
  158. /// </summary>
  159. /// <param name="request">The request.</param>
  160. /// <param name="videoCodec">The video codec.</param>
  161. /// <returns>System.String.</returns>
  162. private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec)
  163. {
  164. var args = string.Empty;
  165. // webm
  166. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  167. {
  168. args = "-g 120 -cpu-used 1 -lag-in-frames 16 -deadline realtime -slices 4 -vprofile 0";
  169. }
  170. // asf/wmv
  171. else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  172. {
  173. args = "-g 100 -qmax 15";
  174. }
  175. else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  176. {
  177. args = "-preset superfast";
  178. }
  179. if (request.VideoBitRate.HasValue)
  180. {
  181. args += " -b:v " + request.VideoBitRate;
  182. }
  183. return args.Trim();
  184. }
  185. }
  186. }