diff --git a/FFMpegCore.Test/ArgumentBuilderTest.cs b/FFMpegCore.Test/ArgumentBuilderTest.cs
index 6fd108d..082d9bf 100644
--- a/FFMpegCore.Test/ArgumentBuilderTest.cs
+++ b/FFMpegCore.Test/ArgumentBuilderTest.cs
@@ -231,6 +231,13 @@ public void Builder_BuildString_FrameOutputCount()
Assert.AreEqual("-i \"input.mp4\" -vframes 50 \"output.mp4\"", str);
}
+ [TestMethod]
+ public void Builder_BuildString_VideoStreamNumber()
+ {
+ var str = FFMpegArguments.FromFileInput("input.mp4").OutputToFile("output.mp4", false, opt => opt.SelectStream(1)).Arguments;
+ Assert.AreEqual("-i \"input.mp4\" -map 0:1 \"output.mp4\"", str);
+ }
+
[TestMethod]
public void Builder_BuildString_FrameRate()
{
diff --git a/FFMpegCore/FFMpeg/Arguments/MapStreamArgument.cs b/FFMpegCore/FFMpeg/Arguments/MapStreamArgument.cs
new file mode 100644
index 0000000..b904be5
--- /dev/null
+++ b/FFMpegCore/FFMpeg/Arguments/MapStreamArgument.cs
@@ -0,0 +1,19 @@
+namespace FFMpegCore.Arguments
+{
+ ///
+ /// Represents choice of video stream
+ ///
+ public class MapStreamArgument : IArgument
+ {
+ private readonly int _inputFileIndex;
+ private readonly int _streamIndex;
+
+ public MapStreamArgument(int streamIndex, int inputFileIndex)
+ {
+ _inputFileIndex = inputFileIndex;
+ _streamIndex = streamIndex;
+ }
+
+ public string Text => $"-map {_inputFileIndex}:{_streamIndex}";
+ }
+}
\ No newline at end of file
diff --git a/FFMpegCore/FFMpeg/FFMpeg.cs b/FFMpegCore/FFMpeg/FFMpeg.cs
index 2928e29..a345160 100644
--- a/FFMpegCore/FFMpeg/FFMpeg.cs
+++ b/FFMpegCore/FFMpeg/FFMpeg.cs
@@ -20,15 +20,17 @@ public static class FFMpeg
/// Output video file path
/// Seek position where the thumbnail should be taken.
/// Thumbnail size. If width or height equal 0, the other will be computed automatically.
+ /// Selected video stream index.
+ /// Input file index
/// Bitmap with the requested snapshot.
- public static bool Snapshot(string input, string output, Size? size = null, TimeSpan? captureTime = null)
+ public static bool Snapshot(string input, string output, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
{
if (Path.GetExtension(output) != FileExtension.Png)
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
var source = FFProbe.Analyse(input);
- var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime);
-
+ var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
+
return arguments
.OutputToFile(output, true, outputOptions)
.ProcessSynchronously();
@@ -40,15 +42,17 @@ public static bool Snapshot(string input, string output, Size? size = null, Time
/// Output video file path
/// Seek position where the thumbnail should be taken.
/// Thumbnail size. If width or height equal 0, the other will be computed automatically.
+ /// Selected video stream index.
+ /// Input file index
/// Bitmap with the requested snapshot.
- public static async Task SnapshotAsync(string input, string output, Size? size = null, TimeSpan? captureTime = null)
+ public static async Task SnapshotAsync(string input, string output, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
{
if (Path.GetExtension(output) != FileExtension.Png)
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
var source = await FFProbe.AnalyseAsync(input);
- var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime);
-
+ var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
+
return await arguments
.OutputToFile(output, true, outputOptions)
.ProcessAsynchronously();
@@ -60,13 +64,15 @@ public static async Task SnapshotAsync(string input, string output, Size?
/// Source video file.
/// Seek position where the thumbnail should be taken.
/// Thumbnail size. If width or height equal 0, the other will be computed automatically.
+ /// Selected video stream index.
+ /// Input file index
/// Bitmap with the requested snapshot.
- public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null)
+ public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
{
var source = FFProbe.Analyse(input);
- var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime);
+ var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
using var ms = new MemoryStream();
-
+
arguments
.OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options
.ForceFormat("rawvideo")))
@@ -82,13 +88,15 @@ public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? capture
/// Source video file.
/// Seek position where the thumbnail should be taken.
/// Thumbnail size. If width or height equal 0, the other will be computed automatically.
+ /// Selected video stream index.
+ /// Input file index
/// Bitmap with the requested snapshot.
- public static async Task SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null)
+ public static async Task SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
{
var source = await FFProbe.AnalyseAsync(input);
- var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime);
+ var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
using var ms = new MemoryStream();
-
+
await arguments
.OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options
.ForceFormat("rawvideo")))
@@ -98,15 +106,23 @@ await arguments
return new Bitmap(ms);
}
- private static (FFMpegArguments, Action outputOptions) BuildSnapshotArguments(string input, IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null)
+ private static (FFMpegArguments, Action outputOptions) BuildSnapshotArguments(
+ string input,
+ IMediaAnalysis source,
+ Size? size = null,
+ TimeSpan? captureTime = null,
+ int? streamIndex = null,
+ int inputFileIndex = 0)
{
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
size = PrepareSnapshotSize(source, size);
+ streamIndex = streamIndex == null ? 0 : source.VideoStreams.FirstOrDefault(videoStream => videoStream.Index == streamIndex).Index;
return (FFMpegArguments
.FromFileInput(input, false, options => options
- .Seek(captureTime)),
+ .Seek(captureTime)),
options => options
+ .SelectStream((int)streamIndex, inputFileIndex)
.WithVideoCodec(VideoCodec.Png)
.WithFrameOutputCount(1)
.Resize(size));
@@ -116,11 +132,11 @@ private static (FFMpegArguments, Action outputOptions) Bu
{
if (wantedSize == null || (wantedSize.Value.Height <= 0 && wantedSize.Value.Width <= 0) || source.PrimaryVideoStream == null)
return null;
-
+
var currentSize = new Size(source.PrimaryVideoStream.Width, source.PrimaryVideoStream.Height);
if (source.PrimaryVideoStream.Rotation == 90 || source.PrimaryVideoStream.Rotation == 180)
currentSize = new Size(source.PrimaryVideoStream.Height, source.PrimaryVideoStream.Width);
-
+
if (wantedSize.Value.Width != currentSize.Width || wantedSize.Value.Height != currentSize.Height)
{
if (wantedSize.Value.Width <= 0 && wantedSize.Value.Height > 0)
diff --git a/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs b/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs
index be342c4..41ac38c 100644
--- a/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs
+++ b/FFMpegCore/FFMpeg/FFMpegArgumentOptions.cs
@@ -8,7 +8,7 @@ namespace FFMpegCore
public class FFMpegArgumentOptions : FFMpegArgumentsBase
{
internal FFMpegArgumentOptions() { }
-
+
public FFMpegArgumentOptions WithAudioCodec(Codec audioCodec) => WithArgument(new AudioCodecArgument(audioCodec));
public FFMpegArgumentOptions WithAudioCodec(string audioCodec) => WithArgument(new AudioCodecArgument(audioCodec));
public FFMpegArgumentOptions WithAudioBitrate(AudioQuality audioQuality) => WithArgument(new AudioBitrateArgument(audioQuality));
@@ -17,9 +17,9 @@ internal FFMpegArgumentOptions() { }
public FFMpegArgumentOptions WithVariableBitrate(int vbr) => WithArgument(new VariableBitRateArgument(vbr));
public FFMpegArgumentOptions Resize(int width, int height) => WithArgument(new SizeArgument(width, height));
public FFMpegArgumentOptions Resize(Size? size) => WithArgument(new SizeArgument(size));
-
-
+
+
public FFMpegArgumentOptions WithBitStreamFilter(Channel channel, Filter filter) => WithArgument(new BitStreamFilterArgument(channel, filter));
public FFMpegArgumentOptions WithConstantRateFactor(int crf) => WithArgument(new ConstantRateFactorArgument(crf));
public FFMpegArgumentOptions CopyChannel(Channel channel = Channel.Both) => WithArgument(new CopyArgument(channel));
@@ -28,11 +28,11 @@ internal FFMpegArgumentOptions() { }
public FFMpegArgumentOptions WithFastStart() => WithArgument(new FaststartArgument());
public FFMpegArgumentOptions WithFrameOutputCount(int frames) => WithArgument(new FrameOutputCountArgument(frames));
public FFMpegArgumentOptions WithHardwareAcceleration(HardwareAccelerationDevice hardwareAccelerationDevice = HardwareAccelerationDevice.Auto) => WithArgument(new HardwareAccelerationArgument(hardwareAccelerationDevice));
-
+
public FFMpegArgumentOptions UsingShortest(bool shortest = true) => WithArgument(new ShortestArgument(shortest));
public FFMpegArgumentOptions UsingMultithreading(bool multithread) => WithArgument(new ThreadsArgument(multithread));
public FFMpegArgumentOptions UsingThreads(int threads) => WithArgument(new ThreadsArgument(threads));
-
+
public FFMpegArgumentOptions WithVideoCodec(Codec videoCodec) => WithArgument(new VideoCodecArgument(videoCodec));
public FFMpegArgumentOptions WithVideoCodec(string videoCodec) => WithArgument(new VideoCodecArgument(videoCodec));
public FFMpegArgumentOptions WithVideoBitrate(int bitrate) => WithArgument(new VideoBitrateArgument(bitrate));
@@ -48,10 +48,11 @@ public FFMpegArgumentOptions WithVideoFilters(Action videoFi
public FFMpegArgumentOptions WithSpeedPreset(Speed speed) => WithArgument(new SpeedPresetArgument(speed));
public FFMpegArgumentOptions WithStartNumber(int startNumber) => WithArgument(new StartNumberArgument(startNumber));
public FFMpegArgumentOptions WithCustomArgument(string argument) => WithArgument(new CustomArgument(argument));
-
+
public FFMpegArgumentOptions Seek(TimeSpan? seekTo) => WithArgument(new SeekArgument(seekTo));
public FFMpegArgumentOptions Loop(int times) => WithArgument(new LoopArgument(times));
public FFMpegArgumentOptions OverwriteExisting() => WithArgument(new OverwriteArgument());
+ public FFMpegArgumentOptions SelectStream(int streamIndex, int inputFileIndex = 0) => WithArgument(new MapStreamArgument(streamIndex, inputFileIndex));
public FFMpegArgumentOptions ForceFormat(ContainerFormat format) => WithArgument(new ForceFormatArgument(format));
public FFMpegArgumentOptions ForceFormat(string format) => WithArgument(new ForceFormatArgument(format));