VideoHandler.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Model.Entities;
  5. using System.IO;
  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("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. string codec = GetAudioCodec(outputFormat);
  90. string args = "-acodec " + codec;
  91. if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
  92. {
  93. int? channels = GetNumAudioChannels(codec);
  94. if (channels.HasValue)
  95. {
  96. args += " -ac " + channels.Value;
  97. }
  98. if (AudioSampleRate.HasValue)
  99. {
  100. args += " -ar " + AudioSampleRate.Value;
  101. }
  102. }
  103. return args;
  104. }
  105. private string GetVideoCodec(string outputFormat)
  106. {
  107. if (outputFormat.Equals("webm"))
  108. {
  109. // Per webm specification, it must be vpx
  110. return "libvpx";
  111. }
  112. else if (outputFormat.Equals("flv"))
  113. {
  114. return "libx264";
  115. }
  116. else if (outputFormat.Equals("ts"))
  117. {
  118. return "libx264";
  119. }
  120. else if (outputFormat.Equals("asf"))
  121. {
  122. return "wmv2";
  123. }
  124. return "copy";
  125. }
  126. private string GetAudioCodec(string outputFormat)
  127. {
  128. if (outputFormat.Equals("webm"))
  129. {
  130. // Per webm specification, it must be vorbis
  131. return "libvorbis";
  132. }
  133. else if (outputFormat.Equals("flv"))
  134. {
  135. return "libvo_aacenc";
  136. }
  137. else if (outputFormat.Equals("ts"))
  138. {
  139. return "libvo_aacenc";
  140. }
  141. else if (outputFormat.Equals("asf"))
  142. {
  143. return "libvo_aacenc";
  144. }
  145. return "copy";
  146. }
  147. private int? GetNumAudioChannels(string audioCodec)
  148. {
  149. if (audioCodec.Equals("libvo_aacenc"))
  150. {
  151. // libvo_aacenc currently only supports two channel output
  152. return 2;
  153. }
  154. return AudioChannels;
  155. }
  156. }
  157. }