mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2024-11-10 08:34:12 +01:00
Nullable streamIndex and inputFileIndex
This commit is contained in:
parent
3888a07ab2
commit
562a50d874
4 changed files with 37 additions and 25 deletions
|
@ -234,7 +234,7 @@ public void Builder_BuildString_FrameOutputCount()
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Builder_BuildString_VideoStreamNumber()
|
public void Builder_BuildString_VideoStreamNumber()
|
||||||
{
|
{
|
||||||
var str = FFMpegArguments.FromFileInput("input.mp4").OutputToFile("output.mp4", false, opt => opt.SelectStream(1)).Arguments;
|
var str = FFMpegArguments.FromFileInput("input.mp4").OutputToFile("output.mp4", false, opt => opt.SelectStream(0,1)).Arguments;
|
||||||
Assert.AreEqual("-i \"input.mp4\" -map 0:1 \"output.mp4\"", str);
|
Assert.AreEqual("-i \"input.mp4\" -map 0:1 \"output.mp4\"", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
namespace FFMpegCore.Arguments
|
namespace FFMpegCore.Arguments
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents choice of video stream, works with one input file
|
/// Represents choice of video stream
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class MapStreamArgument : IArgument
|
public class MapStreamArgument : IArgument
|
||||||
{
|
{
|
||||||
|
private readonly int _inputFileIndex;
|
||||||
private readonly int _streamIndex;
|
private readonly int _streamIndex;
|
||||||
|
|
||||||
public MapStreamArgument(int index)
|
public MapStreamArgument(int inputFileIndex, int streamIndex)
|
||||||
{
|
{
|
||||||
_streamIndex = index;
|
_inputFileIndex = inputFileIndex;
|
||||||
|
_streamIndex = streamIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string Text => $"-map 0:{_streamIndex}";
|
public string Text => $"-map {_inputFileIndex}:{_streamIndex}";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -20,16 +20,17 @@ public static class FFMpeg
|
||||||
/// <param name="output">Output video file path</param>
|
/// <param name="output">Output video file path</param>
|
||||||
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
|
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
|
||||||
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
|
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
|
||||||
|
/// <param name="inputFileIndex">Input file index</param>
|
||||||
/// <param name="streamIndex">Selected video stream index.</param>
|
/// <param name="streamIndex">Selected video stream index.</param>
|
||||||
/// <returns>Bitmap with the requested snapshot.</returns>
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||||
public static bool Snapshot(string input, string output, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
|
public static bool Snapshot(string input, string output, Size? size = null, TimeSpan? captureTime = null, int inputFileIndex = 0, int? streamIndex = null)
|
||||||
{
|
{
|
||||||
if (Path.GetExtension(output) != FileExtension.Png)
|
if (Path.GetExtension(output) != FileExtension.Png)
|
||||||
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
|
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
|
||||||
|
|
||||||
var source = FFProbe.Analyse(input);
|
var source = FFProbe.Analyse(input);
|
||||||
var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex);
|
var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, inputFileIndex, streamIndex);
|
||||||
|
|
||||||
return arguments
|
return arguments
|
||||||
.OutputToFile(output, true, outputOptions)
|
.OutputToFile(output, true, outputOptions)
|
||||||
.ProcessSynchronously();
|
.ProcessSynchronously();
|
||||||
|
@ -41,16 +42,17 @@ public static bool Snapshot(string input, string output, Size? size = null, Time
|
||||||
/// <param name="output">Output video file path</param>
|
/// <param name="output">Output video file path</param>
|
||||||
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
|
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
|
||||||
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
|
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
|
||||||
|
/// <param name="inputFileIndex">Input file index</param>
|
||||||
/// <param name="streamIndex">Selected video stream index.</param>
|
/// <param name="streamIndex">Selected video stream index.</param>
|
||||||
/// <returns>Bitmap with the requested snapshot.</returns>
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||||
public static async Task<bool> SnapshotAsync(string input, string output, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
|
public static async Task<bool> SnapshotAsync(string input, string output, Size? size = null, TimeSpan? captureTime = null, int inputFileIndex = 0, int? streamIndex = null)
|
||||||
{
|
{
|
||||||
if (Path.GetExtension(output) != FileExtension.Png)
|
if (Path.GetExtension(output) != FileExtension.Png)
|
||||||
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
|
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
|
||||||
|
|
||||||
var source = await FFProbe.AnalyseAsync(input);
|
var source = await FFProbe.AnalyseAsync(input);
|
||||||
var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex);
|
var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, inputFileIndex, streamIndex);
|
||||||
|
|
||||||
return await arguments
|
return await arguments
|
||||||
.OutputToFile(output, true, outputOptions)
|
.OutputToFile(output, true, outputOptions)
|
||||||
.ProcessAsynchronously();
|
.ProcessAsynchronously();
|
||||||
|
@ -62,14 +64,15 @@ public static async Task<bool> SnapshotAsync(string input, string output, Size?
|
||||||
/// <param name="input">Source video file.</param>
|
/// <param name="input">Source video file.</param>
|
||||||
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
|
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
|
||||||
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
|
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
|
||||||
|
/// <param name="inputFileIndex">Input file index</param>
|
||||||
/// <param name="streamIndex">Selected video stream index.</param>
|
/// <param name="streamIndex">Selected video stream index.</param>
|
||||||
/// <returns>Bitmap with the requested snapshot.</returns>
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||||
public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
|
public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? captureTime = null, int inputFileIndex = 0, int? streamIndex = null)
|
||||||
{
|
{
|
||||||
var source = FFProbe.Analyse(input);
|
var source = FFProbe.Analyse(input);
|
||||||
var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex);
|
var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, inputFileIndex, streamIndex);
|
||||||
using var ms = new MemoryStream();
|
using var ms = new MemoryStream();
|
||||||
|
|
||||||
arguments
|
arguments
|
||||||
.OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options
|
.OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options
|
||||||
.ForceFormat("rawvideo")))
|
.ForceFormat("rawvideo")))
|
||||||
|
@ -85,14 +88,15 @@ public static Bitmap Snapshot(string input, Size? size = null, TimeSpan? capture
|
||||||
/// <param name="input">Source video file.</param>
|
/// <param name="input">Source video file.</param>
|
||||||
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
|
/// <param name="captureTime">Seek position where the thumbnail should be taken.</param>
|
||||||
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
|
/// <param name="size">Thumbnail size. If width or height equal 0, the other will be computed automatically.</param>
|
||||||
|
/// <param name="inputFileIndex">Input file index</param>
|
||||||
/// <param name="streamIndex">Selected video stream index.</param>
|
/// <param name="streamIndex">Selected video stream index.</param>
|
||||||
/// <returns>Bitmap with the requested snapshot.</returns>
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||||
public static async Task<Bitmap> SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
|
public static async Task<Bitmap> SnapshotAsync(string input, Size? size = null, TimeSpan? captureTime = null, int inputFileIndex = 0, int? streamIndex = null)
|
||||||
{
|
{
|
||||||
var source = await FFProbe.AnalyseAsync(input);
|
var source = await FFProbe.AnalyseAsync(input);
|
||||||
var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, streamIndex);
|
var (arguments, outputOptions) = BuildSnapshotArguments(input, source, size, captureTime, inputFileIndex, streamIndex);
|
||||||
using var ms = new MemoryStream();
|
using var ms = new MemoryStream();
|
||||||
|
|
||||||
await arguments
|
await arguments
|
||||||
.OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options
|
.OutputToPipe(new StreamPipeSink(ms), options => outputOptions(options
|
||||||
.ForceFormat("rawvideo")))
|
.ForceFormat("rawvideo")))
|
||||||
|
@ -102,17 +106,23 @@ await arguments
|
||||||
return new Bitmap(ms);
|
return new Bitmap(ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildSnapshotArguments(string input, IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
|
private static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildSnapshotArguments(
|
||||||
|
string input,
|
||||||
|
IMediaAnalysis source,
|
||||||
|
Size? size = null,
|
||||||
|
TimeSpan? captureTime = null,
|
||||||
|
int inputFileIndex = 0,
|
||||||
|
int? streamIndex = null)
|
||||||
{
|
{
|
||||||
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
|
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
|
||||||
size = PrepareSnapshotSize(source, size);
|
size = PrepareSnapshotSize(source, size);
|
||||||
var index = source.VideoStreams.FirstOrDefault(videoStream => videoStream.Index == streamIndex)?.Index;
|
streamIndex = streamIndex == null ? 0 : source.VideoStreams.FirstOrDefault(videoStream => videoStream.Index == streamIndex).Index;
|
||||||
|
|
||||||
return (FFMpegArguments
|
return (FFMpegArguments
|
||||||
.FromFileInput(input, false, options => options
|
.FromFileInput(input, false, options => options
|
||||||
.Seek(captureTime)),
|
.Seek(captureTime)),
|
||||||
options => options
|
options => options
|
||||||
.SelectStream(index ?? 0)
|
.SelectStream((int)streamIndex, inputFileIndex)
|
||||||
.WithVideoCodec(VideoCodec.Png)
|
.WithVideoCodec(VideoCodec.Png)
|
||||||
.WithFrameOutputCount(1)
|
.WithFrameOutputCount(1)
|
||||||
.Resize(size));
|
.Resize(size));
|
||||||
|
@ -122,11 +132,11 @@ private static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) Bu
|
||||||
{
|
{
|
||||||
if (wantedSize == null || (wantedSize.Value.Height <= 0 && wantedSize.Value.Width <= 0) || source.PrimaryVideoStream == null)
|
if (wantedSize == null || (wantedSize.Value.Height <= 0 && wantedSize.Value.Width <= 0) || source.PrimaryVideoStream == null)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
var currentSize = new Size(source.PrimaryVideoStream.Width, source.PrimaryVideoStream.Height);
|
var currentSize = new Size(source.PrimaryVideoStream.Width, source.PrimaryVideoStream.Height);
|
||||||
if (source.PrimaryVideoStream.Rotation == 90 || source.PrimaryVideoStream.Rotation == 180)
|
if (source.PrimaryVideoStream.Rotation == 90 || source.PrimaryVideoStream.Rotation == 180)
|
||||||
currentSize = new Size(source.PrimaryVideoStream.Height, source.PrimaryVideoStream.Width);
|
currentSize = new Size(source.PrimaryVideoStream.Height, source.PrimaryVideoStream.Width);
|
||||||
|
|
||||||
if (wantedSize.Value.Width != currentSize.Width || wantedSize.Value.Height != currentSize.Height)
|
if (wantedSize.Value.Width != currentSize.Width || wantedSize.Value.Height != currentSize.Height)
|
||||||
{
|
{
|
||||||
if (wantedSize.Value.Width <= 0 && wantedSize.Value.Height > 0)
|
if (wantedSize.Value.Width <= 0 && wantedSize.Value.Height > 0)
|
||||||
|
|
|
@ -48,11 +48,11 @@ public FFMpegArgumentOptions WithVideoFilters(Action<VideoFilterOptions> videoFi
|
||||||
public FFMpegArgumentOptions WithSpeedPreset(Speed speed) => WithArgument(new SpeedPresetArgument(speed));
|
public FFMpegArgumentOptions WithSpeedPreset(Speed speed) => WithArgument(new SpeedPresetArgument(speed));
|
||||||
public FFMpegArgumentOptions WithStartNumber(int startNumber) => WithArgument(new StartNumberArgument(startNumber));
|
public FFMpegArgumentOptions WithStartNumber(int startNumber) => WithArgument(new StartNumberArgument(startNumber));
|
||||||
public FFMpegArgumentOptions WithCustomArgument(string argument) => WithArgument(new CustomArgument(argument));
|
public FFMpegArgumentOptions WithCustomArgument(string argument) => WithArgument(new CustomArgument(argument));
|
||||||
|
|
||||||
public FFMpegArgumentOptions Seek(TimeSpan? seekTo) => WithArgument(new SeekArgument(seekTo));
|
public FFMpegArgumentOptions Seek(TimeSpan? seekTo) => WithArgument(new SeekArgument(seekTo));
|
||||||
public FFMpegArgumentOptions Loop(int times) => WithArgument(new LoopArgument(times));
|
public FFMpegArgumentOptions Loop(int times) => WithArgument(new LoopArgument(times));
|
||||||
public FFMpegArgumentOptions OverwriteExisting() => WithArgument(new OverwriteArgument());
|
public FFMpegArgumentOptions OverwriteExisting() => WithArgument(new OverwriteArgument());
|
||||||
public FFMpegArgumentOptions SelectStream(int index) => WithArgument(new MapStreamArgument(index));
|
public FFMpegArgumentOptions SelectStream(int streamIndex, int inputFileIndex = 0) => WithArgument(new MapStreamArgument(inputFileIndex, streamIndex));
|
||||||
|
|
||||||
public FFMpegArgumentOptions ForceFormat(ContainerFormat format) => WithArgument(new ForceFormatArgument(format));
|
public FFMpegArgumentOptions ForceFormat(ContainerFormat format) => WithArgument(new ForceFormatArgument(format));
|
||||||
public FFMpegArgumentOptions ForceFormat(string format) => WithArgument(new ForceFormatArgument(format));
|
public FFMpegArgumentOptions ForceFormat(string format) => WithArgument(new ForceFormatArgument(format));
|
||||||
|
|
Loading…
Reference in a new issue