Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 48 additions & 0 deletions YoutubeExplode.Converter.Tests/EnvironmentSpecs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.IO;
using System.Threading.Tasks;
using FluentAssertions;
using Xunit;
using Xunit.Abstractions;
using YoutubeExplode.Converter.Tests.Utils;

namespace YoutubeExplode.Converter.Tests;

public class EnvironmentSpecs(ITestOutputHelper testOutput) : IAsyncLifetime
{
public async Task InitializeAsync() => await FFmpeg.InitializeAsync();

public Task DisposeAsync() => Task.CompletedTask;

[Fact]
public async Task I_can_download_a_video_with_custom_environment_variables_passed_to_FFmpeg()
{
// Arrange
var youtube = new YoutubeClient();

using var dir = TempDir.Create();
var filePath = Path.Combine(dir.Path, "video.mp4");

var logFilePath = Path.Combine(dir.Path, "ffreport.log");

// FFREPORT file path must be relative to the current working directory
var logFilePathFormatted = Path.GetRelativePath(
Directory.GetCurrentDirectory(),
logFilePath
)
.Replace('\\', '/');

// Act
await youtube.Videos.DownloadAsync(
"9bZkp7q19f0",
filePath,
o =>
o.SetFFmpegPath(FFmpeg.FilePath)
.SetEnvironmentVariable("FFREPORT", $"file={logFilePathFormatted}:level=32")
);

// Assert
File.Exists(logFilePath).Should().BeTrue();

testOutput.WriteLine(await File.ReadAllTextAsync(logFilePath));
}
}
2 changes: 1 addition & 1 deletion YoutubeExplode.Converter.Tests/GeneralSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ await youtube.Videos.DownloadAsync(
}

[Fact]
public async Task I_can_download_a_video_using_custom_conversion_settings()
public async Task I_can_download_a_video_with_custom_conversion_settings()
{
// Arrange
var youtube = new YoutubeClient();
Expand Down
2 changes: 1 addition & 1 deletion YoutubeExplode.Converter/ConversionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static async ValueTask DownloadAsync(
CancellationToken cancellationToken = default
)
{
var ffmpeg = new FFmpeg(request.FFmpegCliFilePath);
var ffmpeg = new FFmpeg(request.FFmpegCliFilePath, request.EnvironmentVariables);
var converter = new Converter(videoClient, ffmpeg, request.Preset);

await converter.ProcessAsync(
Expand Down
18 changes: 16 additions & 2 deletions YoutubeExplode.Converter/ConversionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using YoutubeExplode.Videos.Streams;

Expand All @@ -11,7 +12,8 @@ public class ConversionRequest(
string ffmpegCliFilePath,
string outputFilePath,
Container container,
ConversionPreset preset
ConversionPreset preset,
IReadOnlyDictionary<string, string?> environmentVariables
)
{
/// <summary>
Expand All @@ -24,7 +26,13 @@ public ConversionRequest(
ConversionFormat format,
ConversionPreset preset
)
: this(ffmpegCliFilePath, outputFilePath, new Container(format.Name), preset) { }
: this(
ffmpegCliFilePath,
outputFilePath,
new Container(format.Name),
preset,
new Dictionary<string, string?>()
) { }

/// <summary>
/// Path to the FFmpeg CLI.
Expand All @@ -51,4 +59,10 @@ ConversionPreset preset
/// Encoder preset.
/// </summary>
public ConversionPreset Preset { get; } = preset;

/// <summary>
/// Environment variables to set for the FFmpeg process.
/// </summary>
public IReadOnlyDictionary<string, string?> EnvironmentVariables { get; } =
environmentVariables;
}
17 changes: 16 additions & 1 deletion YoutubeExplode.Converter/ConversionRequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using YoutubeExplode.Converter.Utils.Extensions;
Expand All @@ -11,6 +12,10 @@ namespace YoutubeExplode.Converter;
/// </summary>
public class ConversionRequestBuilder(string outputFilePath)
{
private readonly Dictionary<string, string?> _environmentVariables = new(
StringComparer.Ordinal
);

private string? _ffmpegCliFilePath;
private Container? _container;
private ConversionPreset _preset;
Expand Down Expand Up @@ -64,6 +69,15 @@ public ConversionRequestBuilder SetPreset(ConversionPreset preset)
return this;
}

/// <summary>
/// Sets an environment variable for the FFmpeg process.
/// </summary>
public ConversionRequestBuilder SetEnvironmentVariable(string name, string? value)
{
_environmentVariables[name] = value;
return this;
}

/// <summary>
/// Builds the resulting request.
/// </summary>
Expand All @@ -72,6 +86,7 @@ public ConversionRequest Build() =>
_ffmpegCliFilePath ?? FFmpeg.GetFilePath(),
outputFilePath,
_container ?? GetDefaultContainer(),
_preset
_preset,
_environmentVariables
);
}
9 changes: 7 additions & 2 deletions YoutubeExplode.Converter/FFmpeg.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
Expand All @@ -13,9 +14,12 @@
namespace YoutubeExplode.Converter;

// Ideally this should use named pipes and stream through stdout.
// However, named pipes aren't well supported on non-Windows OS and
// However, named pipes aren't well-supported on non-Windows OS and
// stdout streaming only works with some specific formats.
internal partial class FFmpeg(string filePath)
internal partial class FFmpeg(
string filePath,
IReadOnlyDictionary<string, string?> environmentVariables
)
{
public async ValueTask ExecuteAsync(
string arguments,
Expand All @@ -36,6 +40,7 @@ public async ValueTask ExecuteAsync(
{
await Cli.Wrap(filePath)
.WithArguments(arguments)
.WithEnvironmentVariables(environmentVariables)
.WithStandardErrorPipe(stdErrPipe)
.ExecuteAsync(cancellationToken);
}
Expand Down
1 change: 0 additions & 1 deletion YoutubeExplode.Tests/StreamSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Buffers;
using System.IO;
using System.Linq;
Expand Down