Skip to content

Commit 4d29bfe

Browse files
committed
fix: avoid throwing exception when trying to create an output directory for ffmpeg process
1 parent ca0cd79 commit 4d29bfe

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

src/LiveStreamingServerNet.StreamProcessor/Internal/FFmpeg/FFmpegProcess.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,39 @@ public async Task RunAsync(
3232
OnStreamProcessorEnded? onEnded,
3333
CancellationToken cancellation)
3434
{
35-
if (!Uri.IsWellFormedUriString(inputPath, UriKind.Absolute))
36-
DirectoryUtility.CreateDirectoryIfNotExists(Path.GetDirectoryName(_config.OutputPath));
35+
EnsureOutputDirectoryExists(_config.OutputPath);
36+
CleanOutputFile(_config.OutputPath);
37+
3738
await RunProcessAsync(inputPath, _config.OutputPath, onStarted, onEnded, cancellation);
3839
}
3940

41+
private void EnsureOutputDirectoryExists(string outputPath)
42+
{
43+
ErrorBoundary.Execute(() =>
44+
{
45+
if (IsNonUriOutputPath(outputPath))
46+
{
47+
DirectoryUtility.CreateDirectoryIfNotExists(Path.GetDirectoryName(_config.OutputPath));
48+
}
49+
});
50+
}
51+
52+
private static void CleanOutputFile(string outputPath)
53+
{
54+
ErrorBoundary.Execute(() =>
55+
{
56+
if (IsNonUriOutputPath(outputPath))
57+
{
58+
File.Delete(outputPath);
59+
}
60+
});
61+
}
62+
63+
private static bool IsNonUriOutputPath(string outputPath)
64+
{
65+
return !string.IsNullOrWhiteSpace(outputPath) && !Uri.IsWellFormedUriString(outputPath, UriKind.Absolute);
66+
}
67+
4068
private async Task RunProcessAsync(string inputPath, string outputPath, OnStreamProcessorStarted? onStarted, OnStreamProcessorEnded? onEnded, CancellationToken cancellation)
4169
{
4270
var arguments = _config.Arguments
@@ -49,9 +77,6 @@ private async Task RunProcessAsync(string inputPath, string outputPath, OnStream
4977

5078
try
5179
{
52-
if (!string.IsNullOrWhiteSpace(outputPath))
53-
File.Delete(outputPath);
54-
5580
process.StartInfo = new ProcessStartInfo
5681
{
5782
FileName = _config.FFmpegPath,
@@ -86,7 +111,6 @@ private async Task RunProcessAsync(string inputPath, string outputPath, OnStream
86111
await onEnded.Invoke(outputPath);
87112
}
88113
}
89-
90114
private static async Task WaitForProcessTerminatingGracefully(Process process, int gracefulPeriod)
91115
{
92116
if (!process.HasExited)

0 commit comments

Comments
 (0)