From 788ae8ab5f6ea9233caa89c3e99af2b2908a43d6 Mon Sep 17 00:00:00 2001 From: xiang Date: Sat, 2 Nov 2024 23:20:44 +0800 Subject: [PATCH] update --- Video.xaml.cs | 258 ++++++++++++++++++++++++++------------------------ 1 file changed, 136 insertions(+), 122 deletions(-) diff --git a/Video.xaml.cs b/Video.xaml.cs index cd9eee5..2d7bd85 100644 --- a/Video.xaml.cs +++ b/Video.xaml.cs @@ -1,7 +1,12 @@ using System.Diagnostics; using System.IO; +using System.Linq; +using System.Text; using System.Windows; +using System.Windows.Shapes; +using WPFDevelopers.Helpers; using static System.Net.WebRequestMethods; +using File = System.IO.File; namespace VideoConcat { @@ -11,17 +16,28 @@ namespace VideoConcat public partial class Video : Window { public string text = "", outPut = ""; - public FileSystemInfo[]? Floders { get; set; } - public string[] Heads = []; - public string[] Tails = []; - public List Middles = []; - public DirectoryInfo? Path { get; set; } + public Dictionary videoFolders = []; + + public DirectoryInfo Path; + + public Dictionary AllPublicCombinVideos = []; + + int GenerateVideoCount = 0; + + + public Video() { InitializeComponent(); + string currentPath = Directory.GetCurrentDirectory(); + DirectoryInfo ExecParentPath = Directory.GetParent(currentPath) ?? new DirectoryInfo(""); + if (ExecParentPath != null) + { + Path = ExecParentPath; + } } @@ -29,10 +45,10 @@ namespace VideoConcat { - int count = 0; + try { - count = int.Parse(videoCount.Text); + GenerateVideoCount = int.Parse(videoCount.Text); } catch (Exception) { @@ -41,7 +57,7 @@ namespace VideoConcat return; } - WriteTxt($"生成视频的个数:{videoCount.Text} 个"); + WriteTxt($"生成视频的个数:{GenerateVideoCount} 个"); Check_Folder(); @@ -49,107 +65,64 @@ namespace VideoConcat MakeOutPutDir(); startButton.IsEnabled = false; - - - - - - + GenerateAllvideos(videoFolders); + var dateStart = DateTime.Now; //记录用时的起始时间 await Task.Run(async () => { - var dateStart = DateTime.Now; //记录用时的起始时间 - - for (int i = 0; i <= count; i++) + List> lists = GenerateAllvideos(videoFolders); - { + await MockIOPerformanceAsync(lists); - await MockIOPerformanceAsync(Middles[i]); - //System.Windows.Application.Current.Dispatcher.Invoke(() => - //{ - // processVideoBar.Dispatcher.Invoke(() => - // { - // processVideoBar.Value = i; - - // }); - // outputTxt.Dispatcher.Invoke(new Action(() => - // { - // WriteTxt("正在生成第" + i + "个视频"); - // })); - //}); - //System.Threading.Thread.Sleep(50); - } startButton.Dispatcher.Invoke(() => { startButton.IsEnabled = true; }); - var dateEnd = DateTime.Now; - var timeSpan = dateEnd - dateStart;//记录开票用时 + }); + var dateEnd = DateTime.Now; + var timeSpan = dateEnd - dateStart;//记录开票用时 - });//ProcessVideo.RunTask(processVideoBar, startButton) + WriteTxt($"用时:{timeSpan.TotalSeconds}秒"); } private void Check_Folder() { - string currentPath = Directory.GetCurrentDirectory(); - Path = Directory.GetParent(currentPath); if (Path == null) { WriteTxt("获取相关视频文件目录失败!"); return; } WriteTxt("当前目录为:" + Path.FullName); + videoFolders.Clear(); + AllPublicCombinVideos.Clear(); + try { - Boolean hasHead = false; - Boolean hasTail = false; - - DirectoryInfo dirD = Path as DirectoryInfo; - //获取文件夹下所有文件夹 - Floders = dirD.GetDirectories(); - - //对单个FileSystemInfo进行判断,如果是文件夹则进行递归操作 - - foreach (FileSystemInfo folder in Floders) + int k = 1; + for (int i = 1; i < 100; i++) { - WriteTxt($"{folder.Name}"); - if (folder.Name == "head") + var folder = string.Format("{0:D2}", i); + string currentFolderPath = $"{Path}\\{folder}"; + if (Directory.Exists(currentFolderPath)) { - hasHead = true; - Heads = GetAllVideos($"{Path}\\{folder.Name}"); - continue; - + WriteTxt(folder); + string[] videos = GetAllVideos(currentFolderPath); + if (videos.Length > 0) + { + videoFolders.Add(k, videos); + AllPublicCombinVideos.Add(k, new A()); + k++; + } } - if (folder.Name == "tail") - { - hasTail = true; - Tails = GetAllVideos($"{Path}\\{folder.Name}"); - continue; - } - - string[] files = GetAllVideos($"{Path}\\{folder.Name}"); - Middles.Add(files); - + } - if (!hasHead) - { - WriteTxt("存在名称为head的文件夹,将作为视频开头!"); - } - - if (!hasTail) - { - WriteTxt("存在名称为tail的文件夹,将作为视频结尾!"); - } - - Middles.Insert(0, Heads); - Middles.Add(Tails); - + return; } catch (Exception ex) { @@ -160,9 +133,14 @@ namespace VideoConcat private void WriteTxt(string str) { - text += str + "\r\n"; - outputTxt.Text = text; - scrowText.ScrollToEnd(); + + outputTxt.Dispatcher.Invoke(() => + { + text += str + "\r\n"; + outputTxt.Text = text; + scrowText.ScrollToEnd(); + }); + } public static string[] GetAllVideos(string path) @@ -170,49 +148,28 @@ namespace VideoConcat return Directory.GetFiles(path, "*.mp4", SearchOption.AllDirectories); } - /// - /// 执行 - /// C# Process进程调用 https://docs.microsoft.com/zh-cn/dotnet/api/system.diagnostics.process?view=net-5.0 - /// - /// 执行命令 - 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) + public void CombineMp4WithoutTxt(List files, string StrOutMp4Path) { Process p = new();//建立外部调用线程 p.StartInfo.FileName = Directory.GetCurrentDirectory() + "\\ffmpeg.exe";//要调用外部程序的绝对路径 - string combineFile = ""; + //int i = 0; + //string vao = ""; + List ooootext = []; + + files.ForEach(file => { + ooootext.Add($"file '{file}'"); + }); + string tmpConcatFile = $"{outPut}\\{Guid.NewGuid().ToString()}"; + // 也可以指定编码方式 + File.WriteAllLines(tmpConcatFile, ooootext, Encoding.ASCII); - foreach (string file in files) - { - combineFile += $"| {file}"; + - } - - string StrArg = $"-i concat:\"{combineFile}\" -vcodec copy -acodec copy " + StrOutMp4Path + " -y"; + string StrArg = $"-f concat -safe 0 -i {tmpConcatFile} -c copy -bsf:a aac_adtstoasc -movflags +faststart {StrOutMp4Path}"; + //string StrArg = $"-i {combineFile} -filter_complex \"{vao} concat=n={i}:v=1:a=1 [v][a]\" -map \"[v]\" -map \"[a]\" {StrOutMp4Path}"; p.StartInfo.Arguments = StrArg; @@ -238,7 +195,7 @@ namespace VideoConcat public void MakeOutPutDir() { - outPut = $"{Path}\\output"; + outPut = $"{Path.FullName}\\output"; if (!Directory.Exists(outPut)) { @@ -246,27 +203,84 @@ namespace VideoConcat } } - public static async Task> MockIOPerformanceAsync(string[] ls) + public async Task> MockIOPerformanceAsync(List> ls) { - List lss = new List(); - List tasks = new List(); + List lss = []; + List tasks = []; + foreach (var item in ls) { - Task task = new Task(() => + Task task = new(() => { - Debug.WriteLine(Thread.GetCurrentProcessorId()); - lss.Add(item); + DateTime now = DateTime.Now; + Random random = new Random(); + string randomString = random.Next(100000, 999999).ToString(); + string result = now.ToString("yyyyMMddHHmmss") + randomString; + CombineMp4WithoutTxt(item, $"{outPut}\\{result}.mp4"); }); + tasks.Add(task); task.Start(); + if (tasks.Count >= GenerateVideoCount) + { + break; + } } + foreach (var item in tasks) { await item; } + return lss; } + + private List> GenerateAllvideos(Dictionary dict) + { + + List> ls = []; + foreach (var item in dict) + { + ls.Add(new List(item.Value)); + } + List> permutations = CalculatePermutations(ls); + + + return permutations; + } + + static List> CalculatePermutations(List> inputList) + { + List> result = new List>(); + + // 递归计算全排列的辅助函数 + void Permute(List currentPermutation, int index) + { + if (index == inputList.Count) + { + result.Add(new List(currentPermutation)); + return; + } + + foreach (string item in inputList[index]) + { + currentPermutation.Add(item); + Permute(currentPermutation, index + 1); + currentPermutation.RemoveAt(currentPermutation.Count - 1); + } + } + + Permute(new List(), 0); + + return result; + } } + + public class A + { + public int Index { get; set; } + }; + }