Added some intelligence to the video handler. try to just copy the audio stream when possible, instead of encoding.

pull/702/head
LukePulverenti Luke Pulverenti luke pulverenti 13 years ago
parent 0a310fab0e
commit 5f5f2fcdb4

@ -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()) + " -";
}
/// <summary>
/// Gets the number of audio channels to specify on the command line
/// </summary>
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;
}
/// <summary>
/// Gets the number of audio channels to specify on the command line
/// </summary>
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;
}
}
}

@ -212,5 +212,41 @@ namespace MediaBrowser.Api.HttpHandlers
process.Dispose();
}
}
/// <summary>
/// Gets the number of audio channels to specify on the command line
/// </summary>
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;
}
/// <summary>
/// Gets the number of audio channels to specify on the command line
/// </summary>
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;
}
}
}

@ -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;
}
}
}

Loading…
Cancel
Save