Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement hardware acceleration #412

Open
wants to merge 5 commits into
base: integration
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,23 @@ private static string ComputeAudioChannelArgs(ConversionPreset conversionPreset)
return channelArgs;
}

private static string ComputeTransformArgs(ConversionPreset conversionPreset)
private static string ComputeTransformArgs(ConversionPreset conversionPreset, Helpers.HardwareAccelerationMode hwAccel = Helpers.HardwareAccelerationMode.Off)
{
float scaleFactor = conversionPreset.GetSettingsValue<float>(ConversionPreset.ConversionSettingKeys.VideoScale);
string scaleArgs = string.Empty;

if (conversionPreset.OutputType == OutputType.Mkv || conversionPreset.OutputType == OutputType.Mp4)
{
// This presets use h264 codec, the size of the video need to be divisible by 2.
scaleArgs = string.Format("scale=trunc(iw*{0}/2)*2:trunc(ih*{0}/2)*2", scaleFactor.ToString("#.##", CultureInfo.InvariantCulture));
switch (hwAccel)
{
case Helpers.HardwareAccelerationMode.CUDA:
scaleArgs = string.Format("scale_cuda=trunc(iw*{0}/2)*2:trunc(ih*{0}/2)*2:format=yuv420p", scaleFactor.ToString("#.##", CultureInfo.InvariantCulture));
break;
default:
scaleArgs = string.Format("scale=trunc(iw*{0}/2)*2:trunc(ih*{0}/2)*2", scaleFactor.ToString("#.##", CultureInfo.InvariantCulture));
break;
}
}
else if (Math.Abs(scaleFactor - 1f) >= 0.005f)
{
Expand Down Expand Up @@ -96,7 +104,7 @@ private static string ComputeTransformArgs(ConversionPreset conversionPreset)
transformArgs += rotationArgs;
}

if (conversionPreset.OutputType == OutputType.Mkv || conversionPreset.OutputType == OutputType.Mp4)
if (hwAccel == Helpers.HardwareAccelerationMode.Off && (conversionPreset.OutputType == OutputType.Mkv || conversionPreset.OutputType == OutputType.Mp4))
{
// http://trac.ffmpeg.org/wiki/Encode/H.264#Encodingfordumbplayers
// YUV planar color space with 4:2:0 chroma subsampling
Expand Down
32 changes: 23 additions & 9 deletions Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ namespace FileConverter.ConversionJobs
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;

using FileConverter.Controls;

using System.Text.RegularExpressions;
using CommunityToolkit.Mvvm.DependencyInjection;
using FileConverter.Controls;
using FileConverter.Services;

public partial class ConversionJob_FFMPEG : ConversionJob
{
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");
Expand All @@ -23,6 +24,8 @@ public partial class ConversionJob_FFMPEG : ConversionJob

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

ISettingsService settingsService = Ioc.Default.GetRequiredService<ISettingsService>();

public ConversionJob_FFMPEG() : base()
{
}
Expand Down Expand Up @@ -255,7 +258,9 @@ protected virtual void FillFFMpegArgumentsList()
VideoEncodingSpeed videoEncodingSpeed = this.ConversionPreset.GetSettingsValue<VideoEncodingSpeed>(ConversionPreset.ConversionSettingKeys.VideoEncodingSpeed);
int audioEncodingBitrate = this.ConversionPreset.GetSettingsValue<int>(ConversionPreset.ConversionSettingKeys.AudioBitrate);

string transformArgs = ConversionJob_FFMPEG.ComputeTransformArgs(this.ConversionPreset);
Helpers.HardwareAccelerationMode hwAccel = settingsService.Settings.HardwareAccelerationMode;

string transformArgs = ConversionJob_FFMPEG.ComputeTransformArgs(this.ConversionPreset, hwAccel);
string videoFilteringArgs = ConversionJob_FFMPEG.Encapsulate("-vf", transformArgs);

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

string videoCodec = "libx264";
string hwAccelArg = "";
if (hwAccel == Helpers.HardwareAccelerationMode.CUDA)
{
videoCodec = "h264_nvenc";
hwAccelArg = "-hwaccel cuda -hwaccel_output_format cuda";
}

string encoderArgs = string.Format(
"-c:v libx264 -preset {0} -crf {1} {2} {3}",
this.H264EncodingSpeedToPreset(videoEncodingSpeed),
"-c:v {0} -preset {1} -crf {2} {3} {4}",
videoCodec,
this.H264EncodingSpeedToPreset(videoEncodingSpeed),
this.H264QualityToCRF(videoEncodingQuality),
audioArgs,
audioArgs,
videoFilteringArgs);

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

this.ffmpegArgumentStringByPass.Add(new FFMpegPass(arguments));
}
Expand Down
Loading