2012-08-11 20:07:07 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-08-17 15:16:50 +02:00
|
|
|
|
using System.Drawing;
|
2012-08-13 22:18:02 +02:00
|
|
|
|
using System.IO;
|
2012-08-11 20:07:07 +02:00
|
|
|
|
using System.Linq;
|
2012-08-16 04:42:37 +02:00
|
|
|
|
using MediaBrowser.Common.Drawing;
|
2012-08-17 15:16:50 +02:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2012-08-11 20:07:07 +02:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.HttpHandlers
|
|
|
|
|
{
|
2012-08-12 19:05:51 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
|
|
|
|
|
/// </summary>
|
2012-08-11 20:07:07 +02:00
|
|
|
|
class VideoHandler : BaseMediaHandler<Video>
|
|
|
|
|
{
|
2012-08-12 19:05:51 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// We can output these files directly, but we can't encode them
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override IEnumerable<string> UnsupportedOutputEncodingFormats
|
2012-08-11 20:07:07 +02:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2012-08-14 04:24:35 +02:00
|
|
|
|
// mp4, 3gp, mov - muxer does not support non-seekable output
|
|
|
|
|
// avi, mov, mkv, m4v - can't stream these when encoding. the player will try to download them completely before starting playback.
|
|
|
|
|
// wmv - can't seem to figure out the output format name
|
|
|
|
|
return new string[] { "mp4", "3gp", "m4v", "mkv", "avi", "mov", "wmv" };
|
2012-08-11 20:07:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool RequiresConversion()
|
|
|
|
|
{
|
2012-08-13 04:14:39 +02:00
|
|
|
|
string currentFormat = Path.GetExtension(LibraryItem.Path).Replace(".", string.Empty);
|
|
|
|
|
|
|
|
|
|
// For now we won't allow these to pass through.
|
|
|
|
|
// Later we'll add some intelligence to allow it when possible
|
2012-08-13 04:22:34 +02:00
|
|
|
|
if (currentFormat.Equals("mp4", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("m4v", StringComparison.OrdinalIgnoreCase))
|
2012-08-13 04:14:39 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 19:05:51 +02:00
|
|
|
|
if (base.RequiresConversion())
|
2012-08-11 20:07:07 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-08-12 06:03:19 +02:00
|
|
|
|
|
2012-08-16 04:42:37 +02:00
|
|
|
|
if (RequiresVideoConversion())
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 10:22:54 +02:00
|
|
|
|
AudioStream audioStream = (LibraryItem.AudioStreams ?? new AudioStream[] { }).FirstOrDefault();
|
2012-08-11 20:07:07 +02:00
|
|
|
|
|
2012-08-18 10:22:54 +02:00
|
|
|
|
if (audioStream != null)
|
2012-08-11 20:07:07 +02:00
|
|
|
|
{
|
2012-08-18 10:22:54 +02:00
|
|
|
|
if (RequiresAudioConversion(audioStream))
|
2012-08-11 20:07:07 +02:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Yay
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 04:14:39 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Translates the file extension to the format param that follows "-f" on the ffmpeg command line
|
|
|
|
|
/// </summary>
|
2012-08-12 15:59:09 +02:00
|
|
|
|
private string GetFFMpegOutputFormat(string outputFormat)
|
|
|
|
|
{
|
|
|
|
|
if (outputFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "matroska";
|
|
|
|
|
}
|
2012-08-12 19:05:51 +02:00
|
|
|
|
else if (outputFormat.Equals("ts", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "mpegts";
|
|
|
|
|
}
|
|
|
|
|
else if (outputFormat.Equals("ogv", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return "ogg";
|
|
|
|
|
}
|
2012-08-12 15:59:09 +02:00
|
|
|
|
|
|
|
|
|
return outputFormat;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-12 06:03:19 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates arguments to pass to ffmpeg
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override string GetCommandLineArguments()
|
2012-08-11 20:07:07 +02:00
|
|
|
|
{
|
2012-08-12 06:03:19 +02:00
|
|
|
|
List<string> audioTranscodeParams = new List<string>();
|
|
|
|
|
|
2012-08-12 19:05:51 +02:00
|
|
|
|
string outputFormat = GetConversionOutputFormat();
|
2012-08-12 15:59:09 +02:00
|
|
|
|
|
2012-08-13 04:14:39 +02:00
|
|
|
|
return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
|
2012-08-12 15:59:09 +02:00
|
|
|
|
LibraryItem.Path,
|
2012-08-13 04:14:39 +02:00
|
|
|
|
GetVideoArguments(outputFormat),
|
|
|
|
|
GetAudioArguments(outputFormat),
|
2012-08-12 15:59:09 +02:00
|
|
|
|
GetFFMpegOutputFormat(outputFormat)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 04:14:39 +02:00
|
|
|
|
private string GetVideoArguments(string outputFormat)
|
|
|
|
|
{
|
|
|
|
|
string codec = GetVideoCodec(outputFormat);
|
|
|
|
|
|
|
|
|
|
string args = "-vcodec " + codec;
|
|
|
|
|
|
2012-08-16 04:42:37 +02:00
|
|
|
|
if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
if (Width.HasValue || Height.HasValue || MaxHeight.HasValue || MaxWidth.HasValue)
|
|
|
|
|
{
|
|
|
|
|
Size size = DrawingUtils.Resize(LibraryItem.Width, LibraryItem.Height, Width, Height, MaxWidth, MaxHeight);
|
|
|
|
|
|
|
|
|
|
args += string.Format(" -s {0}x{1}", size.Width, size.Height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 04:14:39 +02:00
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetAudioArguments(string outputFormat)
|
|
|
|
|
{
|
2012-08-18 10:22:54 +02:00
|
|
|
|
AudioStream audioStream = (LibraryItem.AudioStreams ?? new AudioStream[] { }).FirstOrDefault();
|
2012-08-13 22:18:02 +02:00
|
|
|
|
|
|
|
|
|
if (audioStream == null)
|
|
|
|
|
{
|
|
|
|
|
return string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string codec = GetAudioCodec(audioStream, outputFormat);
|
2012-08-13 04:14:39 +02:00
|
|
|
|
|
|
|
|
|
string args = "-acodec " + codec;
|
|
|
|
|
|
|
|
|
|
if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
2012-08-13 22:18:02 +02:00
|
|
|
|
int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
|
2012-08-13 04:14:39 +02:00
|
|
|
|
|
|
|
|
|
if (channels.HasValue)
|
|
|
|
|
{
|
|
|
|
|
args += " -ac " + channels.Value;
|
|
|
|
|
}
|
2012-08-13 22:18:02 +02:00
|
|
|
|
|
|
|
|
|
int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
|
|
|
|
|
|
|
|
|
|
if (sampleRate.HasValue)
|
2012-08-13 04:14:39 +02:00
|
|
|
|
{
|
2012-08-13 22:18:02 +02:00
|
|
|
|
args += " -ar " + sampleRate.Value;
|
2012-08-13 04:14:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetVideoCodec(string outputFormat)
|
|
|
|
|
{
|
|
|
|
|
if (outputFormat.Equals("webm"))
|
|
|
|
|
{
|
|
|
|
|
// Per webm specification, it must be vpx
|
|
|
|
|
return "libvpx";
|
|
|
|
|
}
|
|
|
|
|
else if (outputFormat.Equals("asf"))
|
|
|
|
|
{
|
|
|
|
|
return "wmv2";
|
|
|
|
|
}
|
2012-08-14 04:24:35 +02:00
|
|
|
|
else if (outputFormat.Equals("wmv"))
|
|
|
|
|
{
|
|
|
|
|
return "wmv2";
|
|
|
|
|
}
|
|
|
|
|
else if (outputFormat.Equals("ogv"))
|
|
|
|
|
{
|
|
|
|
|
return "libtheora";
|
|
|
|
|
}
|
2012-08-13 04:14:39 +02:00
|
|
|
|
|
2012-08-16 04:42:37 +02:00
|
|
|
|
if (!RequiresVideoConversion())
|
|
|
|
|
{
|
|
|
|
|
return "copy";
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 22:18:02 +02:00
|
|
|
|
return "libx264";
|
2012-08-13 04:14:39 +02:00
|
|
|
|
}
|
2012-08-13 22:18:02 +02:00
|
|
|
|
|
|
|
|
|
private string GetAudioCodec(AudioStream audioStream, string outputFormat)
|
2012-08-12 15:59:09 +02:00
|
|
|
|
{
|
2012-08-13 04:14:39 +02:00
|
|
|
|
if (outputFormat.Equals("webm"))
|
|
|
|
|
{
|
|
|
|
|
// Per webm specification, it must be vorbis
|
|
|
|
|
return "libvorbis";
|
|
|
|
|
}
|
2012-08-14 04:24:35 +02:00
|
|
|
|
else if (outputFormat.Equals("asf"))
|
|
|
|
|
{
|
|
|
|
|
return "wmav2";
|
|
|
|
|
}
|
|
|
|
|
else if (outputFormat.Equals("wmv"))
|
|
|
|
|
{
|
|
|
|
|
return "wmav2";
|
|
|
|
|
}
|
|
|
|
|
else if (outputFormat.Equals("ogv"))
|
|
|
|
|
{
|
|
|
|
|
return "libvorbis";
|
|
|
|
|
}
|
2012-08-13 22:18:02 +02:00
|
|
|
|
|
|
|
|
|
// See if we can just copy the stream
|
2012-08-16 04:42:37 +02:00
|
|
|
|
if (!RequiresAudioConversion(audioStream))
|
2012-08-13 04:14:39 +02:00
|
|
|
|
{
|
2012-08-13 22:18:02 +02:00
|
|
|
|
return "copy";
|
2012-08-13 04:14:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 22:18:02 +02:00
|
|
|
|
return "libvo_aacenc";
|
2012-08-12 15:59:09 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 22:18:02 +02:00
|
|
|
|
private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
|
2012-08-12 15:59:09 +02:00
|
|
|
|
{
|
2012-08-14 04:24:35 +02:00
|
|
|
|
if (libraryItemChannels > 2)
|
2012-08-13 04:14:39 +02:00
|
|
|
|
{
|
2012-08-14 04:24:35 +02:00
|
|
|
|
if (audioCodec.Equals("libvo_aacenc"))
|
|
|
|
|
{
|
|
|
|
|
// libvo_aacenc currently only supports two channel output
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
else if (audioCodec.Equals("wmav2"))
|
|
|
|
|
{
|
|
|
|
|
// wmav2 currently only supports two channel output
|
|
|
|
|
return 2;
|
|
|
|
|
}
|
2012-08-13 04:14:39 +02:00
|
|
|
|
}
|
2012-08-13 22:18:02 +02:00
|
|
|
|
|
|
|
|
|
return GetNumAudioChannelsParam(libraryItemChannels);
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-16 04:42:37 +02:00
|
|
|
|
private bool RequiresVideoConversion()
|
|
|
|
|
{
|
|
|
|
|
// Check dimensions
|
|
|
|
|
if (Width.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (Width.Value != LibraryItem.Width)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (Height.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (Height.Value != LibraryItem.Height)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (MaxWidth.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (MaxWidth.Value < LibraryItem.Width)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (MaxHeight.HasValue)
|
|
|
|
|
{
|
|
|
|
|
if (MaxHeight.Value < LibraryItem.Height)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 10:22:54 +02:00
|
|
|
|
if (LibraryItem.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 || LibraryItem.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1)
|
2012-08-16 04:42:37 +02:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool RequiresAudioConversion(AudioStream audio)
|
2012-08-13 22:18:02 +02:00
|
|
|
|
{
|
2012-08-14 04:24:35 +02:00
|
|
|
|
if (AudioChannels.HasValue)
|
2012-08-13 22:18:02 +02:00
|
|
|
|
{
|
2012-08-14 04:24:35 +02:00
|
|
|
|
if (audio.Channels > AudioChannels.Value)
|
|
|
|
|
{
|
2012-08-16 04:42:37 +02:00
|
|
|
|
return true;
|
2012-08-14 04:24:35 +02:00
|
|
|
|
}
|
2012-08-13 22:18:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 21:16:51 +02:00
|
|
|
|
if (audio.Codec.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
|
2012-08-13 22:18:02 +02:00
|
|
|
|
{
|
2012-08-16 04:42:37 +02:00
|
|
|
|
return false;
|
2012-08-13 22:18:02 +02:00
|
|
|
|
}
|
2012-08-20 21:16:51 +02:00
|
|
|
|
if (audio.Codec.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
|
2012-08-13 22:18:02 +02:00
|
|
|
|
{
|
2012-08-16 04:42:37 +02:00
|
|
|
|
return false;
|
2012-08-13 22:18:02 +02:00
|
|
|
|
}
|
2012-08-20 21:16:51 +02:00
|
|
|
|
if (audio.Codec.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
|
2012-08-13 22:18:02 +02:00
|
|
|
|
{
|
2012-08-16 04:42:37 +02:00
|
|
|
|
return false;
|
2012-08-13 22:18:02 +02:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-16 04:42:37 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? Height
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["height"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? Width
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["width"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int? MaxHeight
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["maxheight"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
2012-08-11 20:07:07 +02:00
|
|
|
|
}
|
2012-08-16 04:42:37 +02:00
|
|
|
|
|
|
|
|
|
private int? MaxWidth
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string val = QueryString["maxwidth"];
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(val))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return int.Parse(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-11 20:07:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|