diff --git a/Models/VideoModel.cs b/Models/VideoModel.cs index b8ff960..ad8cf1d 100644 --- a/Models/VideoModel.cs +++ b/Models/VideoModel.cs @@ -10,6 +10,7 @@ using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows.Input; +using System.Windows.Threading; using System.Xml.Linq; using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header; @@ -25,7 +26,9 @@ namespace VideoConcat.Models private bool _isStart = false; private ObservableCollection _FolderInfos = []; private ObservableCollection _concatVideos = []; + private Dispatcher _dispatcher; + public static int Index= 0; public bool IsStart { @@ -38,6 +41,15 @@ namespace VideoConcat.Models } } + public Dispatcher Dispatcher + { + get => _dispatcher; + set + { + _dispatcher = value; + } + } + public int Num { get => _num; @@ -77,14 +89,14 @@ namespace VideoConcat.Models } - public struct ConcatVideo + public class ConcatVideo { - public string FileName { set; get; } + public int Index { set; get; } + public string FileName { set; get; } = ""; public int Size { set; get; } - public int Seconds { set; get; } + public double Seconds { set; get; } public int Status { set; get; } public int Progress { set; get; } - } public ObservableCollection FolderInfos @@ -112,7 +124,16 @@ namespace VideoConcat.Models get { return _concatVideos; } set { + if (_concatVideos != null) + { + _concatVideos.CollectionChanged -= ItemList_CollectionChanged; + } _concatVideos = value; + if (_concatVideos != null) + { + _concatVideos.CollectionChanged += ItemList_CollectionChanged; + } + OnPropertyChanged(); } } @@ -187,5 +208,11 @@ namespace VideoConcat.Models CanStart = false; } } + + public VideoModel() + { + ConcatVideos = []; + _dispatcher = Dispatcher.CurrentDispatcher; + } } } diff --git a/ViewModels/VideoViewModel.cs b/ViewModels/VideoViewModel.cs index 14016dc..077244d 100644 --- a/ViewModels/VideoViewModel.cs +++ b/ViewModels/VideoViewModel.cs @@ -6,12 +6,17 @@ using System.IO; using Microsoft.Expression.Drawing.Core; using FFMpegCore; using FFMpegCore.Enums; +using static VideoConcat.Models.VideoModel; +using System.Windows.Threading; namespace VideoConcat.ViewModels { public class VideoViewModel { private VideoModel _videoModel; + + public List ConcatVideos { get; set; } = []; + public VideoModel VideoModel { get { return _videoModel; } @@ -31,6 +36,7 @@ namespace VideoConcat.ViewModels ConcatVideos = [], MaxNum = 0, CanStart = false, + BtnOpenFolderCommand = new Command() { DoExcue = obj => @@ -78,6 +84,10 @@ namespace VideoConcat.ViewModels { Task.Run(action: async () => { + VideoModel.Dispatcher.Invoke(() => + { + VideoModel.ConcatVideos.Clear(); + }); MessageBox.Show("开始合并视频"); if (Directory.Exists($"{VideoModel.FolderPath}\\output") == false) { @@ -93,7 +103,7 @@ namespace VideoConcat.ViewModels List> videoLists = []; - + VideoModel.FolderInfos.ForEach(folderInfo => { videoLists.Add(folderInfo.VideoPaths); @@ -161,18 +171,40 @@ namespace VideoConcat.ViewModels { try { - string _outPutName = $"{VideoModel.FolderPath}\\output\\{DateTime.Now:yyyyMMddHHmmss}{random.Next(100000, 999999)}.mp4"; + string _tempFileName = $"{DateTime.Now:yyyyMMddHHmmss}{random.Next(100000, 999999)}.mp4"; + string _outPutName = Path.Combine($"{VideoModel.FolderPath}", "output", _tempFileName); ; - var temporaryVideoParts = combination.Select(_ => { + //int _size = 0; + double _duration = 0; + + var temporaryVideoParts = combination.Select(_ => + { string _tempPath = Path.GetDirectoryName(_) ?? ""; + IMediaAnalysis _mediaInfo =FFProbe.Analyse(_); + _duration += _mediaInfo.Duration.TotalSeconds; + + //GlobalFFOptions.Current.TemporaryFilesFolder return Path.Combine(_tempPath, $"{Path.GetFileNameWithoutExtension(_)}{FileExtension.Ts}"); }).ToArray(); bool _isSuccess = false; + VideoModel.Dispatcher.Invoke(() => + { + VideoModel.ConcatVideos.Add(new ConcatVideo() + { + Index = VideoModel.ConcatVideos.Count + 1, + FileName = _tempFileName, + Size = 100, + Seconds = _duration, + Status = 1, + Progress = 100, + }); + }); + try { - _isSuccess= FFMpegArguments + _isSuccess = FFMpegArguments .FromConcatInput(temporaryVideoParts) .OutputToFile(_outPutName, true, options => options .CopyChannel() @@ -221,11 +253,11 @@ namespace VideoConcat.ViewModels } - LogUtils.Info($"当前视频{string.Join(",", combination)}合并成功"); + LogUtils.Info($"当前视频: {string.Join(";", combination)} 合并成功"); } catch (Exception ex) { - LogUtils.Error($"视频{string.Join(",", combination)}合并失败", ex); + LogUtils.Error($"视频:{string.Join(";", combination)} 合并失败", ex); } finally { @@ -242,9 +274,10 @@ namespace VideoConcat.ViewModels //结束时间 DateTime endTime = DateTime.Now; LogUtils.Info($"所有视频拼接完成,用时{(endTime - startTime).TotalSeconds}秒"); + VideoModel.IsStart = false; - MessageBox.Show("所有视频拼接完成"); VideoCombine.Cleanup(_clearPath); + MessageBox.Show("所有视频拼接完成"); }); }); } diff --git a/Views/Video.xaml b/Views/Video.xaml index 9f53147..c62beb2 100644 --- a/Views/Video.xaml +++ b/Views/Video.xaml @@ -50,7 +50,7 @@ - + @@ -70,20 +70,14 @@ - + - - - - - - - +