This commit is contained in:
xiang 2024-10-31 21:34:57 +08:00
parent 77ee5390b7
commit 94d12c1a86

View File

@ -1,10 +1,7 @@
using Microsoft.Expression.Drawing.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
using System.Diagnostics;
using System.IO;
using System.Windows;
namespace VideoConcat
{
/// <summary>
@ -16,7 +13,7 @@ namespace VideoConcat
public FileSystemInfo[]? Floders { get; set; }
public string[] Heads = [];
public string[] Tails = [];
public string[] Middles = [];
public string[][] Middles = [];
public DirectoryInfo? Path { get; set; }
@ -48,6 +45,8 @@ namespace VideoConcat
Check_Folder();
MakeOutPutDir();
startButton.IsEnabled = false;
@ -56,6 +55,20 @@ namespace VideoConcat
WriteTxt(s);
});
var dateStart = DateTime.Now; //记录用时的起始时间
List<List<string>> fpList = new List<List<string>>();
foreach (var item in await MockIOPerformanceAsync)
{
var dateEnd = DateTime.Now;
var timeSpan = dateEnd - dateStart;//记录开票用时
DebugText += item + " " + timeSpan.TotalSeconds + "\r\n";
}
await Task.Run(() =>
{
@ -156,5 +169,104 @@ namespace VideoConcat
{
return Directory.GetFiles(path, "*.mp4", SearchOption.AllDirectories);
}
/// <summary>
/// 执行
/// C# Process进程调用 https://docs.microsoft.com/zh-cn/dotnet/api/system.diagnostics.process?view=net-5.0
/// </summary>
/// <param name="commandStr">执行命令</param>
public static void CommandManager(string commandStr)
{
try
{
using (Process process = new Process())
{
process.StartInfo.FileName = @"D:\FFmpeg\ffmpeg.exe";//要执行的程序名称(属性获取或设置要启动的应用程序或文档。FileName 属性不需要表示可执行文件。 它可以是其扩展名已经与系统上安装的应用程序关联的任何文件类型。)
process.StartInfo.Arguments = " " + commandStr;//启动该进程时传递的命令行参数
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = false;//可能接受来自调用程序的输入信息
process.StartInfo.RedirectStandardOutput = false;//由调用程序获取输出信息
process.StartInfo.RedirectStandardError = false;//重定向标准错误输出
process.StartInfo.CreateNoWindow = false;//不显示程序窗口
process.Start();//启动程序
process.WaitForExit();//等待程序执行完退出进程(避免进程占用文件或者是合成文件还未生成)*
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
public void CombineMp4WithoutTxt(string[] files, string StrOutMp4Path)
{
Process p = new();//建立外部调用线程
p.StartInfo.FileName = Directory.GetCurrentDirectory() + "\\ffmpeg.exe";//要调用外部程序的绝对路径
string combineFile = "";
foreach (string file in files)
{
combineFile += $"| {file}";
}
string StrArg = $"-i concat:\"{combineFile}\" -vcodec copy -acodec copy " + StrOutMp4Path + " -y";
p.StartInfo.Arguments = StrArg;
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序启动线程(一定为FALSE,详细的请看MSDN)
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,FFMPEG的所有输出信息,都为错误输出流,用StandardOutput是捕获不到任何消息的...这是我耗费了2个多月得出来的经验...mencoder就是用standardOutput来捕获的)
p.StartInfo.CreateNoWindow = true;//不创建进程窗口
p.ErrorDataReceived += new DataReceivedEventHandler(Output);//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//阻塞等待进程结束
p.Close();//关闭进程
p.Dispose();//释放资源
}
private void Output(object sendProcess, DataReceivedEventArgs output)
{
if (!String.IsNullOrEmpty(output.Data))
{
WriteTxt(output.Data);
}
}
public void MakeOutPutDir()
{
string outPut = $"{Path}\\output";
if (!Directory.Exists(outPut))
{
Directory.CreateDirectory(outPut);
}
}
public static async Task<IEnumerable<string>> MockIOPerformanceAsync(List<string> ls)
{
List<string> lss = new List<string>();
List<Task> tasks = new List<Task>();
foreach (var item in ls)
{
Task task = new Task(() =>
{
Debug.WriteLine(Thread.GetCurrentProcessorId());
lss.Add(item);
});
tasks.Add(task);
task.Start();
}
foreach (var item in tasks)
{
await item;
}
return lss;
}
}
}