Moved more common code from audio/video handler down to the base class
This commit is contained in:
parent
537b3553b8
commit
6fe8296266
|
@ -1,28 +1,36 @@
|
||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using MediaBrowser.Model.Entities;
|
using MediaBrowser.Model.Entities;
|
||||||
|
|
||||||
namespace MediaBrowser.Api.HttpHandlers
|
namespace MediaBrowser.Api.HttpHandlers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Supported output formats are: mp3,flac,ogg,wav,asf,wma,aac
|
||||||
|
/// </summary>
|
||||||
public class AudioHandler : BaseMediaHandler<Audio>
|
public class AudioHandler : BaseMediaHandler<Audio>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Supported values: mp3,flac,ogg,wav,asf
|
/// Overriding to provide mp3 as a default, since pretty much every device supports it
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<string> AudioFormats
|
protected override IEnumerable<string> OutputFormats
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
string val = QueryString["audioformats"];
|
IEnumerable<string> vals = base.OutputFormats;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(val))
|
return vals.Any() ? vals : new string[] { "mp3" };
|
||||||
{
|
}
|
||||||
return new string[] { "mp3" };
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return val.Split(',');
|
/// <summary>
|
||||||
|
/// We can output these files directly, but we can't encode them
|
||||||
|
/// </summary>
|
||||||
|
protected override IEnumerable<string> UnsupportedOutputEncodingFormats
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return new string[] { "wma", "aac" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,7 +56,7 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
int index = AudioFormats.ToList().IndexOf(audioFormat);
|
int index = OutputFormats.ToList().IndexOf(audioFormat);
|
||||||
|
|
||||||
return AudioBitRates.ElementAt(index);
|
return AudioBitRates.ElementAt(index);
|
||||||
}
|
}
|
||||||
|
@ -58,14 +66,13 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected override bool RequiresConversion()
|
protected override bool RequiresConversion()
|
||||||
{
|
{
|
||||||
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
|
if (base.RequiresConversion())
|
||||||
|
|
||||||
// If it's not in a format the consumer accepts, return true
|
|
||||||
if (!AudioFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
|
||||||
|
|
||||||
int? bitrate = GetMaxAcceptedBitRate(currentFormat);
|
int? bitrate = GetMaxAcceptedBitRate(currentFormat);
|
||||||
|
|
||||||
// If the bitrate is greater than our desired bitrate, we need to transcode
|
// If the bitrate is greater than our desired bitrate, we need to transcode
|
||||||
|
@ -90,14 +97,6 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the format we'll be converting to
|
|
||||||
/// </summary>
|
|
||||||
protected override string GetOutputFormat()
|
|
||||||
{
|
|
||||||
return AudioFormats.First();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates arguments to pass to ffmpeg
|
/// Creates arguments to pass to ffmpeg
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -105,7 +104,7 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
{
|
{
|
||||||
List<string> audioTranscodeParams = new List<string>();
|
List<string> audioTranscodeParams = new List<string>();
|
||||||
|
|
||||||
string outputFormat = GetOutputFormat();
|
string outputFormat = GetConversionOutputFormat();
|
||||||
|
|
||||||
int? bitrate = GetMaxAcceptedBitRate(outputFormat);
|
int? bitrate = GetMaxAcceptedBitRate(outputFormat);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using MediaBrowser.Common.Configuration;
|
using MediaBrowser.Common.Configuration;
|
||||||
|
@ -15,6 +17,28 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
public abstract class BaseMediaHandler<T> : BaseHandler
|
public abstract class BaseMediaHandler<T> : BaseHandler
|
||||||
where T : BaseItem, new()
|
where T : BaseItem, new()
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Supported values: mp3,flac,ogg,wav,asf,wma,aac
|
||||||
|
/// </summary>
|
||||||
|
protected virtual IEnumerable<string> OutputFormats
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return QueryString["outputformats"].Split(',');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// These formats can be outputted directly but cannot be encoded to
|
||||||
|
/// </summary>
|
||||||
|
protected virtual IEnumerable<string> UnsupportedOutputEncodingFormats
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
return new string[] { };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private T _LibraryItem;
|
private T _LibraryItem;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the library item that will be played, if any
|
/// Gets the library item that will be played, if any
|
||||||
|
@ -71,7 +95,7 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return MimeTypes.GetMimeType("." + GetOutputFormat());
|
return MimeTypes.GetMimeType("." + GetConversionOutputFormat());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,8 +121,35 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract string GetCommandLineArguments();
|
protected abstract string GetCommandLineArguments();
|
||||||
protected abstract string GetOutputFormat();
|
|
||||||
protected abstract bool RequiresConversion();
|
/// <summary>
|
||||||
|
/// Gets the format we'll be converting to
|
||||||
|
/// </summary>
|
||||||
|
protected virtual string GetConversionOutputFormat()
|
||||||
|
{
|
||||||
|
return OutputFormats.First(f => !UnsupportedOutputEncodingFormats.Any(s => s.Equals(f, StringComparison.OrdinalIgnoreCase)));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected virtual bool RequiresConversion()
|
||||||
|
{
|
||||||
|
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
|
||||||
|
|
||||||
|
if (OutputFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
// We can output these files directly, but we can't encode them
|
||||||
|
if (UnsupportedOutputEncodingFormats.Any(f => currentFormat.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// If it's not in a format the consumer accepts, return true
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
protected async override Task WriteResponseToOutputStream(Stream stream)
|
protected async override Task WriteResponseToOutputStream(Stream stream)
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,36 +5,25 @@ using MediaBrowser.Model.Entities;
|
||||||
|
|
||||||
namespace MediaBrowser.Api.HttpHandlers
|
namespace MediaBrowser.Api.HttpHandlers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
|
||||||
|
/// </summary>
|
||||||
class VideoHandler : BaseMediaHandler<Video>
|
class VideoHandler : BaseMediaHandler<Video>
|
||||||
{
|
{
|
||||||
private IEnumerable<string> UnsupportedOutputFormats = new string[] { "mp4" };
|
/// <summary>
|
||||||
|
/// We can output these files directly, but we can't encode them
|
||||||
public IEnumerable<string> VideoFormats
|
/// </summary>
|
||||||
|
protected override IEnumerable<string> UnsupportedOutputEncodingFormats
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
return QueryString["videoformats"].Split(',');
|
return new string[] { "mp4", "wmv" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets the format we'll be converting to
|
|
||||||
/// </summary>
|
|
||||||
protected override string GetOutputFormat()
|
|
||||||
{
|
|
||||||
return VideoFormats.First(f => !UnsupportedOutputFormats.Any(s => s.Equals(f, StringComparison.OrdinalIgnoreCase)));
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override bool RequiresConversion()
|
protected override bool RequiresConversion()
|
||||||
{
|
{
|
||||||
// If it's not in a format we can output to, return true
|
if (base.RequiresConversion())
|
||||||
if (UnsupportedOutputFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it's not in a format the consumer accepts, return true
|
|
||||||
if (!VideoFormats.Any(f => LibraryItem.Path.EndsWith(f, StringComparison.OrdinalIgnoreCase)))
|
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -60,15 +49,18 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
{
|
{
|
||||||
return "matroska";
|
return "matroska";
|
||||||
}
|
}
|
||||||
|
else if (outputFormat.Equals("ts", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return "mpegts";
|
||||||
|
}
|
||||||
|
else if (outputFormat.Equals("ogv", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
return "ogg";
|
||||||
|
}
|
||||||
|
|
||||||
return outputFormat;
|
return outputFormat;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int GetOutputAudioStreamIndex(string outputFormat)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates arguments to pass to ffmpeg
|
/// Creates arguments to pass to ffmpeg
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -76,31 +68,10 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
{
|
{
|
||||||
List<string> audioTranscodeParams = new List<string>();
|
List<string> audioTranscodeParams = new List<string>();
|
||||||
|
|
||||||
string outputFormat = GetOutputFormat();
|
string outputFormat = GetConversionOutputFormat();
|
||||||
|
|
||||||
int audioStreamIndex = GetOutputAudioStreamIndex(outputFormat);
|
return string.Format("-i \"{0}\" {1} {2} -f {3} -",
|
||||||
|
|
||||||
List<string> maps = new List<string>();
|
|
||||||
|
|
||||||
// Add the video stream
|
|
||||||
maps.Add("-map 0:0");
|
|
||||||
|
|
||||||
// Add the audio stream
|
|
||||||
if (audioStreamIndex != -1)
|
|
||||||
{
|
|
||||||
maps.Add("-map 0:" + (1 + audioStreamIndex));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add all the subtitle streams
|
|
||||||
for (int i = 0; i < LibraryItem.Subtitles.Count(); i++)
|
|
||||||
{
|
|
||||||
maps.Add("-map 0:" + (1 + LibraryItem.AudioStreams.Count() + i));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return string.Format("-i \"{0}\" {1} {2} {3} -f {4} -",
|
|
||||||
LibraryItem.Path,
|
LibraryItem.Path,
|
||||||
string.Join(" ", maps.ToArray()),
|
|
||||||
GetVideoArguments(),
|
GetVideoArguments(),
|
||||||
GetAudioArguments(),
|
GetAudioArguments(),
|
||||||
GetFFMpegOutputFormat(outputFormat)
|
GetFFMpegOutputFormat(outputFormat)
|
||||||
|
@ -109,29 +80,12 @@ namespace MediaBrowser.Api.HttpHandlers
|
||||||
|
|
||||||
private string GetVideoArguments()
|
private string GetVideoArguments()
|
||||||
{
|
{
|
||||||
return "-c:v copy";
|
return "-vcodec copy";
|
||||||
}
|
}
|
||||||
|
|
||||||
private string GetAudioArguments()
|
private string GetAudioArguments()
|
||||||
{
|
{
|
||||||
return "-c:a copy";
|
return "-acodec copy";
|
||||||
}
|
|
||||||
|
|
||||||
private string GetSubtitleArguments()
|
|
||||||
{
|
|
||||||
string args = "";
|
|
||||||
|
|
||||||
for (int i = 0; i < LibraryItem.Subtitles.Count(); i++)
|
|
||||||
{
|
|
||||||
if (i > 0)
|
|
||||||
{
|
|
||||||
args += " ";
|
|
||||||
}
|
|
||||||
args += "-c:s copy";
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return args;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user