Skip to content

Commit

Permalink
完成对FFmpegOutputPage的改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 8, 2024
1 parent 1fb9002 commit 5921751
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 96 deletions.
4 changes: 2 additions & 2 deletions SimpleFFmpegGUI.Core/Manager/FFmpegManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public class FFmpegManager : INotifyPropertyChanged
private DateTime pauseStartTime;
private FFmpegProcess process;

public FFmpegManager(TaskInfo task, Logger logger)
public FFmpegManager(TaskInfo task)
{
this.task = task;
this.logger = logger;
logger = new Logger(new FFmpegDbContext());
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.Core/Manager/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private IQueryable<TaskInfo> GetQueueTasksQuery(FFmpegDbContext db)

private async Task ProcessTaskAsync(TaskInfo task, bool main)
{
FFmpegManager ffmpegManager = new FFmpegManager(task, logger);
FFmpegManager ffmpegManager = new FFmpegManager(task);

task.Status = TaskStatus.Processing;
task.StartTime = DateTime.Now;
Expand Down
12 changes: 7 additions & 5 deletions SimpleFFmpegGUI.Core/Manager/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SimpleFFmpegGUI.Manager
{
public class TaskManager
{
private readonly bool taskChecked = false;
private static bool taskChecked = false;
private static readonly object lockObj = new object();
private readonly FFmpegDbContext db;
private readonly Logger logger;
Expand All @@ -26,6 +26,7 @@ public TaskManager(FFmpegDbContext db, Logger logger, QueueManager queueManager)
{
lock (lockObj)
{
taskChecked = true;
foreach (var item in db.Tasks.Where(p => p.Status == TaskStatus.Processing))
{
item.Status = TaskStatus.Error;
Expand Down Expand Up @@ -109,7 +110,7 @@ public async Task ResetTaskAsync(int id)
TaskInfo task = await db.Tasks.FindAsync(id) ?? throw new ArgumentException($"找不到ID为{id}的任务");
if (queue.Tasks.Any(p => p.Id == id))
{
throw new Exception("ID为{id}的任务正在进行中");
throw new Exception($"ID为{id}的任务正在进行中");
}
task.Status = TaskStatus.Queue;
db.Update(task);
Expand Down Expand Up @@ -154,17 +155,18 @@ public async Task CancelTaskAsync(int id)

private void CheckCancelingTask(TaskInfo task)
{
int id = task.Id;
if (task.Status == TaskStatus.Cancel)
{
throw new Exception("ID为{id}的任务已被取消");
throw new Exception($"ID为{id}的任务已被取消");
}
if (task.Status == TaskStatus.Done)
{
throw new Exception("ID为{id}的任务已完成");
throw new Exception($"ID为{id}的任务已完成");
}
if (task.Status == TaskStatus.Error)
{
throw new Exception("ID为{id}的任务已完成并出现错误");
throw new Exception($"ID为{id}的任务已完成并出现错误");
}
}

Expand Down
5 changes: 2 additions & 3 deletions SimpleFFmpegGUI.WPF/Pages/FFmpegOutputPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@
xmlns:r="clr-namespace:SimpleFFmpegGUI.WPF"
xmlns:system="clr-namespace:System;assembly=System.Runtime"
xmlns:ui="http://schemas.modernwpf.com/2019"
Loaded="UserControl_Loaded"
mc:Ignorable="d">
<DockPanel>

<ui:CommandBar
Grid.Row="99"
DockPanel.Dock="Bottom">
<ui:AppBarButton
Click="ClearAllButton_Click"
Command="{Binding ClearAllCommand}"
Icon="Clear"
Label="清空" />
<ui:AppBarButton
Click="CopyAllButton_Click"
Command="{Binding CopyAllCommand}"
Icon="Copy"
Label="复制全部内容" />

Expand Down
87 changes: 6 additions & 81 deletions SimpleFFmpegGUI.WPF/Pages/FFmpegOutputPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
using Microsoft.Extensions.DependencyInjection;
using SimpleFFmpegGUI.Dto;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.Model;
using SimpleFFmpegGUI.WPF.Model;
using SimpleFFmpegGUI.WPF.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
Expand All @@ -29,98 +28,24 @@

namespace SimpleFFmpegGUI.WPF.Pages
{
public class FFmpegOutputPageViewModel
public partial class FFmpegOutputPage : UserControl
{
public FFmpegOutputPageViewModel()
public FFmpegOutputPage()
{
Logger.Log += Logger_Log;
}
private async void Logger_Log(object sender, LogEventArgs e)
{
await App.Current.Dispatcher.InvokeAsync(() =>
{
if (e.Log.Message.StartsWith("frame=")
&& Outputs.Count > 0 && Outputs[^1].Message.StartsWith("frame="))
{
Outputs[^1] = e.Log;
}
else
{
Outputs.Add(e.Log);
}
});
}

public ObservableCollection<Log> Outputs { get; } = new ObservableCollection<Log>();
}

/// <summary>
/// Interaction logic for FFmpegOutputPage.xaml
/// </summary>
public partial class FFmpegOutputPage : UserControl
{
public FFmpegOutputPage(FFmpegOutputPageViewModel viewModel)
{
ViewModel = viewModel;
DataContext = ViewModel;
ViewModel = this.SetDataContext<FFmpegOutputPageViewModel>();
InitializeComponent();
ViewModel.Outputs.CollectionChanged += Outputs_CollectionChanged;
}

public FFmpegOutputPageViewModel ViewModel { get; set; }

private void Outputs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
{
scr.ScrollToEnd();
}
}

private async void Logger_Log(object sender, LogEventArgs e)
{
await Dispatcher.InvokeAsync(() =>
{
if (e.Log.Message.StartsWith("frame=")
&& ViewModel.Outputs.Count > 0 && ViewModel.Outputs[^1].Message.StartsWith("frame="))
{
ViewModel.Outputs[^1] = e.Log;
}
else
{
bool end = scr.VerticalOffset == scr.ScrollableHeight;
ViewModel.Outputs.Add(e.Log);
if (end)
{
scr.ScrollToEnd();
}
}
});
}

public FFmpegOutputPageViewModel ViewModel { get; set; }

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
this.GetWindow().Closing += (s, e) => Logger.Log -= Logger_Log;
}

private void ClearAllButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.Outputs.Clear();
}

private void CopyAllButton_Click(object sender, RoutedEventArgs e)
{
try
{
Clipboard.SetText(string.Join(Environment.NewLine,ViewModel.Outputs.Select(p=>p.Message)));
this.CreateMessage().QueueSuccess("已复制内容到剪贴板");
}
catch (Exception ex)
{
this.CreateMessage().QueueError("复制内容失败", ex);
}
}

private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
try
Expand Down
2 changes: 1 addition & 1 deletion SimpleFFmpegGUI.WPF/Panels/TaskList.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
Margin="-8,-4"
Padding="8,4"
Background="Transparent"
Command="{Binding DataContext.ShowArgumentsCommand,ElementName=root}"
Command="{Binding DataContext.ShowArgumentsCommand, ElementName=root}"
Cursor="Hand">
<TextBlock TextDecorations="Underline">详细参数</TextBlock>
</Button>
Expand Down
6 changes: 3 additions & 3 deletions SimpleFFmpegGUI.WPF/TestWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private async Task CreateRefVideosAsync(string input, string[] sizes)
{
continue;
}
runningFFmpeg = new FFmpegManager(task, logger);
runningFFmpeg = new FFmpegManager(task);
runningFFmpeg.StatusChanged += (s, e) =>
{
var status = runningFFmpeg.GetStatus();
Expand Down Expand Up @@ -452,7 +452,7 @@ private async Task TestAsync(string input)
string qctext = ViewModel.QCMode == 0 ? $"bitrate={test.MBitrate}M&factor={codec.BitrateFactor}" : $"crf={codec.CRF}";
task.Output = Path.GetFullPath($"{TestDir}/codec={codec.Name}&size={sizeTexts[j]}&speed={codec.CpuSpeed}&{qctext}.mp4");

runningFFmpeg = new FFmpegManager(task, logger);
runningFFmpeg = new FFmpegManager(task);
runningFFmpeg.StatusChanged += (s, e) =>
{
var status = runningFFmpeg.GetStatus();
Expand Down Expand Up @@ -508,7 +508,7 @@ private async Task TestAsync(string input)
ViewModel.DetailProgress = 0;


runningFFmpeg = new FFmpegManager(qualityTask, logger);
runningFFmpeg = new FFmpegManager(qualityTask);
runningFFmpeg.StatusChanged += (s, e) =>
{
var status = runningFFmpeg.GetStatus();
Expand Down
54 changes: 54 additions & 0 deletions SimpleFFmpegGUI.WPF/ViewModels/FFmpegOutputPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using CommunityToolkit.Mvvm.Input;
using SimpleFFmpegGUI.Model;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace SimpleFFmpegGUI.WPF.ViewModels
{
public partial class FFmpegOutputPageViewModel : ViewModelBase
{
public FFmpegOutputPageViewModel()
{
Logger.Log += Logger_Log;
}
public ObservableCollection<Log> Outputs { get; } = new ObservableCollection<Log>();

[RelayCommand]
private void ClearAll()
{
Outputs.Clear();
}

[RelayCommand]
private void CopyAll()
{
try
{
Clipboard.SetText(string.Join(Environment.NewLine, Outputs.Select(p => p.Message)));
QueueSuccessMessage("已复制内容到剪贴板");
}
catch (Exception ex)
{
QueueErrorMessage("复制内容失败", ex);
}
}

private async void Logger_Log(object sender, LogEventArgs e)
{
await System.Windows.Application.Current.Dispatcher.InvokeAsync(() =>
{
if (e.Log.Message.StartsWith("frame=")
&& Outputs.Count > 0 && Outputs[^1].Message.StartsWith("frame="))
{
Outputs[^1] = e.Log;
}
else
{
Outputs.Add(e.Log);
}
});
}
}
}

0 comments on commit 5921751

Please sign in to comment.