-
Notifications
You must be signed in to change notification settings - Fork 4
/
FFmpeg.cs
132 lines (121 loc) · 4.52 KB
/
FFmpeg.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Common.Logging;
namespace TranscodeProcessor
{
public class FFmpeg
{
private static ILog log = LogManager.GetCurrentClassLogger();
public delegate void LogData(string line);
public static string Exec(string args, out int exitCode, LogData logger = null)
{
log.Debug("Executing Command: " + args);
try
{
ProcessWrapper proc = new ProcessWrapper();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = FFRest.config["ffmpeg-location"];
proc.StartInfo.Arguments = args;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = false;
//proc.Start();
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
proc.OutputStreamChanged += (sender, m) =>
{
if (m != null)
{
output.Append(m);
if (logger != null)
logger(m);
}
};
proc.ErrorStreamChanged += (sender, m) =>
{
if (m != null)
{
error.Append(m);
if (logger != null)
logger(m);
}
};
proc.Start();
proc.WaitForOutput();
exitCode = proc.ExitCode;
return error.ToString() + output.ToString();
}
catch (Exception ex)
{
log.Debug("Error executing ffmpeg: " + ex.Message);
log.Debug("Exception: ", ex);
exitCode = -1;
}
return "";
}
public static string ExecProbe(string args)
{
log.Debug("Executing Probe: " + args);
try
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents = false;
proc.StartInfo.FileName = FFRest.config["ffprobe-location"];
proc.StartInfo.Arguments = args;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
//proc.Start();
StringBuilder output = new StringBuilder();
StringBuilder error = new StringBuilder();
using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
{
proc.OutputDataReceived += (sender, e) =>
{
if (e.Data == null)
{
outputWaitHandle.Set();
}
else
{
output.AppendLine(e.Data);
}
};
proc.ErrorDataReceived += (sender, e) =>
{
if (e.Data == null)
{
errorWaitHandle.Set();
}
else
{
error.AppendLine(e.Data);
}
};
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
if (proc.WaitForExit(int.MaxValue) &&
outputWaitHandle.WaitOne(int.MaxValue) &&
errorWaitHandle.WaitOne(int.MaxValue))
{
return error.ToString() + output.ToString();
}
}
return "";
}
catch (Exception ex)
{
log.Debug("Error executing ffprobe: " + ex.Message);
log.Debug("Exception: ", ex);
}
return "";
}
}
}