This commit is contained in:
xiang 2024-11-19 21:40:09 +08:00
parent abd1c132a4
commit fb8eb20b74

View File

@ -1,4 +1,5 @@
using System; using Standard;
using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -6,8 +7,6 @@ using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Shapes; using System.Windows.Shapes;
using WPFDevelopers.Helpers; using WPFDevelopers.Helpers;
using File = System.IO.File; using File = System.IO.File;
@ -73,7 +72,7 @@ namespace VideoConcat
MakeOutPutDir(); MakeOutPutDir();
startButton.IsEnabled = false; startButton.IsEnabled = false;
GenerateAllvideos(videoFolders); //GenerateAllvideos(videoFolders);
var dateStart = DateTime.Now; //记录用时的起始时间 var dateStart = DateTime.Now; //记录用时的起始时间
await Task.Run(async () => await Task.Run(async () =>
{ {
@ -94,6 +93,26 @@ namespace VideoConcat
var dateEnd = DateTime.Now; var dateEnd = DateTime.Now;
var timeSpan = dateEnd - dateStart;//记录开票用时 var timeSpan = dateEnd - dateStart;//记录开票用时
try
{
string mp4Path = $"{Directory.GetCurrentDirectory()}\\temp\\";
DirectoryInfo directoryInfo = new DirectoryInfo(mp4Path);
FileInfo[] files = directoryInfo.GetFiles("*.mp4"); // 获取指定目录下的所有 txt 文件
foreach (FileInfo file in files)
{
file.Delete(); // 删除文件
}
}
catch (Exception)
{
}
WriteTxt($"用时:{timeSpan.TotalSeconds}秒"); WriteTxt($"用时:{timeSpan.TotalSeconds}秒");
} }
@ -128,7 +147,7 @@ namespace VideoConcat
string[] videos = GetAllVideos(currentFolderPath); string[] videos = GetAllVideos(currentFolderPath);
if (videos.Length > 0) if (videos.Length > 0)
{ {
if(totalVideoCount == 0) if (totalVideoCount == 0)
{ {
totalVideoCount = 1; totalVideoCount = 1;
} }
@ -176,22 +195,23 @@ namespace VideoConcat
public void CombineMp4WithoutTxt(List<string> files, string StrOutMp4Path) public void CombineMp4WithoutTxt(List<string> files, string StrOutMp4Path)
{ {
Process p = new();//建立外部调用线程 var random = new Random();
p.StartInfo.FileName = Directory.GetCurrentDirectory() + "\\ffmpeg.exe";//要调用外部程序的绝对路径
//int i = 0; //int i = 0;
//string vao = ""; //string vao = "";
List<string> ooootext = []; List<string> ooootext = [];
files.Reverse();
files.ForEach(file => files.ForEach(file =>
{ {
//#file = System.Web.HttpUtility.UrlEncode(file); string tmpOut = $"{Directory.GetCurrentDirectory()}\\temp\\{Guid.NewGuid()}{random.Next(100000, 999999)}.mp4";
ooootext.Add($"file '{file}'"); // ffmpeg.exe - i a.mp4 - ac 1 - ar 48000 - vcodec copy a_re.mp4
Run_Command($"-i \"{file}\" -c:v libx264 -c:a aac {tmpOut}");
ooootext.Add($"file '{tmpOut}'");
}); });
var random = new Random(); log.Info(ooootext);
string tmpConcatFile = $"{outPut}\\{Guid.NewGuid()}{random.Next(100000, 999999)}.aaa";
string tmpConcatFile = $"{Directory.GetCurrentDirectory()}\\temp\\{Guid.NewGuid()}{random.Next(100000, 999999)}";
// 也可以指定编码方式 // 也可以指定编码方式
//File.WriteAllLines(tmpConcatFile, ooootext, Encoding.UTF8); //File.WriteAllLines(tmpConcatFile, ooootext, Encoding.UTF8);
@ -200,21 +220,10 @@ namespace VideoConcat
string StrArg = $"-f concat -safe 0 -i {tmpConcatFile} -bsf:a aac_adtstoasc -movflags +faststart -c copy {StrOutMp4Path}"; string StrArg = $"-f concat -safe 0 -i {tmpConcatFile} -c copy {StrOutMp4Path}";
//string StrArg = $"-i {combineFile} -filter_complex \"{vao} concat=n={i}:v=1:a=1 [v][a]\" -map \"[v]\" -map \"[a]\" {StrOutMp4Path}"; //string StrArg = $"-i {combineFile} -filter_complex \"{vao} concat=n={i}:v=1:a=1 [v][a]\" -map \"[v]\" -map \"[a]\" {StrOutMp4Path}";
Run_Command(StrArg);
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();//释放资源
if (File.Exists(tmpConcatFile)) if (File.Exists(tmpConcatFile))
{ {
try try
@ -229,6 +238,29 @@ namespace VideoConcat
} }
public void Run_Command(string command)
{
Process p = new();//建立外部调用线程
p.StartInfo.FileName = Directory.GetCurrentDirectory() + "\\ffmpeg.exe";//要调用外部程序的绝对路径
p.StartInfo.Arguments = command;
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) private void Output(object sendProcess, DataReceivedEventArgs output)
{ {
if (!String.IsNullOrEmpty(output.Data)) if (!String.IsNullOrEmpty(output.Data))