-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for multiple outputs and tee muxer.
A single input can be encoded simultaneously to multiple oputs or muxed in multiple formats
- Loading branch information
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
| ||
namespace FFMpegCore.Arguments | ||
{ | ||
internal class OutputTeeArgument : IOutputArgument | ||
{ | ||
private readonly FFMpegMultiOutputOptions _options; | ||
|
||
public OutputTeeArgument(FFMpegMultiOutputOptions options) | ||
{ | ||
if (options.Outputs.Count == 0) | ||
{ | ||
throw new ArgumentException("Atleast one output must be specified.", nameof(options)); | ||
} | ||
|
||
_options = options; | ||
} | ||
|
||
public string Text => $"-f tee \"{string.Join("|", _options.Outputs.Select(MapOptions))}\""; | ||
|
||
public Task During(CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
|
||
public void Post() | ||
{ | ||
} | ||
|
||
public void Pre() | ||
{ | ||
} | ||
|
||
private static string MapOptions(FFMpegArgumentOptions option) | ||
{ | ||
var optionPrefix = string.Empty; | ||
if (option.Arguments.Count > 1) | ||
{ | ||
var options = option.Arguments.Take(option.Arguments.Count - 1); | ||
optionPrefix = $"[{string.Join(":", options.Select(MapArgument))}]"; | ||
} | ||
|
||
var output = option.Arguments.OfType<IOutputArgument>().Single(); | ||
return $"{optionPrefix}{output.Text.Trim('"')}"; | ||
} | ||
|
||
private static string MapArgument(IArgument argument) | ||
{ | ||
if (argument is MapStreamArgument map) | ||
{ | ||
return map.Text.Replace("-map ", "select=\\'") + "\\'"; | ||
} | ||
else if (argument is BitStreamFilterArgument bitstreamFilter) | ||
{ | ||
return bitstreamFilter.Text.Replace("-bsf:", "bsfs/").Replace(' ', '='); | ||
} | ||
|
||
return argument.Text.TrimStart('-').Replace(' ', '='); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using FFMpegCore.Arguments; | ||
using FFMpegCore.Pipes; | ||
|
||
namespace FFMpegCore | ||
{ | ||
public class FFMpegMultiOutputOptions | ||
{ | ||
internal readonly List<FFMpegArgumentOptions> Outputs = new(); | ||
|
||
public IEnumerable<IArgument> Arguments => Outputs.SelectMany(o => o.Arguments); | ||
|
||
public FFMpegMultiOutputOptions OutputToFile(string file, bool overwrite = true, Action<FFMpegArgumentOptions>? addArguments = null) => AddOutput(new OutputArgument(file, overwrite), addArguments); | ||
|
||
public FFMpegMultiOutputOptions OutputToUrl(string uri, Action<FFMpegArgumentOptions>? addArguments = null) => AddOutput(new OutputUrlArgument(uri), addArguments); | ||
|
||
public FFMpegMultiOutputOptions OutputToUrl(Uri uri, Action<FFMpegArgumentOptions>? addArguments = null) => AddOutput(new OutputUrlArgument(uri.ToString()), addArguments); | ||
|
||
public FFMpegMultiOutputOptions OutputToPipe(IPipeSink reader, Action<FFMpegArgumentOptions>? addArguments = null) => AddOutput(new OutputPipeArgument(reader), addArguments); | ||
|
||
public FFMpegMultiOutputOptions AddOutput(IOutputArgument argument, Action<FFMpegArgumentOptions>? addArguments) | ||
{ | ||
var args = new FFMpegArgumentOptions(); | ||
addArguments?.Invoke(args); | ||
args.Arguments.Add(argument); | ||
Outputs.Add(args); | ||
return this; | ||
} | ||
} | ||
} |