1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
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
{
/// <summary>
/// Runs FFProbe against an Audio file, caches the result and returns the output
/// </summary>
public static FFProbeResult Run(BaseItem item, string cacheDirectory)
{
string cachePath = GetFFProbeCachePath(item, cacheDirectory);
// Use try catch to avoid having to use File.Exists
try
{
return GetCachedResult(cachePath);
}
catch (FileNotFoundException)
{
}
catch (Exception ex)
{
Logger.LogException(ex);
}
FFProbeResult result = Run(item.Path);
if (result != null)
{
// Fire and forget
CacheResult(result, cachePath);
}
return result;
}
/// <summary>
/// Gets the cached result of an FFProbe operation
/// </summary>
private static FFProbeResult GetCachedResult(string path)
{
return ProtobufSerializer.DeserializeFromFile<FFProbeResult>(path);
}
/// <summary>
/// Caches the result of an FFProbe operation
/// </summary>
private static async void CacheResult(FFProbeResult result, string outputCachePath)
{
await Task.Run(() =>
{
try
{
ProtobufSerializer.SerializeToFile<FFProbeResult>(result, outputCachePath);
}
catch (Exception ex)
{
Logger.LogException(ex);
}
}).ConfigureAwait(false);
}
private static FFProbeResult Run(string input)
{
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;
process.EnableRaisingEvents = true;
process.Exited += process_Exited;
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();
return JsonSerializer.DeserializeFromStream<FFProbeResult>(process.StandardOutput.BaseStream);
}
catch (Exception ex)
{
Logger.LogException(ex);
// Hate having to do this
try
{
process.Kill();
}
catch
{
}
return null;
}
}
static void process_Exited(object sender, EventArgs e)
{
(sender as Process).Dispose();
}
private static string GetFFProbeCachePath(BaseItem item, string cacheDirectory)
{
string outputDirectory = Path.Combine(cacheDirectory, item.Id.ToString().Substring(0, 1));
return Path.Combine(outputDirectory, item.Id + "-" + item.DateModified.Ticks + ".pb");
}
}
}
|