Crunchy-Downloader/CRD/Utils/Muxing/Merger.cs
Elwador d9813191ad - Added **Global Pause button** for the download queue [#418](https://github.com/Crunchy-DL/Crunchy-Downloader/issues/418)
- Added **fallback for sync failures** [#407](https://github.com/Crunchy-DL/Crunchy-Downloader/issues/407)
- Added **history setting** to remove non-existent series/episodes on refresh [#420](https://github.com/Crunchy-DL/Crunchy-Downloader/issues/420)
- Added **movies to history**
- Added **queue persistence**
- Changed **download item state handling**
- Changed **download item removal processing**
- Made small changes to **font detection**
- Changed **rate limit error handling**
- Fixed issue where **files were not always cleaned up** for removed downloads
- Fixed **crash when the queue list was modified**
- Fixed **changelog parsing** not handling versions like `vX.X.X.X`, which caused changes to be re-added on every restart
2026-04-20 15:40:58 +02:00

79 lines
2.6 KiB
C#

using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using CRD.Utils.Muxing.Commands;
using CRD.Utils.Muxing.Structs;
namespace CRD.Utils.Muxing;
public class Merger{
public MergerOptions Options;
public Merger(MergerOptions options){
Options = options;
if (Options.VideoTitle is{ Length: > 0 }){
Options.VideoTitle = Options.VideoTitle.Replace("\"", "'");
}
}
public string FFmpeg(){
return new FFmpegCommandBuilder(Options).Build();
}
public string MkvMerge(){
return new MkvMergeCommandBuilder(Options).Build();
}
public async Task<bool> Merge(string type, string bin, CancellationToken cancellationToken = default){
string command = type switch{
"ffmpeg" => FFmpeg(),
"mkvmerge" => MkvMerge(),
_ => ""
};
if (string.IsNullOrEmpty(command)){
Console.Error.WriteLine("Unable to merge files.");
return false;
}
Console.WriteLine($"[{type}] Started merging");
var result = await Helpers.ExecuteCommandAsync(bin, command, cancellationToken);
if (!result.IsOk && type == "mkvmerge" && result.ErrorCode == 1){
Console.Error.WriteLine($"[{type}] Mkvmerge finished with at least one warning");
} else if (!result.IsOk){
Console.Error.WriteLine($"[{type}] Merging failed with exit code {result.ErrorCode}");
Console.Error.WriteLine($"[{type}] Merging failed command: {command}");
return false;
} else{
Console.WriteLine($"[{type} Done]");
}
return true;
}
public void CleanUp(){
// Combine all media file lists and iterate through them
var allMediaFiles = Options.OnlyAudio.Concat(Options.OnlyVid).Concat(Options.VideoAndAudio)
.ToList();
allMediaFiles.ForEach(file => Helpers.DeleteFile(file.Path));
allMediaFiles.ForEach(file => Helpers.DeleteFile(file.Path + ".resume"));
allMediaFiles.ForEach(file => Helpers.DeleteFile(file.Path + ".new.resume"));
Options.Description?.ForEach(description => Helpers.DeleteFile(description.Path));
Options.Cover.ForEach(cover => Helpers.DeleteFile(cover.Path));
// Delete chapter files if any
Options.Chapters?.ForEach(chapter => Helpers.DeleteFile(chapter.Path));
if (!Options.SkipSubMux){
// Delete subtitle files
Options.Subtitles.ForEach(subtitle => Helpers.DeleteFile(subtitle.File));
}
}
}