VideoConcat/ViewModels/ExtractWindowViewModel.cs
2025-08-12 23:11:03 +08:00

126 lines
4.3 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 FFMpegCore;
using FFMpegCore.Enums;
using Microsoft.Expression.Drawing.Core;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
using VideoConcat.Common.Tools;
using VideoConcat.Models;
using VideoConcat.Services.Video;
using static VideoConcat.Models.VideoModel;
using MessageBox = System.Windows.MessageBox;
namespace VideoConcat.ViewModels
{
public class ExtractWindowViewModel
{
private ExtractWindowModel _extractWindowModel;
public List<ConcatVideo> ConcatVideos { get; set; } = [];
public ExtractWindowModel ExtractWindowModel
{
get { return _extractWindowModel; }
set
{
_extractWindowModel = value;
}
}
public ExtractWindowViewModel()
{
ExtractWindowModel = new ExtractWindowModel
{
CanStart = false,
IsStart = false,
IsCanOperate = true,
BtnOpenFolderCommand = new Command()
{
DoExcue = obj =>
{
FolderBrowserDialog folderBrowserDialog = new();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
ExtractWindowModel.FolderPath = folderBrowserDialog.SelectedPath;
LogUtils.Info($"获取视频文件夹,视频路径:{ExtractWindowModel.FolderPath}");
ListFolder();
}
}
},
BtnStartVideoConcatCommand = new Command()
{
DoExcue = obj =>
{
ExtractWindowModel.HelpInfo = "";
SemaphoreSlim semaphore = new(10); // Limit to 3 threads
List<Task> _tasks = [];
ExtractWindowModel.videos.ForEach(async (video) =>
{
await semaphore.WaitAsync(); // Wait when more than 3 threads are running
var _task = Task.Run(async () =>
{
try
{
// 实例化并调用
var remover = new VideoProcess();
// 删除4秒处的帧需根据实际帧位置调整
string _tmpPath = Path.GetDirectoryName(video) ?? "";
string _tmpFileName = $"{(new Random()).Next(10000,99999)}{Path.GetFileName(video)}";
string outPath = Path.Combine(_tmpPath, "out");
if (!Path.Exists(outPath))
{
Directory.CreateDirectory(outPath);
}
await VideoProcess.RemoveFrameRandomeAsync(video, $"{_tmpPath}\\out\\{_tmpFileName}");
}
finally
{
semaphore.Release(); // Work is done, signal to semaphore that more work is possible
}
});
_tasks.Add(_task);
});
Task.WhenAll(_tasks).ContinueWith((task) =>
{
ExtractWindowModel.HelpInfo = "全部完成!";
});
}
}
};
}
private void ListFolder()
{
DirectoryInfo dir = new(ExtractWindowModel.FolderPath);
try
{
DirectoryInfo dirD = dir as DirectoryInfo;
//获取文件夹下所有视频文件
ExtractWindowModel.videos = Directory.GetFiles(dirD.FullName, "*.mp4");
ExtractWindowModel.SetCanStart();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
}