AudioHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. public class AudioHandler : BaseMediaHandler<Audio>
  9. {
  10. /// <summary>
  11. /// Supported values: mp3,flac,ogg,wav,asf
  12. /// </summary>
  13. public IEnumerable<string> AudioFormats
  14. {
  15. get
  16. {
  17. string val = QueryString["audioformats"];
  18. if (string.IsNullOrEmpty(val))
  19. {
  20. return new string[] { "mp3" };
  21. }
  22. return val.Split(',');
  23. }
  24. }
  25. public IEnumerable<int> AudioBitRates
  26. {
  27. get
  28. {
  29. string val = QueryString["audiobitrates"];
  30. if (string.IsNullOrEmpty(val))
  31. {
  32. return new int[] { };
  33. }
  34. return val.Split(',').Select(v => int.Parse(v));
  35. }
  36. }
  37. private int? GetMaxAcceptedBitRate(string audioFormat)
  38. {
  39. if (!AudioBitRates.Any())
  40. {
  41. return null;
  42. }
  43. int index = AudioFormats.ToList().IndexOf(audioFormat);
  44. return AudioBitRates.ElementAt(index);
  45. }
  46. /// <summary>
  47. /// Determines whether or not the original file requires transcoding
  48. /// </summary>
  49. protected override bool RequiresConversion()
  50. {
  51. string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
  52. // If it's not in a format the consumer accepts, return true
  53. if (!AudioFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
  54. {
  55. return true;
  56. }
  57. int? bitrate = GetMaxAcceptedBitRate(currentFormat);
  58. // If the bitrate is greater than our desired bitrate, we need to transcode
  59. if (bitrate.HasValue && bitrate.Value < LibraryItem.BitRate)
  60. {
  61. return true;
  62. }
  63. // If the number of channels is greater than our desired channels, we need to transcode
  64. if (AudioChannels.HasValue && AudioChannels.Value < LibraryItem.Channels)
  65. {
  66. return true;
  67. }
  68. // If the sample rate is greater than our desired sample rate, we need to transcode
  69. if (AudioSampleRate.HasValue && AudioSampleRate.Value < LibraryItem.SampleRate)
  70. {
  71. return true;
  72. }
  73. // Yay
  74. return false;
  75. }
  76. /// <summary>
  77. /// Gets the format we'll be converting to
  78. /// </summary>
  79. protected override string GetOutputFormat()
  80. {
  81. return AudioFormats.First();
  82. }
  83. /// <summary>
  84. /// Creates arguments to pass to ffmpeg
  85. /// </summary>
  86. protected override string GetCommandLineArguments()
  87. {
  88. List<string> audioTranscodeParams = new List<string>();
  89. string outputFormat = GetOutputFormat();
  90. int? bitrate = GetMaxAcceptedBitRate(outputFormat);
  91. if (bitrate.HasValue)
  92. {
  93. audioTranscodeParams.Add("-ab " + bitrate.Value);
  94. }
  95. if (AudioChannels.HasValue)
  96. {
  97. audioTranscodeParams.Add("-ac " + AudioChannels.Value);
  98. }
  99. if (AudioSampleRate.HasValue)
  100. {
  101. audioTranscodeParams.Add("-ar " + AudioSampleRate.Value);
  102. }
  103. audioTranscodeParams.Add("-f " + outputFormat);
  104. return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
  105. }
  106. }
  107. }