jellyfin/MediaBrowser.Controller/FFMpeg/FFProbe.cs

125 lines
4.0 KiB
C#
Raw Normal View History

2012-08-18 21:40:24 +00:00
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
2012-08-18 21:40:24 +00:00
using MediaBrowser.Common.Logging;
using MediaBrowser.Common.Serialization;
using MediaBrowser.Model.Entities;
2012-08-18 21:40:24 +00:00
namespace MediaBrowser.Controller.FFMpeg
{
/// <summary>
/// Runs FFProbe against a media file and returns metadata.
/// </summary>
2012-08-18 21:40:24 +00:00
public static class FFProbe
{
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)
{
}
2012-08-20 21:48:11 +00:00
await Run(item.Path, outputCachePath).ConfigureAwait(false);
using (FileStream stream = File.OpenRead(outputCachePath))
{
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
}
}
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)
{
}
2012-08-20 21:48:11 +00:00
await Run(item.Path, outputCachePath).ConfigureAwait(false);
using (FileStream stream = File.OpenRead(outputCachePath))
{
return JsonSerializer.DeserializeFromStream<FFProbeResult>(stream);
}
}
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;
startInfo.Arguments = string.Format("\"{0}\" -v quiet -print_format json -show_streams -show_format", input);
2012-08-18 21:40:24 +00:00
//Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
2012-08-18 21:40:24 +00:00
Process process = new Process();
process.StartInfo = startInfo;
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-20 21:48:11 +00:00
await process.StandardOutput.BaseStream.CopyToAsync(stream).ConfigureAwait(false);
2012-08-18 21:40:24 +00:00
process.WaitForExit();
stream.Dispose();
2012-08-18 21:40:24 +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);
stream.Dispose();
2012-08-18 21:40:24 +00:00
// Hate having to do this
try
{
process.Kill();
}
catch
{
}
File.Delete(output);
2012-08-18 21:40:24 +00:00
}
finally
{
process.Dispose();
}
}
}
}