mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2024-11-10 08:34:12 +01:00
parent
50085dec4d
commit
1b24a71636
2 changed files with 59 additions and 21 deletions
|
@ -7,6 +7,7 @@
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace FFMpegCore
|
namespace FFMpegCore
|
||||||
{
|
{
|
||||||
|
@ -22,23 +23,35 @@ public static class FFMpeg
|
||||||
/// <returns>Bitmap with the requested snapshot.</returns>
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||||
public static bool Snapshot(MediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null)
|
public static bool Snapshot(MediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null)
|
||||||
{
|
{
|
||||||
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
|
|
||||||
|
|
||||||
if (Path.GetExtension(output) != FileExtension.Png)
|
if (Path.GetExtension(output) != FileExtension.Png)
|
||||||
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
|
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
|
||||||
|
|
||||||
size = PrepareSnapshotSize(source, size);
|
var arguments = BuildSnapshotArguments(source, size, captureTime);
|
||||||
|
|
||||||
return FFMpegArguments
|
return arguments
|
||||||
.FromSeekedFiles((source.Path, captureTime ?? TimeSpan.Zero))
|
|
||||||
.WithVideoCodec(VideoCodec.Png)
|
|
||||||
.WithFrameOutputCount(1)
|
|
||||||
.Resize(size)
|
|
||||||
.Seek(captureTime)
|
|
||||||
.OutputToFile(output)
|
.OutputToFile(output)
|
||||||
.ProcessSynchronously();
|
.ProcessSynchronously();
|
||||||
}
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
/// Saves a 'png' thumbnail from the input video to drive
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source">Source video analysis</param>
|
||||||
|
/// <param name="output">Output video file path</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>
|
||||||
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||||
|
public static Task<bool> SnapshotAsync(MediaAnalysis source, string output, Size? size = null, TimeSpan? captureTime = null)
|
||||||
|
{
|
||||||
|
if (Path.GetExtension(output) != FileExtension.Png)
|
||||||
|
output = Path.GetFileNameWithoutExtension(output) + FileExtension.Png;
|
||||||
|
|
||||||
|
var arguments = BuildSnapshotArguments(source, size, captureTime);
|
||||||
|
|
||||||
|
return arguments
|
||||||
|
.OutputToFile(output)
|
||||||
|
.ProcessAsynchronously();
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
/// Saves a 'png' thumbnail to an in-memory bitmap
|
/// Saves a 'png' thumbnail to an in-memory bitmap
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="source">Source video file.</param>
|
/// <param name="source">Source video file.</param>
|
||||||
|
@ -47,17 +60,10 @@ public static bool Snapshot(MediaAnalysis source, string output, Size? size = nu
|
||||||
/// <returns>Bitmap with the requested snapshot.</returns>
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||||
public static Bitmap Snapshot(MediaAnalysis source, Size? size = null, TimeSpan? captureTime = null)
|
public static Bitmap Snapshot(MediaAnalysis source, Size? size = null, TimeSpan? captureTime = null)
|
||||||
{
|
{
|
||||||
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
|
var arguments = BuildSnapshotArguments(source, size, captureTime);
|
||||||
|
|
||||||
size = PrepareSnapshotSize(source, size);
|
|
||||||
|
|
||||||
using var ms = new MemoryStream();
|
using var ms = new MemoryStream();
|
||||||
FFMpegArguments
|
|
||||||
.FromInputFiles(source.Path)
|
arguments
|
||||||
.WithVideoCodec(VideoCodec.Png)
|
|
||||||
.WithFrameOutputCount(1)
|
|
||||||
.Resize(size)
|
|
||||||
.Seek(captureTime)
|
|
||||||
.ForceFormat("rawvideo")
|
.ForceFormat("rawvideo")
|
||||||
.OutputToPipe(new StreamPipeSink(ms))
|
.OutputToPipe(new StreamPipeSink(ms))
|
||||||
.ProcessSynchronously();
|
.ProcessSynchronously();
|
||||||
|
@ -65,6 +71,38 @@ public static Bitmap Snapshot(MediaAnalysis source, Size? size = null, TimeSpan?
|
||||||
ms.Position = 0;
|
ms.Position = 0;
|
||||||
return new Bitmap(ms);
|
return new Bitmap(ms);
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Saves a 'png' thumbnail to an in-memory bitmap
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="source">Source video file.</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>
|
||||||
|
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||||
|
public static async Task<Bitmap> SnapshotAsync(MediaAnalysis source, Size? size = null, TimeSpan? captureTime = null)
|
||||||
|
{
|
||||||
|
var arguments = BuildSnapshotArguments(source, size, captureTime);
|
||||||
|
using var ms = new MemoryStream();
|
||||||
|
|
||||||
|
await arguments
|
||||||
|
.ForceFormat("rawvideo")
|
||||||
|
.OutputToPipe(new StreamPipeSink(ms))
|
||||||
|
.ProcessAsynchronously();
|
||||||
|
|
||||||
|
ms.Position = 0;
|
||||||
|
return new Bitmap(ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static FFMpegArguments BuildSnapshotArguments(MediaAnalysis source, Size? size = null, TimeSpan? captureTime = null)
|
||||||
|
{
|
||||||
|
captureTime ??= TimeSpan.FromSeconds(source.Duration.TotalSeconds / 3);
|
||||||
|
size = PrepareSnapshotSize(source, size);
|
||||||
|
|
||||||
|
return FFMpegArguments
|
||||||
|
.FromSeekedFiles((source.Path, captureTime ?? TimeSpan.Zero))
|
||||||
|
.WithVideoCodec(VideoCodec.Png)
|
||||||
|
.WithFrameOutputCount(1)
|
||||||
|
.Resize(size);
|
||||||
|
}
|
||||||
|
|
||||||
private static Size? PrepareSnapshotSize(MediaAnalysis source, Size? size)
|
private static Size? PrepareSnapshotSize(MediaAnalysis source, Size? size)
|
||||||
{
|
{
|
||||||
|
|
|
@ -46,7 +46,7 @@ public FFMpegArgumentProcessor CancellableThrough(out Action cancel)
|
||||||
}
|
}
|
||||||
public bool ProcessSynchronously(bool throwOnError = true)
|
public bool ProcessSynchronously(bool throwOnError = true)
|
||||||
{
|
{
|
||||||
var instance = PrepareInstance(out var cancellationTokenSource);
|
using var instance = PrepareInstance(out var cancellationTokenSource);
|
||||||
var errorCode = -1;
|
var errorCode = -1;
|
||||||
|
|
||||||
void OnCancelEvent(object sender, EventArgs args)
|
void OnCancelEvent(object sender, EventArgs args)
|
||||||
|
|
Loading…
Reference in a new issue