Refactoring

Former-commit-id: c47214b7aa
This commit is contained in:
Fedor Zhilkin 2020-11-13 02:09:52 +03:00
parent 0126cd1e77
commit 4b08805467
2 changed files with 42 additions and 33 deletions

View file

@ -5,13 +5,13 @@
/// </summary> /// </summary>
public class MapStreamArgument : IArgument public class MapStreamArgument : IArgument
{ {
public readonly int VideoStream; private readonly int _streamIndex;
public MapStreamArgument(int videoStreamNum) public MapStreamArgument(int index)
{ {
VideoStream = videoStreamNum; _streamIndex = index;
} }
public string Text => $"-map 0:{VideoStream}"; public string Text => $"-map 0:{_streamIndex}";
} }
} }

View file

@ -20,14 +20,14 @@ 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="videoStreamNumber">Number of video stream in input file. Default it is 0.</param> /// <param name="streamIndex">Index of video stream in input file. Default it is 0.</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
public static bool Snapshot(IMediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0) public static bool Snapshot(IMediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
{ {
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 (arguments, outputOptions) = BuildSnapshotArguments(source, size, captureTime, videoStreamNumber); var (arguments, outputOptions) = BuildSnapshotArguments(source, size, captureTime, streamIndex);
return arguments return arguments
.OutputToFile(output, true, outputOptions) .OutputToFile(output, true, outputOptions)
@ -40,14 +40,14 @@ public static bool Snapshot(IMediaAnalysis source, string output, Size? size = n
/// <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="videoStreamNumber">Number of video stream in input file. Default it is 0.</param> /// <param name="streamIndex">Index of video stream in input file. Default it is 0.</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
public static Task<bool> SnapshotAsync(IMediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0) public static Task<bool> SnapshotAsync(IMediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
{ {
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 (arguments, outputOptions) = BuildSnapshotArguments(source, size, captureTime, videoStreamNumber); var (arguments, outputOptions) = BuildSnapshotArguments(source, size, captureTime, streamIndex);
return arguments return arguments
.OutputToFile(output, true, outputOptions) .OutputToFile(output, true, outputOptions)
@ -59,11 +59,11 @@ public static Task<bool> SnapshotAsync(IMediaAnalysis source, string output, Siz
/// <param name="source">Source video file.</param> /// <param name="source">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="videoStreamNumber">Number of video stream in input file. Default it is 0.</param> /// <param name="streamIndex">Index of video stream in input file. Default it is 0.</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
public static Bitmap Snapshot(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0) public static Bitmap Snapshot(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
{ {
var (arguments, outputOptions) = BuildSnapshotArguments(source, size, captureTime, videoStreamNumber); var (arguments, outputOptions) = BuildSnapshotArguments(source, size, captureTime, streamIndex);
using var ms = new MemoryStream(); using var ms = new MemoryStream();
arguments arguments
@ -80,11 +80,11 @@ public static Bitmap Snapshot(IMediaAnalysis source, Size? size = null, TimeSpan
/// <param name="source">Source video file.</param> /// <param name="source">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="videoStreamNumber">Number of video stream in input file. Default it is 0.</param> /// <param name="streamIndex">Index of video stream in input file. Default it is 0.</param>
/// <returns>Bitmap with the requested snapshot.</returns> /// <returns>Bitmap with the requested snapshot.</returns>
public static async Task<Bitmap> SnapshotAsync(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0) public static async Task<Bitmap> SnapshotAsync(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
{ {
var (arguments, outputOptions) = BuildSnapshotArguments(source, size, captureTime, videoStreamNumber); var (arguments, outputOptions) = BuildSnapshotArguments(source, size, captureTime, streamIndex);
using var ms = new MemoryStream(); using var ms = new MemoryStream();
await arguments await arguments
@ -96,16 +96,25 @@ await arguments
return new Bitmap(ms); return new Bitmap(ms);
} }
private static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildSnapshotArguments(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int videoStreamNumber = 0) private static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildSnapshotArguments(IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int streamIndex = 0)
{ {
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3); captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
size = PrepareSnapshotSize(source, size); size = PrepareSnapshotSize(source, size);
// If user will know about numeration of streams (user passes index of necessary video stream)
int index = source.VideoStreams.Where(videoStream => videoStream.Index == streamIndex).FirstOrDefault().Index;
// User passes number of video stream
// E.g: user can pass 0, but index of first video stream will be 1
/*int index = 0;
try { index = source.VideoStreams[streamIndex].Index; }
catch { };*/
return (FFMpegArguments return (FFMpegArguments
.FromFileInput(source, options => options .FromFileInput(source, options => options
.Seek(captureTime)), .Seek(captureTime)),
options => options options => options
.SelectStream(videoStreamNumber) .SelectStream(index)
.WithVideoCodec(VideoCodec.Png) .WithVideoCodec(VideoCodec.Png)
.WithFrameOutputCount(1) .WithFrameOutputCount(1)
.Resize(size)); .Resize(size));
@ -451,7 +460,7 @@ private static void ParsePartOfCodecs(Dictionary<string, Codec> codecs, string a
instance.DataReceived += (e, args) => instance.DataReceived += (e, args) =>
{ {
var codec = parser(args.Data); var codec = parser(args.Data);
if(codec != null) if (codec != null)
if (codecs.TryGetValue(codec.Name, out var parentCodec)) if (codecs.TryGetValue(codec.Name, out var parentCodec))
parentCodec.Merge(codec); parentCodec.Merge(codec);
else else
@ -498,7 +507,7 @@ public static IReadOnlyList<Codec> GetCodecs(CodecType type)
{ {
if (!FFMpegOptions.Options.UseCache) if (!FFMpegOptions.Options.UseCache)
return GetCodecsInternal().Values.Where(x => x.Type == type).ToList().AsReadOnly(); return GetCodecsInternal().Values.Where(x => x.Type == type).ToList().AsReadOnly();
return FFMpegCache.Codecs.Values.Where(x=>x.Type == type).ToList().AsReadOnly(); return FFMpegCache.Codecs.Values.Where(x => x.Type == type).ToList().AsReadOnly();
} }
public static IReadOnlyList<Codec> GetVideoCodecs() => GetCodecs(CodecType.Video); public static IReadOnlyList<Codec> GetVideoCodecs() => GetCodecs(CodecType.Video);