Skip to content

Commit 03606db

Browse files
committed
优化FFmpeg命令调用方式
将FFmpeg命令的调用方式从`System.Diagnostics.Process.Start`修改为自定义的`FFmpegCmdProcessStartAndWaitForExit`方法。该方法在执行时隐藏命令窗口,并重定向标准输出和错误输出,提升了执行过程的可控性。同时,所有FFmpeg命令均添加了`-loglevel quiet`选项,以减少输出信息,改善用户体验。
1 parent ba5eaf6 commit 03606db

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

Image2Video/MainWindow.xaml.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ private void All_do_Click(object sender, RoutedEventArgs e)
231231

232232
//合成MP4(无声)
233233
Dispatcher.Invoke(() => thread_text.Text = $"合成无声视频");
234-
System.Diagnostics.Process.Start("ffmpeg.exe", $" -y -r {fps_text} -i {cache_dir}\\%d.jpg {cache_dir}\\output.mp4").WaitForExit();
234+
FFmpegCmdProcessStartAndWaitForExit($" -loglevel quiet -y -r {fps_text} -i {cache_dir}\\%d.jpg {cache_dir}\\output.mp4");
235+
//System.Diagnostics.Process.Start("ffmpeg.exe", $" -loglevel quiet -y -r {fps_text} -i {cache_dir}\\%d.jpg {cache_dir}\\output.mp4").WaitForExit();
235236
GC.Collect();
236237

237238
// 处理MP3 生成和MP4同样长的MP3文件
@@ -255,20 +256,24 @@ private void All_do_Click(object sender, RoutedEventArgs e)
255256
}
256257
Dispatcher.Invoke(() => thread_text.Text = $"拼接合适长度的BGM");
257258
//拼接合适长度的mp3
258-
System.Diagnostics.Process.Start("ffmpeg.exe", $" -i \"{concat_str}\" -y -acodec copy {cache_dir}\\b.mp3").WaitForExit();
259+
FFmpegCmdProcessStartAndWaitForExit($" -loglevel quiet -i \"{concat_str}\" -y -acodec copy {cache_dir}\\b.mp3");
260+
//System.Diagnostics.Process.Start("ffmpeg.exe", $" -loglevel quiet -i \"{concat_str}\" -y -acodec copy {cache_dir}\\b.mp3").WaitForExit();
259261
Dispatcher.Invoke(() => thread_text.Text = $"截取BGM到视频长度");
260262
//截取mp3到mp4长度
261-
System.Diagnostics.Process.Start("ffmpeg.exe", $" -i {cache_dir}\\b.mp3 -t {mp4sec} -y -acodec copy {cache_dir}\\c.mp3").WaitForExit();
263+
FFmpegCmdProcessStartAndWaitForExit($" -loglevel quiet -i {cache_dir}\\b.mp3 -t {mp4sec} -y -acodec copy {cache_dir}\\c.mp3");
264+
//System.Diagnostics.Process.Start("ffmpeg.exe", $" -loglevel quiet -i {cache_dir}\\b.mp3 -t {mp4sec} -y -acodec copy {cache_dir}\\c.mp3").WaitForExit();
262265
}
263266
else if (mp4sec < mp3sec)
264267
{
265268
Dispatcher.Invoke(() => thread_text.Text = $"截取BGM到视频长度");
266269
//截取mp3到mp4长度
267-
System.Diagnostics.Process.Start("ffmpeg.exe", $" -i {cache_dir}\\1.mp3 -y -t {mp4sec} -acodec copy {cache_dir}\\c.mp3").WaitForExit();
270+
FFmpegCmdProcessStartAndWaitForExit($" -loglevel quiet -i {cache_dir}\\1.mp3 -y -t {mp4sec} -acodec copy {cache_dir}\\c.mp3");
271+
//System.Diagnostics.Process.Start("ffmpeg.exe", $" -loglevel quiet -i {cache_dir}\\1.mp3 -y -t {mp4sec} -acodec copy {cache_dir}\\c.mp3").WaitForExit();
268272
}
269273
Dispatcher.Invoke(() => thread_text.Text = $"合成BGM和视频");
270274
//合成mp3和mp4
271-
System.Diagnostics.Process.Start("ffmpeg.exe", $" -i {cache_dir}\\c.mp3 -i {cache_dir}\\output.mp4 -y {cache_dir}\\d.mp4").WaitForExit();
275+
FFmpegCmdProcessStartAndWaitForExit($" -loglevel quiet -i {cache_dir}\\c.mp3 -i {cache_dir}\\output.mp4 -y {cache_dir}\\d.mp4");
276+
//System.Diagnostics.Process.Start("ffmpeg.exe", $" -loglevel quiet -i {cache_dir}\\c.mp3 -i {cache_dir}\\output.mp4 -y {cache_dir}\\d.mp4").WaitForExit();
272277

273278
Dispatcher.Invoke(() => thread_text.Text = $"导出带BGM的视频");
274279
//导出带BGM的mp4
@@ -549,7 +554,27 @@ private int ReadMp4During(string filePath)
549554
Debug.WriteLine($"{filePath}->{dir.GetDetailsOf(item, 27)}{all_sec}秒");
550555
return all_sec;
551556
}
552-
557+
//调用FFmpeg(隐藏cmd窗口 )
558+
static Process FFmpegCmdProcessStartAndWaitForExit(string cmd)
559+
{
560+
ProcessStartInfo startInfo = new ProcessStartInfo
561+
{
562+
FileName = "ffmpeg.exe", // 确保路径正确,或使用绝对路径
563+
Arguments = cmd,
564+
UseShellExecute = false, // 不使用系统外壳启动
565+
CreateNoWindow = true, // 不创建窗口
566+
RedirectStandardOutput = true, // 重定向标准输出
567+
RedirectStandardError = true // 重定向错误输出
568+
};
569+
//Console.WriteLine("Hello, World!");
570+
Process process = new Process();
571+
process.StartInfo = startInfo;
572+
//System.Diagnostics.Process.Start("ffmpeg.exe", " -loglevel quiet -y -i 1.mp3 2.mp3");
573+
//process.Start();
574+
process.Start();
575+
process.WaitForExit();
576+
return process;
577+
}
553578
//获取MP3时长
554579
private int ReadMp3During(string filePath)
555580
{

0 commit comments

Comments
 (0)