|
@@ -17,7 +17,7 @@ namespace Common.system
|
17
|
17
|
/// </summary>
|
18
|
18
|
public class FFMpeg
|
19
|
19
|
{
|
20
|
|
- Process myProcess = null;
|
|
20
|
+ public Process myProcess = null;
|
21
|
21
|
/// <summary>
|
22
|
22
|
/// ffmpeg输出日志文件地址 每个文件2MB左右
|
23
|
23
|
/// </summary>
|
|
@@ -28,13 +28,16 @@ namespace Common.system
|
28
|
28
|
/// <param name="FilePath">文件存储路径</param>
|
29
|
29
|
public void StartRecordingVideo(string FilePath)
|
30
|
30
|
{
|
|
31
|
+ while (myProcess != null)
|
|
32
|
+ {
|
|
33
|
+ Thread.Sleep(100);
|
|
34
|
+ }
|
31
|
35
|
//路径
|
32
|
|
- string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
|
|
36
|
+ string Path = FileToolsCommon.GetDirectoryName(FilePath);
|
33
|
37
|
string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
|
34
|
38
|
//文件保存路径
|
35
|
39
|
string PathName = GetFilePathName(FilePath);
|
36
|
40
|
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
37
|
|
- Debug.WriteLine(KillProcessArray.Length.ToString());
|
38
|
41
|
foreach (var KillProcess in KillProcessArray)
|
39
|
42
|
{
|
40
|
43
|
KillProcess.Kill();
|
|
@@ -95,8 +98,13 @@ namespace Common.system
|
95
|
98
|
/// <param name="Mp3Path">MP3音频位置</param>
|
96
|
99
|
public void StartRecordingAudio(string Mp3Path)
|
97
|
100
|
{
|
|
101
|
+ while (myProcess != null)
|
|
102
|
+ {
|
|
103
|
+ Thread.Sleep(100);
|
|
104
|
+ }
|
|
105
|
+ //文件保存路径
|
|
106
|
+ string PathName = GetFilePathName(Mp3Path);
|
98
|
107
|
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
99
|
|
- Debug.WriteLine(KillProcessArray.Length.ToString());
|
100
|
108
|
foreach (var KillProcess in KillProcessArray)
|
101
|
109
|
{
|
102
|
110
|
KillProcess.Kill();
|
|
@@ -107,12 +115,12 @@ namespace Common.system
|
107
|
115
|
string MicrophoneName = GetMicrophone();
|
108
|
116
|
if (string.IsNullOrWhiteSpace(MicrophoneName))
|
109
|
117
|
{
|
110
|
|
- myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" " + Mp3Path;
|
|
118
|
+ myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" " + PathName;
|
111
|
119
|
}
|
112
|
120
|
else
|
113
|
121
|
{
|
114
|
122
|
myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
|
115
|
|
- MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 " + Mp3Path;
|
|
123
|
+ MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 " + PathName;
|
116
|
124
|
}
|
117
|
125
|
myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
|
118
|
126
|
myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
|
|
@@ -127,14 +135,29 @@ namespace Common.system
|
127
|
135
|
/// <summary>
|
128
|
136
|
/// 暂停录制
|
129
|
137
|
/// </summary>
|
130
|
|
- public void SuspendFFmpeg()
|
|
138
|
+ public bool SuspendFFmpeg()
|
131
|
139
|
{
|
132
|
|
- if (myProcess == null)
|
133
|
|
- return;
|
134
|
|
- myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
|
135
|
|
- myProcess.Close();//关闭进程
|
136
|
|
- myProcess.Dispose();//释放资源
|
|
140
|
+ if (myProcess != null)
|
|
141
|
+ {
|
|
142
|
+ myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
|
|
143
|
+ myProcess.Close();//关闭进程
|
|
144
|
+ myProcess.Dispose();//释放资源
|
|
145
|
+ }
|
|
146
|
+ #region 进程是否已经释放
|
|
147
|
+ bool IsRunning = true;
|
|
148
|
+ while (IsRunning)
|
|
149
|
+ {
|
|
150
|
+ IsRunning = false;
|
|
151
|
+ Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
|
|
152
|
+ foreach (var KillProcess in ProcessArray)
|
|
153
|
+ {
|
|
154
|
+ IsRunning = true;
|
|
155
|
+ Thread.Sleep(100);
|
|
156
|
+ }
|
|
157
|
+ }
|
|
158
|
+ #endregion
|
137
|
159
|
myProcess = null;
|
|
160
|
+ return true;
|
138
|
161
|
//myProcess.Kill();
|
139
|
162
|
}
|
140
|
163
|
|
|
@@ -142,17 +165,32 @@ namespace Common.system
|
142
|
165
|
/// 停止录制并合成文件
|
143
|
166
|
/// </summary>
|
144
|
167
|
/// <param name="FilePath">文件存储路径 必须与开始时的路径完全一致</param>
|
145
|
|
- public void StopFFmpeg(string FilePath)
|
|
168
|
+ public bool StopFFmpeg(string FilePath)
|
146
|
169
|
{
|
147
|
170
|
//文件完整路径
|
148
|
|
- if (myProcess == null)
|
149
|
|
- return;
|
150
|
|
- myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
|
151
|
|
- myProcess.Close();//关闭进程
|
152
|
|
- myProcess.Dispose();//释放资源
|
|
171
|
+ if (myProcess != null)
|
|
172
|
+ {
|
|
173
|
+ myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
|
|
174
|
+ myProcess.Close();//关闭进程
|
|
175
|
+ myProcess.Dispose();//释放资源
|
|
176
|
+ }
|
|
177
|
+ #region 进程是否已经释放
|
|
178
|
+ bool IsRunning = true;
|
|
179
|
+ while (IsRunning)
|
|
180
|
+ {
|
|
181
|
+ IsRunning = false;
|
|
182
|
+ Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
|
|
183
|
+ foreach (var KillProcess in ProcessArray)
|
|
184
|
+ {
|
|
185
|
+ IsRunning = true;
|
|
186
|
+ Thread.Sleep(100);
|
|
187
|
+ }
|
|
188
|
+ }
|
|
189
|
+ #endregion
|
153
|
190
|
myProcess = null;
|
154
|
191
|
Thread t1 = new Thread(MergeVideoOrAudio);
|
155
|
192
|
t1.Start(FilePath);
|
|
193
|
+ return true;
|
156
|
194
|
//文件合成
|
157
|
195
|
}
|
158
|
196
|
|
|
@@ -167,14 +205,16 @@ namespace Common.system
|
167
|
205
|
/// <param name="VideoHeight">视频高度</param>
|
168
|
206
|
public void SynthesisVideo(string ImageListPath, string Mp3Path, string VideoSavePath, int Frequency, int VideoWidth, int VideoHeight)
|
169
|
207
|
{
|
170
|
|
- Thread thread = new Thread(delegate ()
|
|
208
|
+ new Thread(new ThreadStart(new Action(() =>
|
171
|
209
|
{
|
172
|
|
- //// 允许不同线程间的调用
|
173
|
|
- //Control.CheckForIllegalCrossThreadCalls = false;
|
174
|
210
|
Thread.Sleep(500);
|
|
211
|
+ while (myProcess != null)
|
|
212
|
+ {
|
|
213
|
+ Thread.Sleep(100);
|
|
214
|
+ }
|
175
|
215
|
string Extension = FileToolsCommon.GetIOExtension(VideoSavePath);//扩展名
|
176
|
216
|
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
177
|
|
- Debug.WriteLine(KillProcessArray.Length.ToString());
|
|
217
|
+ //Debug.WriteLine(KillProcessArray.Length.ToString());
|
178
|
218
|
foreach (var KillProcess in KillProcessArray)
|
179
|
219
|
{
|
180
|
220
|
KillProcess.Kill();
|
|
@@ -220,23 +260,53 @@ namespace Common.system
|
220
|
260
|
myProcess.WaitForExit(); //阻塞等待进程结束
|
221
|
261
|
myProcess.Close(); //关闭进程
|
222
|
262
|
myProcess.Dispose(); //释放资源
|
223
|
|
- //生成视频后删除图片保存列表
|
224
|
|
- FileToolsCommon.DeleteDirectory(ImageListPath);
|
225
|
|
- });
|
|
263
|
+ #region 进程是否已经释放
|
|
264
|
+ bool IsRunning = true;
|
|
265
|
+ while (IsRunning)
|
|
266
|
+ {
|
|
267
|
+ IsRunning = false;
|
|
268
|
+ Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
|
|
269
|
+ foreach (var KillProcess in ProcessArray)
|
|
270
|
+ {
|
|
271
|
+ IsRunning = true;
|
|
272
|
+ Thread.Sleep(100);
|
|
273
|
+ }
|
|
274
|
+ }
|
|
275
|
+ #endregion
|
|
276
|
+ myProcess = null;
|
|
277
|
+ //生成视频后删除图片和音频保存列表
|
|
278
|
+ //FileToolsCommon.DeleteDirectory(ImageListPath);
|
|
279
|
+ //FileToolsCommon.DeleteDirectory(FileToolsCommon.GetDirectoryName(Mp3Path));
|
|
280
|
+
|
|
281
|
+ //Dispatcher.Invoke(() =>
|
|
282
|
+ //{
|
|
283
|
+ //});
|
|
284
|
+ //// 允许不同线程间的调用
|
|
285
|
+ //Control.CheckForIllegalCrossThreadCalls = false;
|
|
286
|
+ }))).Start();
|
226
|
287
|
}
|
227
|
288
|
|
228
|
289
|
/// <summary>
|
229
|
290
|
/// 视频音频合成
|
230
|
291
|
/// </summary>
|
231
|
292
|
/// <param name="FilePath">存储路径</param>
|
232
|
|
- void MergeVideoOrAudio(object FilePathobj)
|
|
293
|
+ public void MergeVideoOrAudio(object FilePathobj)
|
233
|
294
|
{
|
234
|
|
- Thread.Sleep(500);
|
|
295
|
+ //new Thread(new ThreadStart(new Action(() =>
|
|
296
|
+ //{
|
|
297
|
+ //Dispatcher.Invoke(() =>
|
|
298
|
+ //{
|
|
299
|
+ //});
|
|
300
|
+ //}))).Start();
|
|
301
|
+
|
|
302
|
+ while (myProcess != null)
|
|
303
|
+ {
|
|
304
|
+ Thread.Sleep(100);
|
|
305
|
+ }
|
235
|
306
|
//路径
|
236
|
307
|
string FilePath = (string)FilePathobj;
|
237
|
|
- string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
|
|
308
|
+ string Path = FileToolsCommon.GetDirectoryName(FilePath);
|
238
|
309
|
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
239
|
|
- Debug.WriteLine(KillProcessArray.Length.ToString());
|
240
|
310
|
foreach (var KillProcess in KillProcessArray)
|
241
|
311
|
{
|
242
|
312
|
KillProcess.Kill();
|
|
@@ -256,6 +326,20 @@ namespace Common.system
|
256
|
326
|
myProcess.WaitForExit(); //阻塞等待进程结束
|
257
|
327
|
myProcess.Close(); //关闭进程
|
258
|
328
|
myProcess.Dispose(); //释放资源
|
|
329
|
+ #region 进程是否已经释放
|
|
330
|
+ bool IsRunning = true;
|
|
331
|
+ while (IsRunning)
|
|
332
|
+ {
|
|
333
|
+ IsRunning = false;
|
|
334
|
+ Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
|
|
335
|
+ foreach (var KillProcess in ProcessArray)
|
|
336
|
+ {
|
|
337
|
+ IsRunning = true;
|
|
338
|
+ Thread.Sleep(100);
|
|
339
|
+ }
|
|
340
|
+ }
|
|
341
|
+ #endregion
|
|
342
|
+ myProcess = null;
|
259
|
343
|
}
|
260
|
344
|
|
261
|
345
|
/// <summary>
|
|
@@ -266,7 +350,7 @@ namespace Common.system
|
266
|
350
|
string GetFilePathName(string FilePath)
|
267
|
351
|
{
|
268
|
352
|
//路径
|
269
|
|
- string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
|
|
353
|
+ string Path = FileToolsCommon.GetDirectoryName(FilePath);
|
270
|
354
|
//Path.GetFileName(FilePath);//获取文件名
|
271
|
355
|
string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
|
272
|
356
|
string tempFilePath = Path + "temp/";
|
|
@@ -295,8 +379,11 @@ namespace Common.system
|
295
|
379
|
/// <param name="ImagePath">图片地址</param>
|
296
|
380
|
public void GenerateThumbnails(string VideoPath, string ImagePath)
|
297
|
381
|
{
|
|
382
|
+ while (myProcess != null)
|
|
383
|
+ {
|
|
384
|
+ Thread.Sleep(100);
|
|
385
|
+ }
|
298
|
386
|
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
299
|
|
- Debug.WriteLine(KillProcessArray.Length.ToString());
|
300
|
387
|
foreach (var KillProcess in KillProcessArray)
|
301
|
388
|
{
|
302
|
389
|
KillProcess.Kill();
|
|
@@ -317,6 +404,20 @@ namespace Common.system
|
317
|
404
|
myProcess.WaitForExit(); //阻塞等待进程结束
|
318
|
405
|
myProcess.Close(); //关闭进程
|
319
|
406
|
myProcess.Dispose(); //释放资源
|
|
407
|
+ #region 进程是否已经释放
|
|
408
|
+ bool IsRunning = true;
|
|
409
|
+ while (IsRunning)
|
|
410
|
+ {
|
|
411
|
+ IsRunning = false;
|
|
412
|
+ Process[] ProcessArray = Process.GetProcessesByName("ffmpeg");
|
|
413
|
+ foreach (var KillProcess in ProcessArray)
|
|
414
|
+ {
|
|
415
|
+ IsRunning = true;
|
|
416
|
+ Thread.Sleep(100);
|
|
417
|
+ }
|
|
418
|
+ }
|
|
419
|
+ #endregion
|
|
420
|
+ myProcess = null;
|
320
|
421
|
}
|
321
|
422
|
|
322
|
423
|
/// <summary>
|