| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 | 
							- using MediaBrowser.Common.Drawing;
 
- using MediaBrowser.Common.Net.Handlers;
 
- using MediaBrowser.Model.Entities;
 
- using System;
 
- using System.Collections.Generic;
 
- using System.ComponentModel.Composition;
 
- using System.Drawing;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Net;
 
- namespace MediaBrowser.Api.HttpHandlers
 
- {
 
-     /// <summary>
 
-     /// Supported output formats: mkv,m4v,mp4,asf,wmv,mov,webm,ogv,3gp,avi,ts,flv
 
-     /// </summary>
 
-     [Export(typeof(BaseHandler))]
 
-     class VideoHandler : BaseMediaHandler<Video>
 
-     {
 
-         public override bool HandlesRequest(HttpListenerRequest request)
 
-         {
 
-             return ApiService.IsApiUrlMatch("video", request);
 
-         }
 
-         
 
-         /// <summary>
 
-         /// We can output these files directly, but we can't encode them
 
-         /// </summary>
 
-         protected override IEnumerable<string> UnsupportedOutputEncodingFormats
 
-         {
 
-             get
 
-             {
 
-                 // 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" };
 
-             }
 
-         }
 
-         protected override bool RequiresConversion()
 
-         {
 
-             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
 
-             if (currentFormat.Equals("mp4", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase) || currentFormat.Equals("m4v", StringComparison.OrdinalIgnoreCase))
 
-             {
 
-                 return true;
 
-             }
 
-             if (base.RequiresConversion())
 
-             {
 
-                 return true;
 
-             }
 
-             if (RequiresVideoConversion())
 
-             {
 
-                 return true;
 
-             }
 
-             AudioStream audioStream = (LibraryItem.AudioStreams ?? new List<AudioStream>()).FirstOrDefault();
 
-             if (audioStream != null)
 
-             {
 
-                 if (RequiresAudioConversion(audioStream))
 
-                 {
 
-                     return true;
 
-                 }
 
-             }
 
-             // Yay
 
-             return false;
 
-         }
 
-         /// <summary>
 
-         /// Translates the file extension to the format param that follows "-f" on the ffmpeg command line
 
-         /// </summary>
 
-         private string GetFFMpegOutputFormat(string outputFormat)
 
-         {
 
-             if (outputFormat.Equals("mkv", StringComparison.OrdinalIgnoreCase))
 
-             {
 
-                 return "matroska";
 
-             }
 
-             else if (outputFormat.Equals("ts", StringComparison.OrdinalIgnoreCase))
 
-             {
 
-                 return "mpegts";
 
-             }
 
-             else if (outputFormat.Equals("ogv", StringComparison.OrdinalIgnoreCase))
 
-             {
 
-                 return "ogg";
 
-             }
 
-             return outputFormat;
 
-         }
 
-         /// <summary>
 
-         /// Creates arguments to pass to ffmpeg
 
-         /// </summary>
 
-         protected override string GetCommandLineArguments()
 
-         {
 
-             List<string> audioTranscodeParams = new List<string>();
 
-             string outputFormat = GetConversionOutputFormat();
 
-             return string.Format("-i \"{0}\" -threads 0 {1} {2} -f {3} -",
 
-                 LibraryItem.Path,
 
-                 GetVideoArguments(outputFormat),
 
-                 GetAudioArguments(outputFormat),
 
-                 GetFFMpegOutputFormat(outputFormat)
 
-                 );
 
-         }
 
-         private string GetVideoArguments(string outputFormat)
 
-         {
 
-             string codec = GetVideoCodec(outputFormat);
 
-             string args = "-vcodec " + codec;
 
-             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);
 
-                 }
 
-             }
 
-             return args;
 
-         }
 
-         private string GetAudioArguments(string outputFormat)
 
