using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.IO.Pipelines;
using System.Threading.Tasks;
using System.Windows;
using BililiveRecorder.Flv;
using BililiveRecorder.Flv.Grouping;
using BililiveRecorder.Flv.Parser;
using BililiveRecorder.Flv.Pipeline;
using BililiveRecorder.Flv.Writer;
using BililiveRecorder.Flv.Xml;
using BililiveRecorder.WPF.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.WindowsAPICodePack.Dialogs;
using Serilog;
#nullable enable
namespace BililiveRecorder.WPF.Pages
{
///
/// Interaction logic for ToolboxAutoFixPage.xaml
///
public partial class ToolboxAutoFixPage
{
private static readonly ILogger logger = Log.ForContext();
public ToolboxAutoFixPage()
{
this.InitializeComponent();
}
private void SelectFile_Button_Click(object sender, RoutedEventArgs e)
{
// var title = LocExtension.GetLocalizedValue("BililiveRecorder.WPF:Strings:WorkDirectorySelector_Title");
var title = "选择要修复的文件";
var fileDialog = new CommonOpenFileDialog()
{
Title = title,
IsFolderPicker = false,
Multiselect = false,
AllowNonFileSystemItems = false,
AddToMostRecentlyUsedList = false,
EnsurePathExists = true,
EnsureFileExists = true,
NavigateToShortcut = true,
Filters =
{
new CommonFileDialogFilter("Flv files",".flv")
}
};
if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
this.FileNameTextBox.Text = fileDialog.FileName;
}
}
private async void Fix_Button_Click(object sender, RoutedEventArgs e)
{
AutoFixProgressDialog? progressDialog = null;
try
{
var inputPath = this.FileNameTextBox.Text;
if (string.IsNullOrWhiteSpace(inputPath) || !File.Exists(inputPath))
return;
logger.Debug("修复文件 {Path}", inputPath);
progressDialog = new AutoFixProgressDialog();
var showTask = progressDialog.ShowAsync();
IFlvWriterTargetProvider? targetProvider = null;
{
var title = "选择保存位置";
var fileDialog = new CommonSaveFileDialog()
{
Title = title,
AddToMostRecentlyUsedList = false,
EnsurePathExists = true,
EnsureValidNames = true,
NavigateToShortcut = true,
OverwritePrompt = false,
DefaultDirectory = Path.GetDirectoryName(inputPath),
DefaultFileName = Path.GetFileName(inputPath)
};
if (fileDialog.ShowDialog() == CommonFileDialogResult.Ok)
targetProvider = new AutoFixFlvWriterTargetProvider(fileDialog.FileName);
else
return;
}
using var inputStream = File.OpenRead(inputPath);
var memoryStreamProvider = new DefaultMemoryStreamProvider();
using var grouping = new TagGroupReader(new FlvTagPipeReader(PipeReader.Create(inputStream), memoryStreamProvider, skipData: false, logger: logger));
using var writer = new FlvProcessingContextWriter(targetProvider, memoryStreamProvider, logger);
var context = new FlvProcessingContext();
var session = new Dictionary