123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Threading.Tasks;
- using MediaBrowser.Common.Logging;
- using MediaBrowser.Common.Serialization;
- using MediaBrowser.Model.Entities;
- namespace MediaBrowser.Controller.FFMpeg
- {
- /// <summary>
- /// Runs FFProbe against a media file and returns metadata.
- /// </summary>
- 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
- {
- return GetCachedResult(outputCachePath);
- }
- catch (FileNotFoundException)
- {
- }
- await Run(item.Path, outputCachePath).ConfigureAwait(false);
- return GetCachedResult(item.Path);
- }
- public static FFProbeResult GetCachedResult(string path)
- {
- using (FileStream stream = File.OpenRead(path))
- {
- 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
- {
- return GetCachedResult(outputCachePath);
- }
- catch (FileNotFoundException)
- {
- }
- await Run(item.Path, outputCachePath).ConfigureAwait(false);
- return GetCachedResult(item.Path);
- }
- private async static Task Run(string input, string output)
- {
- 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);
- //Logger.LogInfo(startInfo.FileName + " " + startInfo.Arguments);
- Process process = new Process();
- process.StartInfo = startInfo;
- FileStream stream = new FileStream(output, FileMode.Create);
- 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();
- await process.StandardOutput.BaseStream.CopyToAsync(stream).ConfigureAwait(false);
- process.WaitForExit();
- stream.Dispose();
- if (process.ExitCode != 0)
- {
- Logger.LogInfo("FFProbe exited with code {0} for {1}", process.ExitCode, input);
- }
- }
- catch (Exception ex)
- {
- Logger.LogException(ex);
- stream.Dispose();
- // Hate having to do this
- try
- {
- process.Kill();
- }
- catch
- {
- }
- File.Delete(output);
- }
- finally
- {
- process.Dispose();
- }
- }
- }
- }
|