VideoService.cs 7.4 KB

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