|
@@ -4,6 +4,8 @@ using System.Diagnostics;
|
4
|
4
|
using System.IO;
|
5
|
5
|
using System.Linq;
|
6
|
6
|
using System.Text;
|
|
7
|
+using System.Threading;
|
|
8
|
+
|
7
|
9
|
using VisioForge.Shared.NAudio.CoreAudioApi;
|
8
|
10
|
using VisioForge.Shared.NAudio.Wave;
|
9
|
11
|
|
|
@@ -11,6 +13,8 @@ namespace Common.system
|
11
|
13
|
{
|
12
|
14
|
/// <summary>
|
13
|
15
|
/// ffmpeg帮助类
|
|
16
|
+ /// 创建人:赵耀
|
|
17
|
+ /// 创建时间::2020年8月22日
|
14
|
18
|
/// 需要安装\ffmpeg\bin\Setup Screen Capturer Recorder v0.12.10.exe
|
15
|
19
|
/// 本地调试需要配置环境变量 将ffmpeg.exe位置配置到环境变量的path中
|
16
|
20
|
/// </summary>
|
|
@@ -24,10 +28,14 @@ namespace Common.system
|
24
|
28
|
/// <summary>
|
25
|
29
|
/// 录制屏幕
|
26
|
30
|
/// </summary>
|
27
|
|
- /// <param name="PathName">文件存储路径</param>
|
28
|
|
- /// <param name="VideoType">视频类型</param>
|
29
|
|
- public void StartRecordingVideo(string PathName, string VideoType)
|
|
31
|
+ /// <param name="FilePath">文件存储路径</param>
|
|
32
|
+ public void StartRecordingVideo(string FilePath)
|
30
|
33
|
{
|
|
34
|
+ //路径
|
|
35
|
+ string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
|
|
36
|
+ string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
|
|
37
|
+ //文件保存路径
|
|
38
|
+ string PathName = GetFilePathName(FilePath);
|
31
|
39
|
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
32
|
40
|
Debug.WriteLine(KillProcessArray.Length.ToString());
|
33
|
41
|
foreach (var KillProcess in KillProcessArray)
|
|
@@ -38,7 +46,7 @@ namespace Common.system
|
38
|
46
|
LogPath = CreateffmpegLog();
|
39
|
47
|
string MicrophoneName = GetMicrophone();
|
40
|
48
|
myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
|
41
|
|
- switch (VideoType.ToUpper())
|
|
49
|
+ switch (Extension.ToUpper())
|
42
|
50
|
{
|
43
|
51
|
case "MP4":
|
44
|
52
|
if (string.IsNullOrWhiteSpace(MicrophoneName))
|
|
@@ -55,12 +63,12 @@ namespace Common.system
|
55
|
63
|
case "FLV":
|
56
|
64
|
if (string.IsNullOrWhiteSpace(MicrophoneName))
|
57
|
65
|
{
|
58
|
|
- myProcess.StartInfo.Arguments = "-f dshow -i video=\"screen-capture-recorder\" -f dshow -i audio=\"virtual-audio-capturer\" -vcodec libx264 -acodec libmp3lame -r 15 -crf 22 -f "+ VideoType.ToLower() + " " + PathName; //ffmpeg的参数
|
|
66
|
+ myProcess.StartInfo.Arguments = "-f dshow -i video=\"screen-capture-recorder\" -f dshow -i audio=\"virtual-audio-capturer\" -vcodec libx264 -acodec libmp3lame -r 15 -crf 22 -f " + Extension.ToLower() + " " + PathName; //ffmpeg的参数
|
59
|
67
|
}
|
60
|
68
|
else
|
61
|
69
|
{
|
62
|
70
|
myProcess.StartInfo.Arguments = "-f dshow -i audio=\"virtual-audio-capturer\" -f dshow -i audio=\"" +
|
63
|
|
- MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f dshow -i video=\"screen-capture-recorder\" -pix_fmt yuv420p -vcodec h264 -preset:v ultrafast -tune:v zerolatency -acodec aac -ar 44100 -ac 2 -f " + VideoType.ToLower() + " " + PathName; //ffmpeg的参数
|
|
71
|
+ MicrophoneName + "\" -filter_complex amix=inputs=2:duration=first:dropout_transition=2 -f dshow -i video=\"screen-capture-recorder\" -pix_fmt yuv420p -vcodec h264 -preset:v ultrafast -tune:v zerolatency -acodec aac -ar 44100 -ac 2 -f " + Extension.ToLower() + " " + PathName; //ffmpeg的参数
|
64
|
72
|
}
|
65
|
73
|
break;
|
66
|
74
|
default:
|
|
@@ -120,11 +128,10 @@ namespace Common.system
|
120
|
128
|
}
|
121
|
129
|
|
122
|
130
|
/// <summary>
|
123
|
|
- /// 停止录制
|
|
131
|
+ /// 暂停录制
|
124
|
132
|
/// </summary>
|
125
|
|
- public void StopFFmpeg()
|
|
133
|
+ public void SuspendFFmpeg()
|
126
|
134
|
{
|
127
|
|
- //Thread.Sleep(3000);
|
128
|
135
|
if (myProcess == null)
|
129
|
136
|
return;
|
130
|
137
|
myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
|
|
@@ -135,7 +142,25 @@ namespace Common.system
|
135
|
142
|
}
|
136
|
143
|
|
137
|
144
|
/// <summary>
|
138
|
|
- /// 合成视频
|
|
145
|
+ /// 停止录制并合成文件
|
|
146
|
+ /// </summary>
|
|
147
|
+ /// <param name="FilePath">文件存储路径 必须与开始时的路径完全一致</param>
|
|
148
|
+ public void StopFFmpeg(string FilePath)
|
|
149
|
+ {
|
|
150
|
+ //文件完整路径
|
|
151
|
+ if (myProcess == null)
|
|
152
|
+ return;
|
|
153
|
+ myProcess.StandardInput.WriteLine("q");//在这个进程的控制台中模拟输入q,用于暂停录制
|
|
154
|
+ myProcess.Close();//关闭进程
|
|
155
|
+ myProcess.Dispose();//释放资源
|
|
156
|
+ myProcess = null;
|
|
157
|
+ Thread t1 = new Thread(MergeVideoOrAudio);
|
|
158
|
+ t1.Start(FilePath);
|
|
159
|
+ //文件合成
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ /// <summary>
|
|
163
|
+ /// 图片音频合成视频
|
139
|
164
|
/// </summary>
|
140
|
165
|
/// <param name="ImageListPath">图片列表位置 命名为1.png 2.png</param>
|
141
|
166
|
/// <param name="Mp3Path">MP3音频位置</param>
|
|
@@ -143,9 +168,76 @@ namespace Common.system
|
143
|
168
|
/// <param name="Frequency">每秒帧率</param>
|
144
|
169
|
/// <param name="VideoWidth">视频宽度</param>
|
145
|
170
|
/// <param name="VideoHeight">视频高度</param>
|
146
|
|
- /// <param name="VideoType">视频类型 MP4 FLV AVI</param>
|
147
|
|
- private void SynthesisVideo(string ImageListPath, string Mp3Path, string VideoSavePath, int Frequency, int VideoWidth, int VideoHeight, string VideoType)
|
|
171
|
+ private void SynthesisVideo(string ImageListPath, string Mp3Path, string VideoSavePath, int Frequency, int VideoWidth, int VideoHeight)
|
|
172
|
+ {
|
|
173
|
+ Thread thread = new Thread(delegate ()
|
|
174
|
+ {
|
|
175
|
+ //// 允许不同线程间的调用
|
|
176
|
+ //Control.CheckForIllegalCrossThreadCalls = false;
|
|
177
|
+ Thread.Sleep(500);
|
|
178
|
+ string Extension = FileToolsCommon.GetIOExtension(VideoSavePath);//扩展名
|
|
179
|
+ Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
|
180
|
+ Debug.WriteLine(KillProcessArray.Length.ToString());
|
|
181
|
+ foreach (var KillProcess in KillProcessArray)
|
|
182
|
+ {
|
|
183
|
+ KillProcess.Kill();
|
|
184
|
+ }
|
|
185
|
+ myProcess = new Process();
|
|
186
|
+ LogPath = CreateffmpegLog();
|
|
187
|
+ this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
|
|
188
|
+ switch (Extension.ToUpper())
|
|
189
|
+ {
|
|
190
|
+ case "MP4":
|
|
191
|
+ myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
|
|
192
|
+ ImageListPath + @"%d.png -i " +
|
|
193
|
+ Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
|
|
194
|
+ VideoSavePath;
|
|
195
|
+ break;
|
|
196
|
+ case "AVI":
|
|
197
|
+ myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
|
|
198
|
+ ImageListPath + @"%d.png -i " +
|
|
199
|
+ Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f AVI " +
|
|
200
|
+ VideoSavePath;
|
|
201
|
+ break;
|
|
202
|
+ case "FLV":
|
|
203
|
+ myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
|
|
204
|
+ ImageListPath + @"%d.png -i " +
|
|
205
|
+ Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f FLV " +
|
|
206
|
+ VideoSavePath;
|
|
207
|
+ break;
|
|
208
|
+ default:
|
|
209
|
+ myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
|
|
210
|
+ ImageListPath + @"%d.png -i " +
|
|
211
|
+ Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
|
|
212
|
+ VideoSavePath;
|
|
213
|
+ break;
|
|
214
|
+ }
|
|
215
|
+ myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
|
|
216
|
+ myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
|
|
217
|
+ myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
|
|
218
|
+ myProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
|
|
219
|
+ //外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
|
|
220
|
+ myProcess.ErrorDataReceived += new DataReceivedEventHandler(Output);
|
|
221
|
+ myProcess.Start(); //启动线程
|
|
222
|
+ myProcess.BeginErrorReadLine(); //开始异步读取
|
|
223
|
+ myProcess.WaitForExit(); //阻塞等待进程结束
|
|
224
|
+ myProcess.Close(); //关闭进程
|
|
225
|
+ myProcess.Dispose(); //释放资源
|
|
226
|
+ //生成视频后删除图片保存列表
|
|
227
|
+ FileToolsCommon.DeleteDirectory(ImageListPath);
|
|
228
|
+ });
|
|
229
|
+ }
|
|
230
|
+
|
|
231
|
+ /// <summary>
|
|
232
|
+ /// 视频音频合成
|
|
233
|
+ /// </summary>
|
|
234
|
+ /// <param name="FilePath">存储路径</param>
|
|
235
|
+ void MergeVideoOrAudio(object FilePathobj)
|
148
|
236
|
{
|
|
237
|
+ Thread.Sleep(500);
|
|
238
|
+ //路径
|
|
239
|
+ string FilePath = (string)FilePathobj;
|
|
240
|
+ string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
|
149
|
241
|
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
150
|
242
|
Debug.WriteLine(KillProcessArray.Length.ToString());
|
151
|
243
|
foreach (var KillProcess in KillProcessArray)
|
|
@@ -153,35 +245,9 @@ namespace Common.system
|
153
|
245
|
KillProcess.Kill();
|
154
|
246
|
}
|
155
|
247
|
myProcess = new Process();
|
156
|
|
- LogPath=CreateffmpegLog();
|
|
248
|
+ LogPath = CreateffmpegLog();
|
157
|
249
|
this.myProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/ffmpeg/bin/ffmpeg.exe"); //ffmpeg.exe的绝对路径
|
158
|
|
- switch (VideoType.ToUpper())
|
159
|
|
- {
|
160
|
|
- case "MP4":
|
161
|
|
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
|
162
|
|
- ImageListPath + @"%d.png -i " +
|
163
|
|
- Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
|
164
|
|
- VideoSavePath;
|
165
|
|
- break;
|
166
|
|
- case "AVI":
|
167
|
|
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
|
168
|
|
- ImageListPath + @"%d.png -i " +
|
169
|
|
- Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f AVI " +
|
170
|
|
- VideoSavePath;
|
171
|
|
- break;
|
172
|
|
- case "FLV":
|
173
|
|
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
|
174
|
|
- ImageListPath + @"%d.png -i " +
|
175
|
|
- Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -f FLV " +
|
176
|
|
- VideoSavePath;
|
177
|
|
- break;
|
178
|
|
- default:
|
179
|
|
- myProcess.StartInfo.Arguments = @"-y -r " + Frequency + " -i " +
|
180
|
|
- ImageListPath + @"%d.png -i " +
|
181
|
|
- Mp3Path + @" -s " + VideoWidth + "x" + VideoHeight + " -vcodec mpeg4 " +
|
182
|
|
- VideoSavePath;
|
183
|
|
- break;
|
184
|
|
- }
|
|
250
|
+ myProcess.StartInfo.Arguments = "-f concat -safe 0 -i " + Path + "temp/filelist.d -c copy " + FilePath;
|
185
|
251
|
myProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
|
186
|
252
|
myProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
|
187
|
253
|
myProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
|
|
@@ -193,17 +259,44 @@ namespace Common.system
|
193
|
259
|
myProcess.WaitForExit(); //阻塞等待进程结束
|
194
|
260
|
myProcess.Close(); //关闭进程
|
195
|
261
|
myProcess.Dispose(); //释放资源
|
196
|
|
- //生成视频后删除图片保存列表
|
197
|
|
- FileToolsCommon.DeleteDirectory(ImageListPath);
|
198
|
|
-
|
199
|
262
|
}
|
200
|
|
-
|
|
263
|
+
|
|
264
|
+ /// <summary>
|
|
265
|
+ /// 获取文件完整路径
|
|
266
|
+ /// </summary>
|
|
267
|
+ /// <param name="FilePath">文件路径</param>
|
|
268
|
+ /// <returns></returns>
|
|
269
|
+ string GetFilePathName(string FilePath)
|
|
270
|
+ {
|
|
271
|
+ //路径
|
|
272
|
+ string Path = FileToolsCommon.GetDirectoryName(FilePath) + @"\";
|
|
273
|
+ //Path.GetFileName(FilePath);//获取文件名
|
|
274
|
+ string Extension = FileToolsCommon.GetIOExtension(FilePath);//扩展名
|
|
275
|
+ string tempFilePath = Path + "temp/";
|
|
276
|
+ //创建临时目录
|
|
277
|
+ FileToolsCommon.CreateDirectory(tempFilePath);
|
|
278
|
+ //创建记录文件
|
|
279
|
+ if (!FileToolsCommon.IsExistFile(tempFilePath + "filelist.d"))
|
|
280
|
+ {
|
|
281
|
+ FileToolsCommon.CreateFile(tempFilePath + "filelist.d");
|
|
282
|
+ }
|
|
283
|
+ int num = 1;
|
|
284
|
+ string CompleteFilePath = tempFilePath + num + Extension;
|
|
285
|
+ while (FileToolsCommon.IsExistFile(CompleteFilePath))
|
|
286
|
+ {
|
|
287
|
+ num++;
|
|
288
|
+ CompleteFilePath = tempFilePath + num + Extension;
|
|
289
|
+ }
|
|
290
|
+ FileToolsCommon.AppendText(tempFilePath + "filelist.d", "file ./" + num + Extension + "\r\n");
|
|
291
|
+ return CompleteFilePath;
|
|
292
|
+ }
|
|
293
|
+
|
201
|
294
|
/// <summary>
|
202
|
295
|
/// 生成缩略图
|
203
|
296
|
/// </summary>
|
204
|
297
|
/// <param name="VideoPath">视频地址</param>
|
205
|
298
|
/// <param name="ImagePath">图片地址</param>
|
206
|
|
- void GenerateThumbnails(string VideoPath,string ImagePath)
|
|
299
|
+ void GenerateThumbnails(string VideoPath, string ImagePath)
|
207
|
300
|
{
|
208
|
301
|
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
|
209
|
302
|
Debug.WriteLine(KillProcessArray.Length.ToString());
|