-         {
 
-             AudioStream audioStream = (LibraryItem.AudioStreams ?? new List<AudioStream>()).FirstOrDefault();
 
-             if (audioStream == null)
 
-             {
 
-                 return string.Empty;
 
-             }
 
-             string codec = GetAudioCodec(audioStream, outputFormat);
 
-             string args = "-acodec " + codec;
 
-             if (!codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
 
-             {
 
-                 int? channels = GetNumAudioChannelsParam(codec, audioStream.Channels);
 
-                 if (channels.HasValue)
 
-                 {
 
-                     args += " -ac " + channels.Value;
 
-                 }
 
-                 int? sampleRate = GetSampleRateParam(audioStream.SampleRate);
 
-                 if (sampleRate.HasValue)
 
-                 {
 
-                     args += " -ar " + sampleRate.Value;
 
-                 }
 
-             }
 
-             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";
 
-             }
 
-             else if (outputFormat.Equals("wmv"))
 
-             {
 
-                 return "wmv2";
 
-             }
 
-             else if (outputFormat.Equals("ogv"))
 
-             {
 
-                 return "libtheora";
 
-             }
 
-             if (!RequiresVideoConversion())
 
-             {
 
-                 return "copy";
 
-             }
 
-             return "libx264";
 
-         }
 
-         private string GetAudioCodec(AudioStream audioStream, string outputFormat)
 
-         {
 
-             if (outputFormat.Equals("webm"))
 
-             {
 
-                 // Per webm specification, it must be vorbis
 
-                 return "libvorbis";
 
-             }
 
-             else if (outputFormat.Equals("asf"))
 
-             {
 
-                 return "wmav2";
 
-             }
 
-             else if (outputFormat.Equals("wmv"))
 
-             {
 
-                 return "wmav2";
 
-             }
 
-             else if (outputFormat.Equals("ogv"))
 
-             {
 
-                 return "libvorbis";
 
-             }
 
-             // See if we can just copy the stream
 
-             if (!RequiresAudioConversion(audioStream))
 
-             {
 
-                 return "copy";
 
-             }
 
-             return "libvo_aacenc";
 
-         }
 
-         private int? GetNumAudioChannelsParam(string audioCodec, int libraryItemChannels)
 
-         {
 
-             if (libraryItemChannels > 2)
 
-             {
 
-                 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;
 
-                 }
 
-             }
 
-             return GetNumAudioChannelsParam(libraryItemChannels);
 
-         }
 
-         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;
 
-                 }
 
-             }
 
-             if (LibraryItem.Codec.IndexOf("264", StringComparison.OrdinalIgnoreCase) != -1 || LibraryItem.Codec.IndexOf("avc", StringComparison.OrdinalIgnoreCase) != -1)
 
-             {
 
-                 return false;
 
-             }
 
-             return false;
 
-         }
 
-         private bool RequiresAudioConversion(AudioStream audio)
 
-         {
 
-             if (AudioChannels.HasValue)
 
-             {
 
-                 if (audio.Channels > AudioChannels.Value)
 
-                 {
 
-                     return true;
 
-                 }
 
-             }
 
-             if (audio.Codec.IndexOf("aac", StringComparison.OrdinalIgnoreCase) != -1)
 
-             {
 
-                 return false;
 
-             }
 
-             if (audio.Codec.IndexOf("ac-3", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("ac3", StringComparison.OrdinalIgnoreCase) != -1)
 
-             {
 
-                 return false;
 
-             }
 
-             if (audio.Codec.IndexOf("mpeg", StringComparison.OrdinalIgnoreCase) != -1 || audio.Codec.IndexOf("mp3", StringComparison.OrdinalIgnoreCase) != -1)
 
-             {
 
-                 return false;
 
-             }
 
-             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);
 
-             }
 
-         }
 
-         private int? MaxWidth
 
-         {
 
-             get
 
-             {
 
-                 string val = QueryString["maxwidth"];
 
-                 if (string.IsNullOrEmpty(val))
 
-                 {
 
-                     return null;
 
-                 }
 
-                 return int.Parse(val);
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |