diff --git a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs index c7adc9a583..f0d86ab37e 100644 --- a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs @@ -113,14 +113,14 @@ namespace MediaBrowser.Api.HttpHandlers audioTranscodeParams.Add("-ab " + bitrate.Value); } - int? channels = GetNumAudioChannelsParam(); + int? channels = GetNumAudioChannelsParam(LibraryItem.Channels); if (channels.HasValue) { audioTranscodeParams.Add("-ac " + channels.Value); } - int? sampleRate = GetSampleRateParam(); + int? sampleRate = GetSampleRateParam(LibraryItem.SampleRate); if (sampleRate.HasValue) { @@ -131,41 +131,5 @@ namespace MediaBrowser.Api.HttpHandlers return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -"; } - - /// - /// Gets the number of audio channels to specify on the command line - /// - private int? GetNumAudioChannelsParam() - { - // If the user requested a max number of channels - if (AudioChannels.HasValue) - { - // Only specify the param if we're going to downmix - if (AudioChannels.Value < LibraryItem.Channels) - { - return AudioChannels.Value; - } - } - - return null; - } - - /// - /// Gets the number of audio channels to specify on the command line - /// - private int? GetSampleRateParam() - { - // If the user requested a max value - if (AudioSampleRate.HasValue) - { - // Only specify the param if we're going to downmix - if (AudioSampleRate.Value < LibraryItem.SampleRate) - { - return AudioSampleRate.Value; - } - } - - return null; - } } } diff --git a/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs b/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs index 890e739506..da397fec93 100644 --- a/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/BaseMediaHandler.cs @@ -212,5 +212,41 @@ namespace MediaBrowser.Api.HttpHandlers process.Dispose(); } } + + /// + /// Gets the number of audio channels to specify on the command line + /// + protected int? GetNumAudioChannelsParam(int libraryItemChannels) + { + // If the user requested a max number of channels + if (AudioChannels.HasValue) + { + // Only specify the param if we're going to downmix + if (AudioChannels.Value < libraryItemChannels) + { + return AudioChannels.Value; + } + } + + return null; + } + + /// + /// Gets the number of audio channels to specify on the command line + /// + protected int? GetSampleRateParam(int libraryItemSampleRate) + { + // If the user requested a max value + if (AudioSampleRate.HasValue) + { + // Only specify the param if we're going to downmix + if (AudioSampleRate.Value < libraryItemSampleRate) + { + return AudioSampleRate.Value; + } + } + + return null; + } } } diff --git a/MediaBrowser.Api/HttpHandlers/VideoHandler.cs b/MediaBrowser.Api/HttpHandlers/VideoHandler.cs index 4e2c6b0da4..c767335bb8 100644 --- a/MediaBrowser.Api/HttpHandlers/VideoHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/VideoHandler.cs @@ -1,8 +1,8 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using MediaBrowser.Model.Entities; -using System.IO; namespace MediaBrowser.Api.HttpHandlers { @@ -102,21 +102,31 @@ namespace MediaBrowser.Api.HttpHandlers private string GetAudioArguments(string outputFormat) { - string codec = GetAudioCodec(outputFormat); + AudioStream audioStream = LibraryItem.AudioStreams.FirstOrDefault(); + + if (audioStream == null) + { + return string.Empty; + } + + string codec = GetAudioCodec(audioStream, outputFormat); string args = "-acodec " + codec; if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase)) { - int? channels = GetNumAudioChannels(codec); + int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels); if (channels.HasValue) { args += " -ac " + channels.Value; } - if (AudioSampleRate.HasValue) + + int? sampleRate = GetSampleRateParam(audioStream.SampleRate); + + if (sampleRate.HasValue) { - args += " -ar " + AudioSampleRate.Value; + args += " -ar " + sampleRate.Value; } } @@ -131,54 +141,65 @@ namespace MediaBrowser.Api.HttpHandlers // Per webm specification, it must be vpx return "libvpx"; } - else if (outputFormat.Equals("flv")) - { - return "libx264"; - } - else if (outputFormat.Equals("ts")) - { - return "libx264"; - } else if (outputFormat.Equals("asf")) { return "wmv2"; } - return "copy"; + return "libx264"; } - - private string GetAudioCodec(string outputFormat) + + private string GetAudioCodec(AudioStream audioStream, string outputFormat) { if (outputFormat.Equals("webm")) { // Per webm specification, it must be vorbis return "libvorbis"; } - else if (outputFormat.Equals("flv")) - { - return "libvo_aacenc"; - } - else if (outputFormat.Equals("ts")) - { - return "libvo_aacenc"; - } - else if (outputFormat.Equals("asf")) + + // See if we can just copy the stream + if (HasBasicAudio(audioStream)) { - return "libvo_aacenc"; + return "copy"; } - return "copy"; + return "libvo_aacenc"; } - private int? GetNumAudioChannels(string audioCodec) + private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels) { - if (audioCodec.Equals("libvo_aacenc")) + if (libraryItemChannels > 2 && audioCodec.Equals("libvo_aacenc")) { // libvo_aacenc currently only supports two channel output return 2; } - - return AudioChannels; + + return GetNumAudioChannelsParam(libraryItemChannels); + } + + private bool HasBasicAudio(AudioStream audio) + { + int maxChannels = AudioChannels ?? 2; + + if (audio.Channels > maxChannels) + { + return false; + } + + if (audio.AudioFormat.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1) + { + return true; + } + if (audio.AudioFormat.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1) + { + return true; + } + if (audio.AudioFormat.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.AudioFormat.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1) + { + return true; + } + + return false; } } }