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

Made ExecuteAsync method public and added overload to stream output via pipe #79

Open
wants to merge 3 commits into
base: master
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
96 changes: 50 additions & 46 deletions src/FFmpeg.NET/Engine.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public Engine(string ffmpegPath = null)
public event EventHandler<ConversionCompleteEventArgs> Complete;
public event EventHandler<ConversionDataEventArgs> Data;


// ---------------------------------------------------------
// Wrapper methods for ease of use
// ---------------------------------------------------------

public async Task<MetaData> GetMetaDataAsync(IInputArgument mediaFile, CancellationToken cancellationToken)
{
var parameters = new FFmpegParameters
Expand Down Expand Up @@ -87,83 +92,82 @@ public async Task<Stream> ConvertAsync(IInputArgument input, ConversionOptions o

public async Task ConvertAsync(IInputArgument input, Stream output, ConversionOptions options, CancellationToken cancellationToken)
{
var pipeName = $"{_pipePrefix}{Guid.NewGuid()}";
var parameters = new FFmpegParameters
{
Task = FFmpegTask.Convert,
Input = input,
Output = new OutputPipe(GetPipePath(pipeName)),
ConversionOptions = options
};

var process = CreateProcess(parameters);
var pipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

await pipe.WaitForConnectionAsync(cancellationToken);
await Task.WhenAll(
pipe.CopyToAsync(output, cancellationToken),
process.ExecuteAsync(cancellationToken).ContinueWith(x =>
{
pipe.Disconnect();
pipe.Dispose();
}, cancellationToken)
).ConfigureAwait(false);
Cleanup(process);
await ExecuteAsync(parameters, output, cancellationToken).ConfigureAwait(false);
}

public async Task ConvertAsync(IArgument argument, Stream output, CancellationToken cancellationToken)
{
var outputPipeName = $"{_pipePrefix}{Guid.NewGuid()}";
var outputArgument = new OutputPipe(GetPipePath(outputPipeName));
var pipe = new NamedPipeServerStream(outputPipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

var arguments = argument.Argument + $" {outputArgument.Argument}";
var parameters = new FFmpegParameters { CustomArguments = arguments };
var process = CreateProcess(parameters);

await Task.WhenAll(
pipe.WaitForConnectionAsync(cancellationToken).ContinueWith(async x =>
{
await pipe.CopyToAsync(output, cancellationToken);
}),
process.ExecuteAsync(cancellationToken).ContinueWith(x =>
{
pipe.Disconnect();
pipe.Dispose();
}, cancellationToken)
).ConfigureAwait(false);

Cleanup(process);
}

private async Task ExecuteAsync(FFmpegParameters parameters, CancellationToken cancellationToken)
{
var ffmpegProcess = CreateProcess(parameters);
await ffmpegProcess.ExecuteAsync(cancellationToken).ConfigureAwait(false);
Cleanup(ffmpegProcess);
var parameters = new FFmpegParameters
{
Task = FFmpegTask.Execute,
CustomArguments = argument.Argument
};
await ExecuteAsync(parameters, output, cancellationToken).ConfigureAwait(false);
}

public async Task ExecuteAsync(string arguments, CancellationToken cancellationToken)
{
var parameters = new FFmpegParameters
{
Task = FFmpegTask.Execute,
CustomArguments = arguments,
};
await ExecuteAsync(parameters, cancellationToken).ConfigureAwait(false);
}

// if further overloads are needed
// it should be considered if ExecuteAsync(FFmpegParameters parameters, CancellationToken cancellationToken) should be made public
public async Task ExecuteAsync(string arguments, string workingDirectory, CancellationToken cancellationToken)
{
var parameters = new FFmpegParameters
{
Task = FFmpegTask.Execute,
CustomArguments = arguments,
WorkingDirectory = workingDirectory
};
await ExecuteAsync(parameters, cancellationToken).ConfigureAwait(false);
}

// ---------------------------------------------------------
// Basic API to call ffmpeg
// ---------------------------------------------------------

public async Task ExecuteAsync(FFmpegParameters parameters, CancellationToken cancellationToken)
{
var ffmpegProcess = CreateProcess(parameters);
await ffmpegProcess.ExecuteAsync(cancellationToken).ConfigureAwait(false);
Cleanup(ffmpegProcess);
}

public async Task ExecuteAsync(FFmpegParameters parameters, Stream output, CancellationToken cancellationToken)
{
var outputPipeName = $"{_pipePrefix}{Guid.NewGuid()}";
var outputArgument = new OutputPipe(GetPipePath(outputPipeName));
var pipe = new NamedPipeServerStream(outputPipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

parameters.Output = outputArgument;
var ffmpegProcess = CreateProcess(parameters);
try
{
var executeProcess = ffmpegProcess.ExecuteAsync(cancellationToken);
var copyData = pipe.WaitForConnectionAsync(cancellationToken)
.ContinueWith(async x =>
{
await pipe.CopyToAsync(output, cancellationToken);
}, cancellationToken).Unwrap();
await Task.WhenAll(executeProcess, copyData).ConfigureAwait(false);
pipe.Disconnect();
}
finally
{
pipe.Dispose();
Cleanup(ffmpegProcess);
}
}

private FFmpegProcess CreateProcess(FFmpegParameters parameters)
{
Expand Down
26 changes: 23 additions & 3 deletions src/FFmpeg.NET/FFmpegArgumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ internal static class FFmpegArgumentBuilder
{
public static string Build(FFmpegParameters parameters)
{
if (parameters.HasCustomArguments)
return parameters.CustomArguments;

return parameters.Task switch
{
FFmpegTask.Convert => Convert(parameters.Input, parameters.Output, parameters.ConversionOptions),
FFmpegTask.GetMetaData => GetMetadata(parameters.Input),
FFmpegTask.GetThumbnail => GetThumbnail(parameters.Input, parameters.Output, parameters.ConversionOptions),
FFmpegTask.Execute => Execute(parameters.Input, parameters.Output, parameters.CustomArguments),
_ => throw new ArgumentOutOfRangeException(),
};
}
Expand Down Expand Up @@ -182,6 +180,28 @@ private static string Convert(IInputArgument input, IOutputArgument output, Conv
return commandBuilder.AppendFormat(" {0} ", output.Argument).ToString();
}

private static string Execute(IInputArgument input, IOutputArgument output, string customArguments)
{
StringBuilder commandBuilder = new StringBuilder();
commandBuilder.Append(customArguments);

if (input != null)
{
if (input.UseStandardInput)
{
commandBuilder.Append(" -nostdin ");
}
commandBuilder.Append($" -i {input.Argument} ");
}

if (output != null)
{
commandBuilder.Append($" {output.Argument} ");
}

return commandBuilder.ToString();
}

private static void AppendHWAccelOutputFormat(StringBuilder commandBuilder, ConversionOptions conversionOptions)
{
if (conversionOptions.HWAccel != HWAccel.None && conversionOptions.HWAccelOutputFormatCopy)
Expand Down
3 changes: 2 additions & 1 deletion src/FFmpeg.NET/FFmpegTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public enum FFmpegTask
{
Convert,
GetMetaData,
GetThumbnail
GetThumbnail,
Execute
}
}