Skip to content

Commit 5993f10

Browse files
authored
Merge pull request #548 from Tichau/integration
WIP: File Converter release v2.1
2 parents 0bc1f55 + 892c8cb commit 5993f10

38 files changed

+7659
-3077
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,4 @@ FakesAssemblies/
196196

197197
# Ignore the installer signing commands file
198198
Installer/Installer.sign
199+
Certificate/*

Application/FileConverter/App.config

Lines changed: 0 additions & 27 deletions
This file was deleted.

Application/FileConverter/Application.xaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// <copyright file="Application.xaml.cs" company="AAllard">License: http://www.gnu.org/licenses/gpl.html GPL version 3.</copyright>
22

33
/* File Converter - This program allow you to convert file format to another.
4-
Copyright (C) 2024 Adrien Allard
4+
Copyright (C) 2025 Adrien Allard
55
email: adrien.allard.pro@gmail.com
66
77
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
@@ -38,8 +38,8 @@ public partial class Application : System.Windows.Application
3838
private static readonly Version Version = new Version()
3939
{
4040
Major = 2,
41-
Minor = 0,
42-
Patch = 2,
41+
Minor = 1,
42+
Patch = 0,
4343
};
4444

4545
private bool needToRunConversionThread;

Application/FileConverter/ConversionJobs/ConversionJobFactory.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ public static ConversionJob Create(ConversionPreset conversionPreset, string inp
4343
return new ConversionJob_ImageMagick(conversionPreset, inputFilePath);
4444
}
4545

46-
if (Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Image ||
47-
Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Document)
46+
if (conversionPreset.OutputType == OutputType.Jpg ||
47+
conversionPreset.OutputType == OutputType.Png ||
48+
conversionPreset.OutputType == OutputType.Webp)
4849
{
4950
return new ConversionJob_ImageMagick(conversionPreset, inputFilePath);
5051
}

Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.Converters.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,23 @@ private static string ComputeAudioChannelArgs(ConversionPreset conversionPreset)
3737
return channelArgs;
3838
}
3939

40-
private static string ComputeTransformArgs(ConversionPreset conversionPreset)
40+
private static string ComputeTransformArgs(ConversionPreset conversionPreset, Helpers.HardwareAccelerationMode hwAccel = Helpers.HardwareAccelerationMode.Off)
4141
{
4242
float scaleFactor = conversionPreset.GetSettingsValue<float>(ConversionPreset.ConversionSettingKeys.VideoScale);
4343
string scaleArgs = string.Empty;
4444

4545
if (conversionPreset.OutputType == OutputType.Mkv || conversionPreset.OutputType == OutputType.Mp4)
4646
{
4747
// This presets use h264 codec, the size of the video need to be divisible by 2.
48-
scaleArgs = string.Format("scale=trunc(iw*{0}/2)*2:trunc(ih*{0}/2)*2", scaleFactor.ToString("#.##", CultureInfo.InvariantCulture));
48+
switch (hwAccel)
49+
{
50+
case Helpers.HardwareAccelerationMode.CUDA:
51+
scaleArgs = string.Format("scale_cuda=trunc(iw*{0}/2)*2:trunc(ih*{0}/2)*2:format=yuv420p", scaleFactor.ToString("#.##", CultureInfo.InvariantCulture));
52+
break;
53+
default:
54+
scaleArgs = string.Format("scale=trunc(iw*{0}/2)*2:trunc(ih*{0}/2)*2", scaleFactor.ToString("#.##", CultureInfo.InvariantCulture));
55+
break;
56+
}
4957
}
5058
else if (Math.Abs(scaleFactor - 1f) >= 0.005f)
5159
{
@@ -96,7 +104,7 @@ private static string ComputeTransformArgs(ConversionPreset conversionPreset)
96104
transformArgs += rotationArgs;
97105
}
98106

99-
if (conversionPreset.OutputType == OutputType.Mkv || conversionPreset.OutputType == OutputType.Mp4)
107+
if (hwAccel == Helpers.HardwareAccelerationMode.Off && (conversionPreset.OutputType == OutputType.Mkv || conversionPreset.OutputType == OutputType.Mp4))
100108
{
101109
// http://trac.ffmpeg.org/wiki/Encode/H.264#Encodingfordumbplayers
102110
// YUV planar color space with 4:2:0 chroma subsampling

Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ namespace FileConverter.ConversionJobs
77
using System.Diagnostics;
88
using System.Globalization;
99
using System.IO;
10-
using System.Text.RegularExpressions;
11-
12-
using FileConverter.Controls;
13-
10+
using System.Text.RegularExpressions;
11+
using CommunityToolkit.Mvvm.DependencyInjection;
12+
using FileConverter.Controls;
13+
using FileConverter.Services;
14+
1415
public partial class ConversionJob_FFMPEG : ConversionJob
1516
{
1617
private readonly Regex durationRegex = new Regex(@"Duration:\s*([0-9][0-9]):([0-9][0-9]):([0-9][0-9])\.([0-9][0-9]),.*bitrate:\s*([0-9]+) kb\/s");
@@ -23,6 +24,8 @@ public partial class ConversionJob_FFMPEG : ConversionJob
2324

2425
private readonly List<FFMpegPass> ffmpegArgumentStringByPass = new List<FFMpegPass>();
2526

27+
ISettingsService settingsService = Ioc.Default.GetRequiredService<ISettingsService>();
28+
2629
public ConversionJob_FFMPEG() : base()
2730
{
2831
}
@@ -255,7 +258,9 @@ protected virtual void FillFFMpegArgumentsList()
255258
VideoEncodingSpeed videoEncodingSpeed = this.ConversionPreset.GetSettingsValue<VideoEncodingSpeed>(ConversionPreset.ConversionSettingKeys.VideoEncodingSpeed);
256259
int audioEncodingBitrate = this.ConversionPreset.GetSettingsValue<int>(ConversionPreset.ConversionSettingKeys.AudioBitrate);
257260

258-
string transformArgs = ConversionJob_FFMPEG.ComputeTransformArgs(this.ConversionPreset);
261+
Helpers.HardwareAccelerationMode hwAccel = settingsService.Settings.HardwareAccelerationMode;
262+
263+
string transformArgs = ConversionJob_FFMPEG.ComputeTransformArgs(this.ConversionPreset, hwAccel);
259264
string videoFilteringArgs = ConversionJob_FFMPEG.Encapsulate("-vf", transformArgs);
260265

261266
string audioArgs = "-an";
@@ -264,14 +269,23 @@ protected virtual void FillFFMpegArgumentsList()
264269
audioArgs = $"-c:a aac -qscale:a {this.AACBitrateToQualityIndex(audioEncodingBitrate)}";
265270
}
266271

272+
string videoCodec = "libx264";
273+
string hwAccelArg = "";
274+
if (hwAccel == Helpers.HardwareAccelerationMode.CUDA)
275+
{
276+
videoCodec = "h264_nvenc";
277+
hwAccelArg = "-hwaccel cuda -hwaccel_output_format cuda";
278+
}
279+
267280
string encoderArgs = string.Format(
268-
"-c:v libx264 -preset {0} -crf {1} {2} {3}",
269-
this.H264EncodingSpeedToPreset(videoEncodingSpeed),
281+
"-c:v {0} -preset {1} -crf {2} {3} {4}",
282+
videoCodec,
283+
this.H264EncodingSpeedToPreset(videoEncodingSpeed),
270284
this.H264QualityToCRF(videoEncodingQuality),
271-
audioArgs,
285+
audioArgs,
272286
videoFilteringArgs);
273287

274-
string arguments = string.Format("-n -stats -i \"{0}\" {2} \"{1}\"", this.InputFilePath, this.OutputFilePath, encoderArgs);
288+
string arguments = string.Format("-n -stats {3} -i \"{0}\" {2} \"{1}\"", this.InputFilePath, this.OutputFilePath, encoderArgs, hwAccelArg);
275289

276290
this.ffmpegArgumentStringByPass.Add(new FFMpegPass(arguments));
277291
}

Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ protected override void Convert()
8686
readSettings.Format = MagickFormat.Dng;
8787
break;
8888

89+
case ".gif":
90+
// Get the first frame of the gif for conversion.
91+
// Maybe in the future make this user selectable.
92+
readSettings.FrameIndex = 0;
93+
break;
94+
8995
default:
9096
break;
9197
}
@@ -175,8 +181,8 @@ private void ConvertImage(MagickImage image, bool ignoreScale = false)
175181
bool clampSizeToPowerOf2 = this.ConversionPreset.GetSettingsValue<bool>(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2);
176182
if (clampSizeToPowerOf2)
177183
{
178-
int referenceSize = System.Math.Min(image.Width, image.Height);
179-
int size = 2;
184+
uint referenceSize = System.Math.Min(image.Width, image.Height);
185+
uint size = 2;
180186
while (size * 2 <= referenceSize)
181187
{
182188
size *= 2;
@@ -190,11 +196,11 @@ private void ConvertImage(MagickImage image, bool ignoreScale = false)
190196

191197
if (this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageMaximumSize))
192198
{
193-
int maximumSize = this.ConversionPreset.GetSettingsValue<int>(ConversionPreset.ConversionSettingKeys.ImageMaximumSize);
199+
uint maximumSize = this.ConversionPreset.GetSettingsValue<uint>(ConversionPreset.ConversionSettingKeys.ImageMaximumSize);
194200
if (maximumSize > 0)
195201
{
196-
int width = System.Math.Min(image.Width, maximumSize);
197-
int height = System.Math.Min(image.Height, maximumSize);
202+
uint width = System.Math.Min(image.Width, maximumSize);
203+
uint height = System.Math.Min(image.Height, maximumSize);
198204

199205
Debug.Log($"Clamp size to maximum size of {width}x{width} (from {image.Width}x{image.Height} to {width}x{height}).");
200206

@@ -211,7 +217,7 @@ private void ConvertImage(MagickImage image, bool ignoreScale = false)
211217
break;
212218

213219
case OutputType.Jpg:
214-
image.Quality = this.ConversionPreset.GetSettingsValue<int>(ConversionPreset.ConversionSettingKeys.ImageQuality);
220+
image.Quality = this.ConversionPreset.GetSettingsValue<uint>(ConversionPreset.ConversionSettingKeys.ImageQuality);
215221
break;
216222

217223
case OutputType.Pdf:
@@ -220,7 +226,7 @@ private void ConvertImage(MagickImage image, bool ignoreScale = false)
220226
break;
221227

222228
case OutputType.Webp:
223-
image.Quality = this.ConversionPreset.GetSettingsValue<int>(ConversionPreset.ConversionSettingKeys.ImageQuality);
229+
image.Quality = this.ConversionPreset.GetSettingsValue<uint>(ConversionPreset.ConversionSettingKeys.ImageQuality);
224230
break;
225231

226232
default:

0 commit comments

Comments
 (0)