VideoConcat/ViewModels/VideoViewModel.cs
2025-01-12 00:17:40 +08:00

140 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using VideoConcat.Models;
using System.Windows;
using System.Windows.Forms;
using MessageBox = System.Windows.MessageBox;
using System.ComponentModel;
using VideoConcat.Common.Tools;
using System.IO;
using Microsoft.Expression.Drawing.Core;
using FFMpegCore;
namespace VideoConcat.ViewModels
{
public class VideoViewModel
{
private VideoModel _videoModel;
public VideoModel VideoModel
{
get { return _videoModel; }
set
{
_videoModel = value;
}
}
public VideoViewModel()
{
VideoModel = new VideoModel();
VideoModel.BtnOpenFolderCommand = new BtnOpenFolderCommand()
{
DoExcue = obj =>
{
FolderBrowserDialog folderBrowserDialog = new();
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
VideoModel.FolderPath = folderBrowserDialog.SelectedPath;
LogUtils.Info($"获取视频文件夹,视频路径:{VideoModel.FolderPath}");
ListFolder(VideoModel.FolderPath);
}
}
};
VideoModel.BtnStartVideoConcatCommand = new BtnStartVideoConcatCommand()
{
DoExcue = obj =>
{
LogUtils.Info("开始合并视频");
List<List<string>> combinations = [];
List<string> currentCombination = [];
List<List<string>> videoLists = [];
VideoModel.FolderInfos.ForEach(folderInfo =>
{
videoLists.Add(folderInfo.VideoPaths);
});
VideoCombine.GenerateCombinations(videoLists, 0, currentCombination, combinations);
int combinationIndex = 1;
foreach (List<string> combination in combinations)
{
Console.Write($"组合 {combinationIndex++}: ");
foreach (string video in combination)
{
Console.Write(video + " ");
}
Console.WriteLine();
FFMpeg.Join("./1.mp4",combination.ToArray());
}
Console.WriteLine($"总共有 {combinations.Count} 种拼接方案。");
}
};
}
private void ListFolder(string path)
{
DirectoryInfo dir = new(path);
try
{
DirectoryInfo dirD = dir as DirectoryInfo;
DirectoryInfo[] folders = dirD.GetDirectories();
VideoModel.FolderInfos.Clear();
//获取文件夹下所有视频文件
foreach (DirectoryInfo Folder in folders)
{
string[] files = Directory.GetFiles(Folder.FullName, "*.mp4");
LogUtils.Info($"{Folder.Name}下有{files.Length}个视频文件");
VideoModel.FolderInfos.Add(new VideoModel.FolderInfo { DirectoryInfo = Folder, Num = files.Length, VideoPaths = new List<string>(files) });
}
VideoModel.UpdateSum();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
}
class BtnOpenFolderCommand : ICommand
{
public event EventHandler? CanExecuteChanged;
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter)
{
DoExcue?.Invoke(parameter);
}
public Action<Object?>? DoExcue { get; set; }
}
class BtnStartVideoConcatCommand : ICommand
{
public event EventHandler? CanExecuteChanged;
public bool CanExecute(object? parameter) => true;
public void Execute(object? parameter)
{
DoExcue?.Invoke(parameter);
}
public Action<Object?>? DoExcue { get; set; }
}
}