-
Notifications
You must be signed in to change notification settings - Fork 12
/
HandBrakeTool.cs
159 lines (129 loc) · 5.08 KB
/
HandBrakeTool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using InsaneGenius.Utilities;
using Serilog;
// https://handbrake.fr/docs/en/latest/cli/command-line-reference.html
namespace PlexCleaner;
public partial class HandBrakeTool : MediaTool
{
public override ToolFamily GetToolFamily()
{
return ToolFamily.HandBrake;
}
public override ToolType GetToolType()
{
return ToolType.HandBrake;
}
protected override string GetToolNameWindows()
{
return "HandBrakeCLI.exe";
}
protected override string GetToolNameLinux()
{
return "HandBrakeCLI";
}
public override bool GetInstalledVersion(out MediaToolInfo mediaToolInfo)
{
// Initialize
mediaToolInfo = new MediaToolInfo(this);
// Get version
const string commandline = "--version";
var exitCode = Command(commandline, out var output);
if (exitCode != 0)
{
return false;
}
// First line as version
// E.g. Windows : "HandBrake 1.3.3"
// E.g. Linux : "HandBrake 1.3.3"
var lines = output.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
// Extract the short version number
var match = InstalledVersionRegex().Match(lines[0]);
Debug.Assert(match.Success);
mediaToolInfo.Version = match.Groups["version"].Value;
// Get tool filename
mediaToolInfo.FileName = GetToolPath();
// Get other attributes if we can read the file
if (File.Exists(mediaToolInfo.FileName))
{
FileInfo fileInfo = new(mediaToolInfo.FileName);
mediaToolInfo.ModifiedTime = fileInfo.LastWriteTimeUtc;
mediaToolInfo.Size = fileInfo.Length;
}
return true;
}
protected override bool GetLatestVersionWindows(out MediaToolInfo mediaToolInfo)
{
// Initialize
mediaToolInfo = new MediaToolInfo(this);
try
{
// Get the latest release version number from github releases
// https://github.com/HandBrake/HandBrake
const string repo = "HandBrake/HandBrake";
mediaToolInfo.Version = GetLatestGitHubRelease(repo);
// Create the filename using the version number
// HandBrakeCLI-1.3.2-win-x86_64.zip
mediaToolInfo.FileName = $"HandBrakeCLI-{mediaToolInfo.Version}-win-x86_64.zip";
// Get the GitHub download Uri
mediaToolInfo.Url = GitHubRelease.GetDownloadUri(repo, mediaToolInfo.Version, mediaToolInfo.FileName);
}
catch (Exception e) when (Log.Logger.LogAndHandle(e, MethodBase.GetCurrentMethod()?.Name))
{
return false;
}
return true;
}
protected override bool GetLatestVersionLinux(out MediaToolInfo mediaToolInfo)
{
// Initialize
mediaToolInfo = new MediaToolInfo(this);
// TODO: Linux implementation
return false;
}
public bool ConvertToMkv(string inputName, string outputName, bool includeSubtitles, bool deInterlace)
{
// Delete output file
FileEx.DeleteFile(outputName);
// TODO: How to suppress console output when running in parallel mode?
// if (Program.Options.Parallel)
// Build commandline
StringBuilder commandline = new();
CreateDefaultArgs(inputName, commandline);
commandline.Append($"--output \"{outputName}\" ");
commandline.Append("--format av_mkv ");
// Video encoder options
// E.g. --encoder x264 --quality 20 --encoder-preset medium
commandline.Append($"--encoder {Program.Config.ConvertOptions.HandBrakeOptions.Video} ");
// Deinterlace using decomb filter
if (deInterlace)
{
commandline.Append("--comb-detect --decomb ");
}
// All audio with encoder
// E.g. --all-audio --aencoder copy --audio-fallback ac3
commandline.Append($"--all-audio --aencoder {Program.Config.ConvertOptions.HandBrakeOptions.Audio} ");
// All or no subtitles
commandline.Append(includeSubtitles ? "--all-subtitles " : "--subtitle none ");
// Execute
var exitCode = Command(commandline.ToString());
return exitCode == 0;
}
private static void CreateDefaultArgs(string inputName, StringBuilder commandline)
{
commandline.Append($"--input \"{inputName}\" ");
if (Program.Options.TestSnippets)
{
commandline.Append($"--start-at seconds:00 --stop-at seconds:{(int)Program.SnippetTimeSpan.TotalSeconds} ");
}
}
public const string DefaultVideoOptions = "x264 --quality 22 --encoder-preset medium";
public const string DefaultAudioOptions = "copy --audio-fallback ac3";
private const string VersionPattern = @"HandBrake\ (?<version>.*)";
[GeneratedRegex(VersionPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)]
public static partial Regex InstalledVersionRegex();
}