VideoHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Model.Entities;
  5. namespace MediaBrowser.Api.HttpHandlers
  6. {
  7. class VideoHandler : BaseMediaHandler<Video>
  8. {
  9. private IEnumerable<string> UnsupportedOutputFormats = new string[] { "mp4" };
  10. public IEnumerable<string> VideoFormats
  11. {
  12. get
  13. {
  14. return QueryString["videoformats"].Split(',');
  15. }
  16. }
  17. /// <summary>
  18. /// Gets the format we'll be converting to
  19. /// </summary>
  20. protected override string GetOutputFormat()
  21. {
  22. return VideoFormats.First(f => !UnsupportedOutputFormats.Any(s => s.Equals(f, StringComparison.OrdinalIgnoreCase)));
  23. }
  24. protected override bool RequiresConversion()
  25. {
  26. // If it's not in a format we can output to, return true
  27. if (UnsupportedOutputFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
  28. {
  29. return true;
  30. }
  31. // If it's not in a format the consumer accepts, return true
  32. if (!VideoFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
  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. private string GetFFMpegOutputFormat(string outputFormat)
  49. {
  50. if (outputFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase))
  51. {
  52. return "matroska";
  53. }
  54. return outputFormat;
  55. }
  56. private int GetOutputAudioStreamIndex(string outputFormat)
  57. {
  58. return 0;
  59. }
  60. /// <summary>
  61. /// Creates arguments to pass to ffmpeg
  62. /// </summary>
  63. protected override string GetCommandLineArguments()
  64. {
  65. List<string> audioTranscodeParams = new List<string>();
  66. string outputFormat = GetOutputFormat();
  67. int audioStreamIndex = GetOutputAudioStreamIndex(outputFormat);
  68. List<string> maps = new List<string>();
  69. // Add the video stream
  70. maps.Add("-map 0:0");
  71. // Add the audio stream
  72. if (audioStreamIndex != -1)
  73. {
  74. maps.Add("-map 0:" + (1 + audioStreamIndex));
  75. }
  76. // Add all the subtitle streams
  77. for (int i = 0; i < LibraryItem.Subtitles.Count(); i++)
  78. {
  79. maps.Add("-map 0:" + (1 + LibraryItem.AudioStreams.Count() + i));
  80. }
  81. return string.Format("-i \"{0}\" {1} {2} {3} -f {4} -",
  82. LibraryItem.Path,
  83. string.Join(" ", maps.ToArray()),
  84. GetVideoArguments(),
  85. GetAudioArguments(),
  86. GetFFMpegOutputFormat(outputFormat)
  87. );
  88. }
  89. private string GetVideoArguments()
  90. {
  91. return "-c:v copy";
  92. }
  93. private string GetAudioArguments()
  94. {
  95. return "-c:a copy";
  96. }
  97. private string GetSubtitleArguments()
  98. {
  99. string args = "";
  100. for (int i = 0; i < LibraryItem.Subtitles.Count(); i++)
  101. {
  102. if (i > 0)
  103. {
  104. args += " ";
  105. }
  106. args += "-c:s copy";
  107. }
  108. return args;
  109. }
  110. }
  111. }