Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed May 24, 2023
1 parent e41184b commit b4518d1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ dotnet_diagnostic.CA1056.severity = none # Asp.Net Core Pages uses strings na
# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = none # Don't need translated exceptions

# CA1308: Normalize strings to uppercase
dotnet_diagnostic.CA1308.severity = none # Also to lower is required

# CA1707: Identifiers should not contain underscores
dotnet_diagnostic.CA1707.severity = none # I like underscores into constants name

Expand Down
83 changes: 65 additions & 18 deletions src/EthernaVideoImporter.Core/Services/EncoderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,40 @@ public async Task<IEnumerable<VideoLocalFile>> EncodeVideosAsync(

Console.WriteLine($"Encoding resolution {heightResolution}...");

// Get scaled height
#pragma warning disable IDE0028 // Simplify collection initialization
// Build args.
var args = new List<string>();

//input
args.Add("-i"); args.Add(sourceVideoFile.FilePath);

//audio codec
args.Add("-c:a"); args.Add("aac");

//video codec
args.Add("-c:v"); args.Add(options.FFMpegHWAccelerationType switch
{
FFMpegHWAccelerationType.None => "libx264",
FFMpegHWAccelerationType.Cuda => "h264_nvenc",
_ => throw new InvalidOperationException()
});

//preset
args.Add("-preset"); args.Add("slow");

//hw acceleration
switch (options.FFMpegHWAccelerationType)
{
case FFMpegHWAccelerationType.Cuda:
args.Add("-hwaccel"); args.Add("cuda");
break;
default: break;
}

//flags
args.Add("-movflags"); args.Add("faststart");

//filters
var scaledWidth = (int)Math.Round(heightResolution * resolutionRatio, 0);
var roundedScaledWidth = (scaledWidth % 4) switch
{
Expand All @@ -54,26 +87,40 @@ public async Task<IEnumerable<VideoLocalFile>> EncodeVideosAsync(
3 => scaledWidth + 1,
_ => throw new InvalidOperationException()
};
args.Add("-vf");
{
var filters = new List<string>();

var fileName = $"{CommonConsts.TempDirectory.FullName}/{fileNameGuid}_{heightResolution}.mp4";
var args = new string[] {
"-i", sourceVideoFile.FilePath,
"-c:a", "aac",
"-c:v", ffMpegHWAccelerationType switch
//memory management
switch (options.FFMpegHWAccelerationType)
{
FFMpegHWAccelerationType.None => "libx264",
FFMpegHWAccelerationType.Cuda => "h264_nvenc"
},
ffMpegHWAccelerationType switch
case FFMpegHWAccelerationType.Cuda:
filters.Add("hwupload_cuda");
break;
default: break;
}

//scale
switch (options.FFMpegHWAccelerationType)
{
FFMpegHWAccelerationType.None => "",
FFMpegHWAccelerationType.Cuda => "-hwaccel cuda -hwaccel_output_format cuda"
},
"-movflags", "faststart",
"-vf", $"scale={roundedScaledWidth}:{heightResolution}",
"-loglevel", "info",
fileName
};
case FFMpegHWAccelerationType.Cuda:
filters.Add($"scale_npp={roundedScaledWidth}:{heightResolution}");
break;
default:
filters.Add($"scale={roundedScaledWidth}:{heightResolution}");
break;
}

args.Add($"\"{filters.Aggregate((r, f) => $"{r},{f}")}\"");
}

//logs
args.Add("-loglevel"); args.Add("info");

//output
var fileName = $"{CommonConsts.TempDirectory.FullName}/{fileNameGuid}_{heightResolution}.mp4";
args.Add(fileName);
#pragma warning restore IDE0028 // Simplify collection initialization

var command = Command.Run(FFMpegBinaryPath, args);

Expand Down
14 changes: 10 additions & 4 deletions src/EthernaVideoImporter.Devcon/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal static class Program
"\n" +
"Options:\n" +
$" -ff\tPath FFmpeg (default dir: {CommonConsts.DefaultFFmpegFolder})\n" +
$" -hw\tUse NVIDIA CUDA hardware acceleration on FFmpeg (default: false)\n" +
$" -hw\tUse hardware acceleration on FFmpeg (default: {nameof(FFMpegHWAccelerationType.None).ToLowerInvariant()}). Valid values: [{Enum.GetNames<FFMpegHWAccelerationType>().Aggregate((r, i) => $"{r}, {i}").ToLowerInvariant()}]\n" +
$" -t\tTTL (days) Postage Stamp (default value: {DefaultTTLPostageStamp} days)\n" +
" -o\tOffer video downloads to everyone\n" +
" -p\tPin videos\n" +
Expand Down Expand Up @@ -175,7 +175,7 @@ static async Task Main(string[] args)
case "-skip720": skip720 = true; break;
case "-skip480": skip480 = true; break;
case "-skip360": skip360 = true; break;
case "-hw": ffMpegHWAccelerationType = FFMpegHWAccelerationType.Cuda; break;
case "-hw": ffMpegHWAccelerationType = Enum.Parse<FFMpegHWAccelerationType>(args[++i], true); break;
default: throw new ArgumentException(args[i] + " is not a valid argument");
}
}
Expand Down Expand Up @@ -250,7 +250,7 @@ static async Task Main(string[] args)
{
if (customFFMpegFolderPath is not null)
encoderOptions.FFMpegFolderPath = customFFMpegFolderPath;

encoderOptions.FFMpegHWAccelerationType = ffMpegHWAccelerationType;
encoderOptions.IncludeAudioTrack = includeAudioTrack;
encoderOptions.Skip1440 = skip1440;
encoderOptions.Skip1080 = skip1080;
Expand All @@ -266,7 +266,13 @@ static async Task Main(string[] args)
},
useBeeNativeNode,
authResult.RefreshTokenHandler);
services.AddTransient<IYoutubeClient, YoutubeClient>();
services.AddTransient<IYoutubeClient>(_ =>
ffMpegHWAccelerationType switch
{
FFMpegHWAccelerationType.None => new YoutubeClient(),
FFMpegHWAccelerationType.Cuda => new YoutubeClient("cuda"),
_ => throw new InvalidOperationException()
});
services.AddTransient<IYoutubeDownloader, YoutubeDownloader>();
services.AddTransient<IVideoProvider, MdVideoProvider>();

