VideoHandler.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using MediaBrowser.Model.Entities;
  6. namespace MediaBrowser.Api.HttpHandlers
  7. {
  8. /// <summary>
  9. /// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
  10. /// </summary>
  11. class VideoHandler : BaseMediaHandler<Video>
  12. {
  13. /// <summary>
  14. /// We can output these files directly, but we can't encode them
  15. /// </summary>
  16. protected override IEnumerable<string> UnsupportedOutputEncodingFormats
  17. {
  18. get
  19. {
  20. return new string[] { "mp4", "wmv", "3gp", "avi", "ogv", "mov", "m4v", "mkv" };
  21. }
  22. }
  23. protected override bool RequiresConversion()
  24. {
  25. string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
  26. // For now we won't allow these to pass through.
  27. // Later we'll add some intelligence to allow it when possible
  28. if (currentFormat.Equals("mp4", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("m4v", StringComparison.OrdinalIgnoreCase))
  29. {
  30. return true;
  31. }
  32. if (base.RequiresConversion())
  33. {
  34. return true;
  35. }
  36. AudioStream audio = LibraryItem.AudioStreams.FirstOrDefault();
  37. if (audio != null)
  38. {
  39. // If the number of channels is greater than our desired channels, we need to transcode
  40. if (AudioChannels.HasValue && AudioChannels.Value < audio.Channels)
  41. {
  42. return true;
  43. }
  44. }
  45. // Yay
  46. return false;
  47. }
  48. /// <summary>
  49. /// Translates the file extension to the format param that follows "-f" on the ffmpeg command line
  50. /// </summary>
  51. private string GetFFMpegOutputFormat(string outputFormat)
  52. {
  53. if (outputFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase))
  54. {
  55. return "matroska";
  56. }
  57. else if (outputFormat.Equals("ts", StringComparison.OrdinalIgnoreCase))
  58. {
  59. return "mpegts";
  60. }
  61. else if (outputFormat.Equals("ogv", StringComparison.OrdinalIgnoreCase))
  62. {
  63. return "ogg";
  64. }
  65. return outputFormat;
  66. }
  67. /// <summary>
  68. /// Creates arguments to pass to ffmpeg
  69. /// </summary>
  70. protected override string GetCommandLineArguments()
  71. {
  72. List<string> audioTranscodeParams = new List<string>();
  73. string outputFormat = GetConversionOutputFormat();
  74. return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
  75. LibraryItem.Path,
  76. GetVideoArguments(outputFormat),
  77. GetAudioArguments(outputFormat),
  78. GetFFMpegOutputFormat(outputFormat)
  79. );
  80. }
  81. private string GetVideoArguments(string outputFormat)
  82. {
  83. string codec = GetVideoCodec(outputFormat);
  84. string args = "-vcodec " + codec;
  85. return args;
  86. }
  87. private string GetAudioArguments(string outputFormat)
  88. {
  89. AudioStream audioStream = LibraryItem.AudioStreams.FirstOrDefault();
  90. if (audioStream == null)
  91. {
  92. return string.Empty;
  93. }
  94. string codec = GetAudioCodec(audioStream, outputFormat);
  95. string args = "-acodec " + codec;
  96. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  97. {
  98. int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
  99. if (channels.HasValue)
  100. {
  101. args += " -ac " + channels.Value;
  102. }
  103. int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
  104. if (sampleRate.HasValue)
  105. {
  106. args += " -ar " + sampleRate.Value;
  107. }
  108. }
  109. return args;
  110. }
  111. private string GetVideoCodec(string outputFormat)
  112. {
  113. if (outputFormat.Equals("webm"))
  114. {
  115. // Per webm specification, it must be vpx
  116. return "libvpx";
  117. }
  118. else if (outputFormat.Equals("asf"))
  119. {
  120. return "wmv2";
  121. }
  122. return "libx264";
  123. }
  124. private string GetAudioCodec(AudioStream audioStream, string outputFormat)
  125. {
  126. if (outputFormat.Equals("webm"))
  127. {
  128. // Per webm specification, it must be vorbis
  129. return "libvorbis";
  130. }
  131. // See if we can just copy the stream
  132. if (HasBasicAudio(audioStream))
  133. {
  134. return "copy";
  135. }
  136. return "libvo_aacenc";
  137. }
  138. private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
  139. {
  140. if (libraryItemChannels > 2 && audioCodec.Equals("libvo_aacenc"))
  141. {
  142. // libvo_aacenc currently only supports two channel output
  143. return 2;
  144. }
  145. return GetNumAudioChannelsParam(libraryItemChannels);
  146. }
  147. private bool HasBasicAudio(AudioStream audio)
  148. {
  149. int maxChannels = AudioChannels ?? 2;
  150. if (audio.Channels > maxChannels)
  151. {
  152. return false;
  153. }
  154. if (audio.AudioFormat.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
  155. {
  156. return true;
  157. }
  158. if (audio.AudioFormat.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
  159. {
  160. return true;
  161. }
  162. if (audio.AudioFormat.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
  163. {
  164. return true;
  165. }
  166. return false;
  167. }
  168. }
  169. }