diff options
Diffstat (limited to 'MediaBrowser.Api/HttpHandlers')
| -rw-r--r-- | MediaBrowser.Api/HttpHandlers/AudioHandler.cs | 77 | ||||
| -rw-r--r-- | MediaBrowser.Api/HttpHandlers/VideoHandler.cs | 31 |
2 files changed, 61 insertions, 47 deletions
diff --git a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs index f3839a892..294a5b7af 100644 --- a/MediaBrowser.Api/HttpHandlers/AudioHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/AudioHandler.cs @@ -105,7 +105,7 @@ namespace MediaBrowser.Api.HttpHandlers /// <summary>
/// Creates arguments to pass to ffmpeg
/// </summary>
- private string GetAudioArguments()
+ protected override string GetCommandLineArguments()
{
List<string> audioTranscodeParams = new List<string>();
@@ -132,40 +132,6 @@ namespace MediaBrowser.Api.HttpHandlers return "-i \"" + LibraryItem.Path + "\" -vn " + string.Join(" ", audioTranscodeParams.ToArray()) + " -";
}
-
- protected async override Task WriteResponseToOutputStream(Stream stream)
- {
- ProcessStartInfo startInfo = new ProcessStartInfo();
-
- startInfo.CreateNoWindow = true;
-
- startInfo.UseShellExecute = false;
- startInfo.RedirectStandardOutput = true;
-
- startInfo.FileName = ApiService.FFMpegPath;
- startInfo.WorkingDirectory = ApiService.FFMpegDirectory;
- startInfo.Arguments = GetAudioArguments();
-
- Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
-
- Process process = new Process();
- process.StartInfo = startInfo;
-
- try
- {
- process.Start();
-
- await process.StandardOutput.BaseStream.CopyToAsync(stream);
- }
- catch (Exception ex)
- {
- Logger.LogException(ex);
- }
- finally
- {
- process.Dispose();
- }
- }
}
public abstract class BaseMediaHandler<T> : BaseHandler
@@ -252,7 +218,48 @@ namespace MediaBrowser.Api.HttpHandlers base.ProcessRequest(ctx);
}
+ protected abstract string GetCommandLineArguments();
protected abstract string GetOutputFormat();
protected abstract bool RequiresConversion();
+
+ protected async override Task WriteResponseToOutputStream(Stream stream)
+ {
+ ProcessStartInfo startInfo = new ProcessStartInfo();
+
+ startInfo.CreateNoWindow = true;
+
+ startInfo.UseShellExecute = false;
+ startInfo.RedirectStandardOutput = true;
+ startInfo.RedirectStandardError = true;
+
+ startInfo.FileName = ApiService.FFMpegPath;
+ startInfo.WorkingDirectory = ApiService.FFMpegDirectory;
+ startInfo.Arguments = GetCommandLineArguments();
+
+ Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
+
+ Process process = new Process();
+ process.StartInfo = startInfo;
+
+ try
+ {
+ process.Start();
+
+ // MUST read both stdout and stderr asynchronously or a deadlock may occurr
+ process.BeginErrorReadLine();
+
+ await process.StandardOutput.BaseStream.CopyToAsync(stream);
+
+ process.WaitForExit();
+ }
+ catch (Exception ex)
+ {
+ Logger.LogException(ex);
+ }
+ finally
+ {
+ process.Dispose();
+ }
+ }
}
}
diff --git a/MediaBrowser.Api/HttpHandlers/VideoHandler.cs b/MediaBrowser.Api/HttpHandlers/VideoHandler.cs index f94b8fc31..1b5c50c38 100644 --- a/MediaBrowser.Api/HttpHandlers/VideoHandler.cs +++ b/MediaBrowser.Api/HttpHandlers/VideoHandler.cs @@ -1,20 +1,14 @@ using System;
using System.Collections.Generic;
-using System.Diagnostics;
-using System.IO;
using System.Linq;
-using System.Net;
-using System.Threading.Tasks;
-using MediaBrowser.Common.Logging;
-using MediaBrowser.Common.Net;
-using MediaBrowser.Common.Net.Handlers;
-using MediaBrowser.Controller;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api.HttpHandlers
{
class VideoHandler : BaseMediaHandler<Video>
{
+ private IEnumerable<string> UnsupportedOutputFormats = new string[] { "mp4" };
+
public IEnumerable<string> VideoFormats
{
get
@@ -28,17 +22,23 @@ namespace MediaBrowser.Api.HttpHandlers /// </summary>
protected override string GetOutputFormat()
{
- return VideoFormats.First();
+ return VideoFormats.First(f => !UnsupportedOutputFormats.Any(s => s.Equals(f, StringComparison.OrdinalIgnoreCase)));
}
protected override bool RequiresConversion()
{
+ // If it's not in a format we can output to, return true
+ 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;
}
-
+
AudioStream audio = LibraryItem.AudioStreams.FirstOrDefault();
if (audio != null)
@@ -54,9 +54,16 @@ namespace MediaBrowser.Api.HttpHandlers return false;
}
- protected override Task WriteResponseToOutputStream(Stream stream)
+ /// <summary>
+ /// Creates arguments to pass to ffmpeg
+ /// </summary>
+ protected override string GetCommandLineArguments()
{
- throw new NotImplementedException();
+ List<string> audioTranscodeParams = new List<string>();
+
+ string outputFormat = GetOutputFormat();
+ outputFormat = "matroska";
+ return "-i \"" + LibraryItem.Path + "\" -vcodec copy -acodec copy -f " + outputFormat + " -";
}
}
}
|
