2012-08-18 21:40:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2012-08-19 15:58:35 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
2012-08-18 21:40:24 +00:00
|
|
|
|
using MediaBrowser.Common.Logging;
|
|
|
|
|
using MediaBrowser.Common.Serialization;
|
2012-08-19 15:58:35 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2012-08-18 21:40:24 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.FFMpeg
|
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Runs FFProbe against a media file and returns metadata.
|
|
|
|
|
/// </summary>
|
2012-08-18 21:40:24 +00:00
|
|
|
|
public static class FFProbe
|
|
|
|
|
{
|
2012-08-19 15:58:35 +00:00
|
|
|
|
public async static Task<FFProbeResult> Run(Audio item, string outputCachePath)
|
|
|
|
|
{
|
|
|
|
|
// Use try catch to avoid having to use File.Exists
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (FileStream stream = File.OpenRead(outputCachePath))
|
|
|
|
|
{
|
|
|
|
|
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Run(item.Path, outputCachePath);
|
|
|
|
|
|
|
|
|
|
using (FileStream stream = File.OpenRead(outputCachePath))
|
|
|
|
|
{
|
|
|
|
|
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-20 19:16:51 +00:00
|
|
|
|
public async static Task<FFProbeResult> Run(Video item, string outputCachePath)
|
|
|
|
|
{
|
|
|
|
|
// Use try catch to avoid having to use File.Exists
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (FileStream stream = File.OpenRead(outputCachePath))
|
|
|
|
|
{
|
|
|
|
|
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Run(item.Path, outputCachePath);
|
|
|
|
|
|
|
|
|
|
using (FileStream stream = File.OpenRead(outputCachePath))
|
|
|
|
|
{
|
|
|
|
|
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
private async static Task Run(string input, string output)
|
2012-08-18 21:40:24 +00:00
|
|
|
|
{
|
|
|
|
|
ProcessStartInfo startInfo = new ProcessStartInfo();
|
|
|
|
|
|
|
|
|
|
startInfo.CreateNoWindow = true;
|
|
|
|
|
|
|
|
|
|
startInfo.UseShellExecute = false;
|
|
|
|
|
|
|
|
|
|
// Must consume both or ffmpeg may hang due to deadlocks. See comments below.
|
|
|
|
|
startInfo.RedirectStandardOutput = true;
|
|
|
|
|
startInfo.RedirectStandardError = true;
|
|
|
|
|
|
|
|
|
|
startInfo.FileName = Kernel.Instance.ApplicationPaths.FFProbePath;
|
|
|
|
|
startInfo.WorkingDirectory = Kernel.Instance.ApplicationPaths.FFMpegDirectory;
|
2012-08-19 15:58:35 +00:00
|
|
|
|
startInfo.Arguments = string.Format("\"{0}\" -v quiet -print_format json -show_streams -show_format", input);
|
2012-08-18 21:40:24 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
//Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
|
2012-08-18 21:40:24 +00:00
|
|
|
|
|
|
|
|
|
Process process = new Process();
|
|
|
|
|
process.StartInfo = startInfo;
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
FileStream stream = new FileStream(output, FileMode.Create);
|
|
|
|
|
|
2012-08-18 21:40:24 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
process.Start();
|
|
|
|
|
|
|
|
|
|
// MUST read both stdout and stderr asynchronously or a deadlock may occurr
|
|
|
|
|
// If we ever decide to disable the ffmpeg log then you must uncomment the below line.
|
|
|
|
|
process.BeginErrorReadLine();
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
await process.StandardOutput.BaseStream.CopyToAsync(stream);
|
2012-08-18 21:40:24 +00:00
|
|
|
|
|
|
|
|
|
process.WaitForExit();
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
stream.Dispose();
|
2012-08-18 21:40:24 +00:00
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
if (process.ExitCode != 0)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogInfo("FFProbe exited with code {0} for {1}", process.ExitCode, input);
|
|
|
|
|
}
|
2012-08-18 21:40:24 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogException(ex);
|
|
|
|
|
|
2012-08-19 15:58:35 +00:00
|
|
|
|
stream.Dispose();
|
|
|
|
|
|
2012-08-18 21:40:24 +00:00
|
|
|
|
// Hate having to do this
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
process.Kill();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
}
|
2012-08-19 15:58:35 +00:00
|
|
|
|
File.Delete(output);
|
2012-08-18 21:40:24 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
process.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|