VideoService.cs 8.0 KB

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