Skip to content

Commit f2e52f7

Browse files
committed
add working directory as a parameter to the underlying process as of #74
1 parent 657395c commit f2e52f7

6 files changed

Lines changed: 285 additions & 292 deletions

File tree

src/FFmpeg.NET/Engine.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,22 @@ public async Task ExecuteAsync(string arguments, CancellationToken cancellationT
148148
var parameters = new FFmpegParameters
149149
{
150150
CustomArguments = arguments,
151-
152151
};
153152
await ExecuteAsync(parameters, cancellationToken).ConfigureAwait(false);
154153
}
154+
155+
// if further overloads are needed
156+
// it should be considered if ExecuteAsync(FFmpegParameters parameters, CancellationToken cancellationToken) should be made public
157+
public async Task ExecuteAsync(string arguments, string workingDirectory, CancellationToken cancellationToken)
158+
{
159+
var parameters = new FFmpegParameters
160+
{
161+
CustomArguments = arguments,
162+
WorkingDirectory = workingDirectory
163+
};
164+
await ExecuteAsync(parameters, cancellationToken).ConfigureAwait(false);
165+
}
166+
155167

156168
private FFmpegProcess CreateProcess(FFmpegParameters parameters)
157169
{

src/FFmpeg.NET/Extensions/ProcessExtensions.cs

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,68 @@
55

66
namespace FFmpeg.NET.Extensions
77
{
8-
public static class ProcessExtensions
9-
{
10-
public static Task<int> WaitForExitAsync(this Process process, bool stdInDataInput, Action<int> onException, CancellationToken cancellationToken = default)
11-
{
12-
CancellationTokenRegistration ctRegistration = new CancellationTokenRegistration();
13-
bool mustUnregister = false;
14-
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
15-
if (cancellationToken != default)
16-
{
17-
mustUnregister = true;
18-
ctRegistration = cancellationToken.Register(() =>
19-
{
20-
try
21-
{
22-
if (stdInDataInput)
23-
{
24-
// If standard input used for data input just close it.
25-
// It will force process to stop (closing files).
26-
process.StandardInput.Close();
27-
}
28-
else
29-
{
30-
// Send "q" to ffmpeg, which will force it to stop (closing files).
31-
process.StandardInput.Write("q");
32-
}
33-
}
34-
catch (InvalidOperationException)
35-
{
36-
// If the process doesn't exist anymore, ignore it.
37-
}
38-
finally
39-
{
40-
// Cancel the task. This will throw an exception to the calling program.
41-
// Exc.Message will be "A task was canceled."
42-
try
43-
{
44-
tcs.SetCanceled();
45-
}
46-
catch (Exception)
47-
{
48-
}
49-
}
50-
});
51-
}
8+
public static class ProcessExtensions
9+
{
10+
public static Task<int> WaitForExitAsync(this Process process, bool stdInDataInput, Action<int> onException, CancellationToken cancellationToken = default)
11+
{
12+
var ctRegistration = new CancellationTokenRegistration();
13+
var mustUnregister = false;
14+
var tcs = new TaskCompletionSource<int>();
15+
if (cancellationToken != default)
16+
{
17+
mustUnregister = true;
18+
ctRegistration = cancellationToken.Register(() =>
19+
{
20+
try
21+
{
22+
if (stdInDataInput)
23+
// If standard input used for data input just close it.
24+
// It will force process to stop (closing files).
25+
process.StandardInput.Close();
26+
else
27+
// Send "q" to ffmpeg, which will force it to stop (closing files).
28+
process.StandardInput.Write("q");
29+
}
30+
catch (InvalidOperationException)
31+
{
32+
// If the process doesn't exist anymore, ignore it.
33+
}
34+
finally
35+
{
36+
// Cancel the task. This will throw an exception to the calling program.
37+
// Exc.Message will be "A task was canceled."
38+
try
39+
{
40+
tcs.SetCanceled();
41+
}
42+
catch (Exception)
43+
{
44+
}
45+
}
46+
});
47+
}
5248

53-
void processOnExited(object sender, EventArgs e)
54-
{
55-
process.WaitForExit();
56-
if (process.ExitCode != 0)
57-
onException?.Invoke(process.ExitCode);
58-
tcs.TrySetResult(process.ExitCode);
59-
if (mustUnregister) ctRegistration.Dispose();
60-
process.Exited -= processOnExited;
61-
}
49+
void processOnExited(object sender, EventArgs e)
50+
{
51+
process.WaitForExit();
52+
if (process.ExitCode != 0)
53+
onException?.Invoke(process.ExitCode);
54+
tcs.TrySetResult(process.ExitCode);
55+
if (mustUnregister) ctRegistration.Dispose();
56+
process.Exited -= processOnExited;
57+
}
6258

63-
process.EnableRaisingEvents = true;
64-
process.Exited += processOnExited;
59+
process.EnableRaisingEvents = true;
60+
process.Exited += processOnExited;
6561

66-
var started = process.Start();
67-
if (!started)
68-
tcs.TrySetException(new InvalidOperationException($"Could not start process {process}"));
62+
var started = process.Start();
63+
if (!started)
64+
tcs.TrySetException(new InvalidOperationException($"Could not start process {process}"));
6965

70-
process.BeginOutputReadLine();
71-
process.BeginErrorReadLine();
66+
process.BeginOutputReadLine();
67+
process.BeginErrorReadLine();
7268

73-
return tcs.Task;
74-
}
75-
}
69+
return tcs.Task;
70+
}
71+
}
7672
}

src/FFmpeg.NET/FFmpegParameters.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public class FFmpegParameters
88
public FFmpegTask Task { get; set; }
99
public IOutputArgument Output { get; set; }
1010
public IInputArgument Input { get; set; }
11+
public string WorkingDirectory { get; set; }
1112
}
1213
}

0 commit comments

Comments
 (0)