VideoHandler.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System.IO;
  2. using System.Threading;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Common.Net.Handlers;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Resolvers;
  7. using System;
  8. using System.ComponentModel.Composition;
  9. using System.Linq;
  10. using System.Net;
  11. using MediaBrowser.Model.Entities;
  12. namespace MediaBrowser.Api.Streaming
  13. {
  14. /// <summary>
  15. /// Providers a progressive streaming video api
  16. /// </summary>
  17. [Export(typeof(IHttpServerHandler))]
  18. class VideoHandler : BaseProgressiveStreamingHandler<Video>
  19. {
  20. /// <summary>
  21. /// Handleses the request.
  22. /// </summary>
  23. /// <param name="request">The request.</param>
  24. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  25. public override bool HandlesRequest(HttpListenerRequest request)
  26. {
  27. return EntityResolutionHelper.VideoFileExtensions.Any(a => ApiService.IsApiUrlMatch("video" + a, request));
  28. }
  29. /// <summary>
  30. /// Creates arguments to pass to ffmpeg
  31. /// </summary>
  32. /// <param name="outputPath">The output path.</param>
  33. /// <param name="isoMount">The iso mount.</param>
  34. /// <returns>System.String.</returns>
  35. protected override string GetCommandLineArguments(string outputPath, IIsoMount isoMount)
  36. {
  37. var probeSize = Kernel.FFMpegManager.GetProbeSizeArgument(LibraryItem.VideoType, LibraryItem.IsoType);
  38. // Get the output codec name
  39. var videoCodec = GetVideoCodec();
  40. var graphicalSubtitleParam = string.Empty;
  41. if (SubtitleStream != null)
  42. {
  43. // This is for internal graphical subs
  44. if (!SubtitleStream.IsExternal && (SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
  45. {
  46. graphicalSubtitleParam = GetInternalGraphicalSubtitleParam(SubtitleStream, videoCodec);
  47. }
  48. }
  49. return string.Format("{0} {1} -i {2}{3} -threads 0 {4} {5}{6} {7} \"{8}\"",
  50. probeSize,
  51. FastSeekCommandLineParameter,
  52. GetInputArgument(isoMount),
  53. SlowSeekCommandLineParameter,
  54. MapArgs,
  55. GetVideoArguments(videoCodec),
  56. graphicalSubtitleParam,
  57. GetAudioArguments(),
  58. outputPath
  59. ).Trim();
  60. }
  61. /// <summary>
  62. /// Gets video arguments to pass to ffmpeg
  63. /// </summary>
  64. /// <returns>System.String.</returns>
  65. private string GetVideoArguments(string videoCodec)
  66. {
  67. var args = "-vcodec " + videoCodec;
  68. // If we're encoding video, add additional params
  69. if (!videoCodec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  70. {
  71. // Add resolution params, if specified
  72. if (Width.HasValue || Height.HasValue || MaxHeight.HasValue || MaxWidth.HasValue)
  73. {
  74. args += GetOutputSizeParam(videoCodec);
  75. }
  76. if (FrameRate.HasValue)
  77. {
  78. args += string.Format(" -r {0}", FrameRate.Value);
  79. }
  80. // Add the audio bitrate
  81. var qualityParam = GetVideoQualityParam(videoCodec);
  82. if (!string.IsNullOrEmpty(qualityParam))
  83. {
  84. args += " " + qualityParam;
  85. }
  86. }
  87. else if (IsH264(VideoStream))
  88. {
  89. args += " -bsf h264_mp4toannexb";
  90. }
  91. return args;
  92. }
  93. /// <summary>
  94. /// Gets audio arguments to pass to ffmpeg
  95. /// </summary>
  96. /// <returns>System.String.</returns>
  97. private string GetAudioArguments()
  98. {
  99. // If the video doesn't have an audio stream, return a default.
  100. if (AudioStream == null)
  101. {
  102. return string.Empty;
  103. }
  104. // Get the output codec name
  105. var codec = GetAudioCodec();
  106. var args = "-acodec " + codec;
  107. // If we're encoding audio, add additional params
  108. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  109. {
  110. // Add the number of audio channels
  111. var channels = GetNumAudioChannelsParam();
  112. if (channels.HasValue)
  113. {
  114. args += " -ac " + channels.Value;
  115. }
  116. // Add the audio sample rate
  117. var sampleRate = GetSampleRateParam();
  118. if (sampleRate.HasValue)
  119. {
  120. args += " -ar " + sampleRate.Value;
  121. }
  122. if (AudioBitRate.HasValue)
  123. {
  124. args += " -ab " + AudioBitRate.Value;
  125. }
  126. }
  127. return args;
  128. }
  129. /// <summary>
  130. /// Gets the video bitrate to specify on the command line
  131. /// </summary>
  132. /// <param name="videoCodec">The video codec.</param>
  133. /// <returns>System.String.</returns>
  134. private string GetVideoQualityParam(string videoCodec)
  135. {
  136. var args = string.Empty;
  137. // webm
  138. if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
  139. {
  140. args = "-g 120 -cpu-used 1 -lag-in-frames 16 -deadline realtime -slices 4 -vprofile 0";
  141. }
  142. // asf/wmv
  143. else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
  144. {
  145. args = "-g 100 -qmax 15";
  146. }
  147. else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
  148. {
  149. args = "-preset superfast";
  150. }
  151. if (VideoBitRate.HasValue)
  152. {
  153. args += " -b:v " + VideoBitRate;
  154. }
  155. return args.Trim();
  156. }
  157. }
  158. }