Expand Down
27 changes: 20 additions & 7 deletions src/EthernaVideoImporter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal static class Program
"\n" +
"Options:\n" +
$" -ff\tPath FFmpeg (default dir: {CommonConsts.DefaultFFmpegFolder})\n" +
$" -hw\tUse NVIDIA CUDA hardware acceleration on FFmpeg (default: false)\n" +
$" -hw\tUse hardware acceleration on FFmpeg (default: {nameof(FFMpegHWAccelerationType.None).ToLowerInvariant()}). Valid values: [{Enum.GetNames<FFMpegHWAccelerationType>().Aggregate((r, i) => $"{r}, {i}").ToLowerInvariant()}]\n" +
$" -t\tTTL (days) Postage Stamp (default value: {DefaultTTLPostageStamp} days)\n" +
" -o\tOffer video downloads to everyone\n" +
" -p\tPin videos\n" +
Expand Down Expand Up @@ -198,7 +198,7 @@ static async Task Main(string[] args)
case "-skip720": skip720 = true; break;
case "-skip480": skip480 = true; break;
case "-skip360": skip360 = true; break;
case "-hw": ffMpegHWAccelerationType = FFMpegHWAccelerationType.Cuda; break;
case "-hw": ffMpegHWAccelerationType = Enum.Parse<FFMpegHWAccelerationType>(args[++i], true); break;
default: throw new ArgumentException(args[i] + " is not a valid argument");
}
}
Expand Down Expand Up @@ -272,7 +272,7 @@ static async Task Main(string[] args)
{
if (customFFMpegFolderPath is not null)
encoderOptions.FFMpegFolderPath = customFFMpegFolderPath;

encoderOptions.FFMpegHWAccelerationType = ffMpegHWAccelerationType;
encoderOptions.IncludeAudioTrack = includeAudioTrack;
encoderOptions.Skip1440 = skip1440;
encoderOptions.Skip1080 = skip1080;
Expand Down Expand Up @@ -313,8 +313,7 @@ static async Task Main(string[] args)
services.AddSingleton<IValidateOptions<YouTubeChannelVideoProviderOptions>, YouTubeChannelVideoProviderOptionsValidation>();

//services
services.AddTransient<IYoutubeClient, YoutubeClient>();
services.AddTransient<IYoutubeDownloader, YoutubeDownloader>();
AddYoutubeDownloader(services, ffMpegHWAccelerationType);
services.AddTransient<IVideoProvider, YouTubeChannelVideoProvider>();
break;
case SourceType.YouTubeVideo:
Expand All @@ -326,8 +325,7 @@ static async Task Main(string[] args)
services.AddSingleton<IValidateOptions<YouTubeSingleVideoProviderOptions>, YouTubeSingleVideoProviderOptionsValidation>();

//services
services.AddTransient<IYoutubeClient, YoutubeClient>();
services.AddTransient<IYoutubeDownloader, YoutubeDownloader>();
AddYoutubeDownloader(services, ffMpegHWAccelerationType);
services.AddTransient<IVideoProvider, YouTubeSingleVideoProvider>();
break;
default:
Expand All @@ -347,5 +345,20 @@ await importer.RunAsync(
userEthAddr,
unpinRemovedVideos);
}

// Helpers.
private static void AddYoutubeDownloader(
ServiceCollection services,
FFMpegHWAccelerationType ffMpegHWAccelerationType)
{
services.AddTransient<IYoutubeClient>(_ =>
ffMpegHWAccelerationType switch
{
FFMpegHWAccelerationType.None => new YoutubeClient(),
FFMpegHWAccelerationType.Cuda => new YoutubeClient("cuda"),
_ => throw new InvalidOperationException()
});
services.AddTransient<IYoutubeDownloader, YoutubeDownloader>();
}
}
}

0 comments on commit b4518d1

Please sign in to comment.