VideoService.cs 7.3 KB

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