VideoConcat/Video.xaml.cs
2024-10-30 22:27:09 +08:00

161 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Expression.Drawing.Core;
using Microsoft.Extensions.Logging;
using Microsoft.Win32;
using System.IO;
using System.Windows;
namespace VideoConcat
{
/// <summary>
/// Video.xaml 的交互逻辑
/// </summary>
public partial class Video : Window
{
public string text = "";
public FileSystemInfo[]? Floders { get; set; }
public string[] Heads = [];
public string[] Tails = [];
public string[] Middles = [];
public DirectoryInfo? Path { get; set; }
public Video()
{
InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
int count = 0;
try
{
count = int.Parse(videoCount.Text);
}
catch (Exception)
{
MessageBox.Show("请输入正确的数字");
return;
}
WriteTxt($"生成视频的个数:{videoCount.Text} 个");
Check_Folder();
startButton.IsEnabled = false;
Array.ForEach(array: Middles, s =>
{
WriteTxt(s);
});
await Task.Run(() =>
{
for (int i = 0; i <= count; 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;
});
});//ProcessVideo.RunTask(processVideoBar, startButton)
}
private void Check_Folder()
{
string currentPath = Directory.GetCurrentDirectory();
Path = Directory.GetParent(currentPath);
if (Path == null)
{
WriteTxt("获取相关视频文件目录失败!");
return;
}
WriteTxt("当前目录为:" + Path.FullName);
try
{
Boolean hasHead = false;
Boolean hasTail = false;
DirectoryInfo dirD = Path as DirectoryInfo;
//获取文件夹下所有文件夹
Floders = dirD.GetDirectories();
//对单个FileSystemInfo进行判断如果是文件夹则进行递归操作
foreach (FileSystemInfo folder in Floders)
{
WriteTxt($"{folder.Name}");
if (folder.Name == "head")
{
hasHead = true;
Heads = GetAllVideos($"{Path}\\{folder.Name}");
continue;
}
if (folder.Name == "tail")
{
hasTail = true;
Tails = GetAllVideos($"{Path}\\{folder.Name}");
continue;
}
Middles = Middles.Concat(GetAllVideos($"{Path}\\{folder.Name}")).ToArray();
}
if (!hasHead)
{
WriteTxt("存在名称为head的文件夹将作为视频开头");
}
if (!hasTail)
{
WriteTxt("存在名称为tail的文件夹将作为视频结尾");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
private void WriteTxt(string str)
{
text += str + "\r\n";
outputTxt.Text = text;
scrowText.ScrollToEnd();
}
public static string[] GetAllVideos(string path)
{
return Directory.GetFiles(path, "*.mp4", SearchOption.AllDirectories);
}
}
}