mirror of
https://github.com/rosenbjerg/FFMpegCore.git
synced 2025-12-15 02:25:44 +00:00
Compare commits
27 commits
7ae8dd9b4a
...
d3eeeb5cb4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d3eeeb5cb4 | ||
|
|
53b3e0fbd0 | ||
|
|
dac8f97e8b | ||
|
|
bfefa9560e | ||
|
|
37b4119e13 | ||
|
|
7e135a78d3 | ||
|
|
91c1629215 | ||
|
|
7036ad6df2 | ||
|
|
5ba8122d00 | ||
|
|
665c9f9213 | ||
|
|
fb10b78e35 | ||
|
|
99181e9f65 | ||
|
|
af6587d8fe | ||
|
|
9c636d4059 | ||
|
|
5d8d346598 | ||
|
|
918ca9a9ab | ||
|
|
abb9f15eeb | ||
|
|
193ed43f1d | ||
|
|
3d21599c5d | ||
|
|
4025b82fbf | ||
|
|
aa1051b268 | ||
|
|
52ed136459 | ||
|
|
b063d37464 | ||
|
|
935980568c | ||
|
|
91e8e1e18d | ||
|
|
f71e172f66 | ||
|
|
a6e90e2078 |
17 changed files with 451 additions and 45 deletions
13
.github/workflows/ci.yml
vendored
13
.github/workflows/ci.yml
vendored
|
|
@ -18,7 +18,7 @@ jobs:
|
|||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [windows-latest, ubuntu-latest, macos-13]
|
||||
os: [windows-latest, ubuntu-latest, macos-latest]
|
||||
timeout-minutes: 7
|
||||
steps:
|
||||
|
||||
|
|
@ -30,14 +30,15 @@ jobs:
|
|||
with:
|
||||
dotnet-version: '8.0.x'
|
||||
|
||||
- name: Lint with dotnet
|
||||
- if: matrix.os == 'ubuntu-latest'
|
||||
name: Lint with dotnet
|
||||
run: dotnet format FFMpegCore.sln --severity warn --verify-no-changes
|
||||
|
||||
- name: Prepare FFMpeg
|
||||
uses: FedericoCarboni/setup-ffmpeg@v3
|
||||
- name: Setup FFmpeg
|
||||
uses: AnimMouse/setup-ffmpeg@ae28d57dabbb148eff63170b6bf7f2b60062cbae # 1.1.0
|
||||
with:
|
||||
ffmpeg-version: 6.0.1
|
||||
github-token: ${{ secrets.GITHUB_TOKEN }}
|
||||
version: ${{ matrix.os != 'macos-latest' && '7.1' || '711' }}
|
||||
token: ${{ github.token }}
|
||||
|
||||
- name: Test with dotnet
|
||||
run: dotnet test FFMpegCore.sln --collect "XPlat Code Coverage" --logger GitHubActions
|
||||
|
|
|
|||
|
|
@ -16,7 +16,20 @@ namespace FFMpegCore.Test
|
|||
public class VideoTest
|
||||
{
|
||||
private const int BaseTimeoutMilliseconds = 15_000;
|
||||
|
||||
private string _segmentPathSource = "";
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
_segmentPathSource = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}-");
|
||||
}
|
||||
[TestCleanup]
|
||||
public void Cleanup()
|
||||
{
|
||||
foreach (var file in Directory.EnumerateFiles(Path.GetDirectoryName(_segmentPathSource), Path.GetFileName(_segmentPathSource) + "*"))
|
||||
{
|
||||
File.Delete(file);
|
||||
}
|
||||
}
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_ToOGV()
|
||||
{
|
||||
|
|
@ -467,7 +480,7 @@ namespace FFMpegCore.Test
|
|||
}
|
||||
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_Snapshot_PersistSnapshot()
|
||||
public void Video_Snapshot_Png_PersistSnapshot()
|
||||
{
|
||||
using var outputPath = new TemporaryFile("out.png");
|
||||
var input = FFProbe.Analyse(TestResources.Mp4Video);
|
||||
|
|
@ -480,6 +493,63 @@ namespace FFMpegCore.Test
|
|||
Assert.AreEqual("png", analysis.PrimaryVideoStream!.CodecName);
|
||||
}
|
||||
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_Snapshot_Jpg_PersistSnapshot()
|
||||
{
|
||||
using var outputPath = new TemporaryFile("out.jpg");
|
||||
var input = FFProbe.Analyse(TestResources.Mp4Video);
|
||||
|
||||
FFMpeg.Snapshot(TestResources.Mp4Video, outputPath);
|
||||
|
||||
var analysis = FFProbe.Analyse(outputPath);
|
||||
Assert.AreEqual(input.PrimaryVideoStream!.Width, analysis.PrimaryVideoStream!.Width);
|
||||
Assert.AreEqual(input.PrimaryVideoStream.Height, analysis.PrimaryVideoStream!.Height);
|
||||
Assert.AreEqual("mjpeg", analysis.PrimaryVideoStream!.CodecName);
|
||||
}
|
||||
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_Snapshot_Bmp_PersistSnapshot()
|
||||
{
|
||||
using var outputPath = new TemporaryFile("out.bmp");
|
||||
var input = FFProbe.Analyse(TestResources.Mp4Video);
|
||||
|
||||
FFMpeg.Snapshot(TestResources.Mp4Video, outputPath);
|
||||
|
||||
var analysis = FFProbe.Analyse(outputPath);
|
||||
Assert.AreEqual(input.PrimaryVideoStream!.Width, analysis.PrimaryVideoStream!.Width);
|
||||
Assert.AreEqual(input.PrimaryVideoStream.Height, analysis.PrimaryVideoStream!.Height);
|
||||
Assert.AreEqual("bmp", analysis.PrimaryVideoStream!.CodecName);
|
||||
}
|
||||
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_Snapshot_Webp_PersistSnapshot()
|
||||
{
|
||||
using var outputPath = new TemporaryFile("out.webp");
|
||||
var input = FFProbe.Analyse(TestResources.Mp4Video);
|
||||
|
||||
FFMpeg.Snapshot(TestResources.Mp4Video, outputPath);
|
||||
|
||||
var analysis = FFProbe.Analyse(outputPath);
|
||||
Assert.AreEqual(input.PrimaryVideoStream!.Width, analysis.PrimaryVideoStream!.Width);
|
||||
Assert.AreEqual(input.PrimaryVideoStream.Height, analysis.PrimaryVideoStream!.Height);
|
||||
Assert.AreEqual("webp", analysis.PrimaryVideoStream!.CodecName);
|
||||
}
|
||||
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_Snapshot_Exception_PersistSnapshot()
|
||||
{
|
||||
using var outputPath = new TemporaryFile("out.asd");
|
||||
|
||||
try
|
||||
{
|
||||
FFMpeg.Snapshot(TestResources.Mp4Video, outputPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Assert.IsTrue(ex is ArgumentException);
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_Snapshot_Rotated_PersistSnapshot()
|
||||
{
|
||||
|
|
@ -910,5 +980,95 @@ namespace FFMpegCore.Test
|
|||
Assert.AreEqual("h264", outputInfo.PrimaryVideoStream.CodecName);
|
||||
Assert.AreEqual("aac", outputInfo.PrimaryAudioStream!.CodecName);
|
||||
}
|
||||
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_Segmented_File_Output()
|
||||
{
|
||||
using var input = File.OpenRead(TestResources.WebmVideo);
|
||||
var success = FFMpegArguments
|
||||
.FromPipeInput(new StreamPipeSource(input))
|
||||
.OutPutToSegmentedFiles(
|
||||
new SegmentArgument($"{_segmentPathSource}%Y-%m-%d_%H-%M-%S.mkv", true, segmentOptions => segmentOptions
|
||||
.Strftime(true)
|
||||
.Wrap(-1)
|
||||
.Time(60)
|
||||
.ResetTimeStamps(true)),
|
||||
options => options
|
||||
.CopyChannel()
|
||||
.WithVideoCodec("h264")
|
||||
.ForceFormat("matroska")
|
||||
.WithConstantRateFactor(21)
|
||||
.WithVideoBitrate(3000)
|
||||
.WithFastStart()
|
||||
.WithVideoFilters(filterOptions => filterOptions
|
||||
.Scale(VideoSize.Hd)
|
||||
.DrawText(DrawTextOptions.Create(@"'%{localtime}.%{eif\:1M*t-1K*trunc(t*1K)\:d\:3}'", @"C:/Users/yan.gauthier/AppData/Local/Microsoft/Windows/Fonts/Roboto-Regular.ttf")
|
||||
.WithParameter("fontcolor", "yellow")
|
||||
.WithParameter("fontsize", "40")
|
||||
.WithParameter("x", "(w-text_w)")
|
||||
.WithParameter("y", "(h - text_h)")
|
||||
.WithParameter("rate", "19")
|
||||
)
|
||||
)
|
||||
)
|
||||
.CancellableThrough(new CancellationTokenSource().Token, 8000)
|
||||
.ProcessSynchronously(false);
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
|
||||
[TestMethod, Timeout(BaseTimeoutMilliseconds)]
|
||||
public void Video_MultiOutput_With_Segmented_File_Output()
|
||||
{
|
||||
using var input = File.OpenRead(TestResources.WebmVideo);
|
||||
var success = FFMpegArguments
|
||||
.FromPipeInput(new StreamPipeSource(input))
|
||||
.MultiOutput(args => args
|
||||
.OutputToFile($"{_segmentPathSource}2", true, options => options
|
||||
.CopyChannel()
|
||||
.WithVideoCodec("mjpeg")
|
||||
.ForceFormat("matroska")
|
||||
.WithConstantRateFactor(21)
|
||||
.WithVideoBitrate(4000)
|
||||
.WithFastStart()
|
||||
.WithVideoFilters(filterOptions => filterOptions
|
||||
.Scale(VideoSize.Hd)
|
||||
.DrawText(DrawTextOptions.Create(@"'%{localtime}.%{eif\:1M*t-1K*trunc(t*1K)\:d\:3}'", @"C:/Users/yan.gauthier/AppData/Local/Microsoft/Windows/Fonts/Roboto-Regular.ttf")
|
||||
.WithParameter("fontcolor", "yellow")
|
||||
.WithParameter("fontsize", "40")
|
||||
.WithParameter("x", "(w-text_w)")
|
||||
.WithParameter("y", "(h - text_h)")
|
||||
.WithParameter("rate", "19")
|
||||
)
|
||||
)
|
||||
)
|
||||
.OutPutToSegmentedFiles(
|
||||
new SegmentArgument($"{_segmentPathSource}%Y-%m-%d_%H-%M-%S.mkv", true, segmentOptions => segmentOptions
|
||||
.Strftime(true)
|
||||
.Wrap(-1)
|
||||
.Time(60)
|
||||
.ResetTimeStamps(true)),
|
||||
options => options
|
||||
.CopyChannel()
|
||||
.WithVideoCodec("h264")
|
||||
.ForceFormat("matroska")
|
||||
.WithConstantRateFactor(21)
|
||||
.WithVideoBitrate(3000)
|
||||
.WithFastStart()
|
||||
.WithVideoFilters(filterOptions => filterOptions
|
||||
.Scale(VideoSize.Hd)
|
||||
.DrawText(DrawTextOptions.Create(@"'%{localtime}.%{eif\:1M*t-1K*trunc(t*1K)\:d\:3}'", @"C:/Users/yan.gauthier/AppData/Local/Microsoft/Windows/Fonts/Roboto-Regular.ttf")
|
||||
.WithParameter("fontcolor", "yellow")
|
||||
.WithParameter("fontsize", "40")
|
||||
.WithParameter("x", "(w-text_w)")
|
||||
.WithParameter("y", "(h - text_h)")
|
||||
.WithParameter("rate", "19")
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
.CancellableThrough(new CancellationTokenSource().Token, 8000)
|
||||
.ProcessSynchronously(false);
|
||||
Assert.IsTrue(success);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,6 @@
|
|||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
//public string GetText(StringBuilder context);
|
||||
public string GetText(IEnumerable<IArgument> context);
|
||||
string GetText(IEnumerable<IArgument> context);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
88
FFMpegCore/FFMpeg/Arguments/OutputSegmentArgument.cs
Normal file
88
FFMpegCore/FFMpeg/Arguments/OutputSegmentArgument.cs
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
using FFMpegCore.Exceptions;
|
||||
|
||||
namespace FFMpegCore.Arguments
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents output parameter
|
||||
/// </summary>
|
||||
public class OutputSegmentArgument : IOutputArgument
|
||||
{
|
||||
public readonly string SegmentPattern;
|
||||
public readonly bool Overwrite;
|
||||
public readonly SegmentArgumentOptions Options;
|
||||
public OutputSegmentArgument(SegmentArgument segmentArgument)
|
||||
{
|
||||
SegmentPattern = segmentArgument.SegmentPattern;
|
||||
Overwrite = segmentArgument.Overwrite;
|
||||
var segmentArgumentobj = new SegmentArgumentOptions();
|
||||
segmentArgument.Options?.Invoke(segmentArgumentobj);
|
||||
Options = segmentArgumentobj;
|
||||
}
|
||||
|
||||
public void Pre()
|
||||
{
|
||||
if (int.TryParse(Options.Arguments.FirstOrDefault(x => x.Key == "segment_time").Value, out var result) && result < 1)
|
||||
{
|
||||
throw new FFMpegException(FFMpegExceptionType.Process, "Parameter SegmentTime cannot be negative or equal to zero");
|
||||
}
|
||||
|
||||
if (Options.Arguments.FirstOrDefault(x => x.Key == "segment_time").Value == "0")
|
||||
{
|
||||
throw new FFMpegException(FFMpegExceptionType.Process, "Parameter SegmentWrap cannot equal to zero");
|
||||
}
|
||||
}
|
||||
public Task During(CancellationToken cancellationToken = default) => Task.CompletedTask;
|
||||
public void Post()
|
||||
{
|
||||
}
|
||||
|
||||
public string Text => GetText();
|
||||
private string GetText()
|
||||
{
|
||||
var arguments = Options.Arguments
|
||||
.Where(arg => !string.IsNullOrWhiteSpace(arg.Value) && !string.IsNullOrWhiteSpace(arg.Key))
|
||||
.Select(arg =>
|
||||
{
|
||||
return arg.Value;
|
||||
});
|
||||
|
||||
return $"-f segment {string.Join(" ", arguments)} \"{SegmentPattern}\"{(Overwrite ? " -y" : string.Empty)}";
|
||||
}
|
||||
}
|
||||
|
||||
public interface ISegmentArgument
|
||||
{
|
||||
public string Key { get; }
|
||||
public string Value { get; }
|
||||
}
|
||||
|
||||
public class SegmentArgumentOptions
|
||||
{
|
||||
public List<ISegmentArgument> Arguments { get; } = new();
|
||||
|
||||
public SegmentArgumentOptions ResetTimeStamps(bool resetTimestamps = true) => WithArgument(new SegmentResetTimeStampsArgument(resetTimestamps));
|
||||
public SegmentArgumentOptions Strftime(bool enable = false) => WithArgument(new SegmentStrftimeArgument(enable));
|
||||
public SegmentArgumentOptions Time(int time = 60) => WithArgument(new SegmentTimeArgument(time));
|
||||
public SegmentArgumentOptions Wrap(int limit = -1) => WithArgument(new SegmentWrapArgument(limit));
|
||||
public SegmentArgumentOptions WithCustomArgument(string argument) => WithArgument(new SegmentCustomArgument(argument));
|
||||
private SegmentArgumentOptions WithArgument(ISegmentArgument argument)
|
||||
{
|
||||
Arguments.Add(argument);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public class SegmentArgument
|
||||
{
|
||||
public readonly string SegmentPattern;
|
||||
public readonly bool Overwrite;
|
||||
public readonly Action<SegmentArgumentOptions> Options;
|
||||
|
||||
public SegmentArgument(string segmentPattern, bool overwrite, Action<SegmentArgumentOptions> options)
|
||||
{
|
||||
SegmentPattern = segmentPattern;
|
||||
Overwrite = overwrite;
|
||||
Options = options;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
FFMpegCore/FFMpeg/Arguments/SegmentCustomArgument.cs
Normal file
14
FFMpegCore/FFMpeg/Arguments/SegmentCustomArgument.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace FFMpegCore.Arguments
|
||||
{
|
||||
public class SegmentCustomArgument : ISegmentArgument
|
||||
{
|
||||
public readonly string Argument;
|
||||
|
||||
public SegmentCustomArgument(string argument)
|
||||
{
|
||||
Argument = argument;
|
||||
}
|
||||
public string Key => "custom";
|
||||
public string Value => Argument ?? string.Empty;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
namespace FFMpegCore.Arguments
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents reset_timestamps parameter
|
||||
/// </summary>
|
||||
public class SegmentResetTimeStampsArgument : ISegmentArgument
|
||||
{
|
||||
public readonly bool ResetTimestamps;
|
||||
/// <summary>
|
||||
/// Represents reset_timestamps parameter
|
||||
/// </summary>
|
||||
/// <param name="resetTimestamps">true if files timestamps are to be reset</param>
|
||||
public SegmentResetTimeStampsArgument(bool resetTimestamps)
|
||||
{
|
||||
ResetTimestamps = resetTimestamps;
|
||||
}
|
||||
public string Key { get; } = "reset_timestamps";
|
||||
public string Value => ResetTimestamps ? $"-reset_timestamps 1" : string.Empty;
|
||||
}
|
||||
}
|
||||
20
FFMpegCore/FFMpeg/Arguments/SegmentStrftimeArgument.cs
Normal file
20
FFMpegCore/FFMpeg/Arguments/SegmentStrftimeArgument.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
namespace FFMpegCore.Arguments
|
||||
{
|
||||
/// <summary>
|
||||
/// Use the strftime function to define the name of the new segments to write. If this is selected, the output segment name must contain a strftime function template. Default value is 0.
|
||||
/// </summary>
|
||||
public class SegmentStrftimeArgument : ISegmentArgument
|
||||
{
|
||||
public readonly bool Enable;
|
||||
/// <summary>
|
||||
/// Use the strftime function to define the name of the new segments to write. If this is selected, the output segment name must contain a strftime function template. Default value is 0.
|
||||
/// </summary>
|
||||
/// <param name="enable">true to enable strftime</param>
|
||||
public SegmentStrftimeArgument(bool enable)
|
||||
{
|
||||
Enable = enable;
|
||||
}
|
||||
public string Key { get; } = "strftime";
|
||||
public string Value => Enable ? $"-strftime 1" : string.Empty;
|
||||
}
|
||||
}
|
||||
20
FFMpegCore/FFMpeg/Arguments/SegmentTimeArgument.cs
Normal file
20
FFMpegCore/FFMpeg/Arguments/SegmentTimeArgument.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
namespace FFMpegCore.Arguments
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents segment_time parameter
|
||||
/// </summary>
|
||||
public class SegmentTimeArgument : ISegmentArgument
|
||||
{
|
||||
public readonly int Time;
|
||||
/// <summary>
|
||||
/// Represents segment_time parameter
|
||||
/// </summary>
|
||||
/// <param name="time">time in seconds of the segment</param>
|
||||
public SegmentTimeArgument(int time)
|
||||
{
|
||||
Time = time;
|
||||
}
|
||||
public string Key { get; } = "segment_time";
|
||||
public string Value => Time <= 0 ? string.Empty : $"-segment_time {Time}";
|
||||
}
|
||||
}
|
||||
20
FFMpegCore/FFMpeg/Arguments/SegmentWrapArgument.cs
Normal file
20
FFMpegCore/FFMpeg/Arguments/SegmentWrapArgument.cs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
namespace FFMpegCore.Arguments
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents segment_wrap parameter
|
||||
/// </summary>
|
||||
public class SegmentWrapArgument : ISegmentArgument
|
||||
{
|
||||
public readonly int Limit;
|
||||
/// <summary>
|
||||
/// Represents segment_wrap parameter
|
||||
/// </summary>
|
||||
/// <param name="limit">limit value after which segment index will wrap around</param>
|
||||
public SegmentWrapArgument(int limit)
|
||||
{
|
||||
Limit = limit;
|
||||
}
|
||||
public string Key { get; } = "segment_wrap";
|
||||
public string Value => Limit <= 0 ? string.Empty : $"-segment_wrap {Limit}";
|
||||
}
|
||||
}
|
||||
|
|
@ -36,8 +36,8 @@ namespace FFMpegCore.Arguments
|
|||
|
||||
public interface IVideoFilterArgument
|
||||
{
|
||||
public string Key { get; }
|
||||
public string Value { get; }
|
||||
string Key { get; }
|
||||
string Value { get; }
|
||||
}
|
||||
|
||||
public class VideoFilterOptions
|
||||
|
|
|
|||
|
|
@ -15,9 +15,33 @@
|
|||
public static Codec LibX265 => FFMpeg.GetCodec("libx265");
|
||||
public static Codec LibVpx => FFMpeg.GetCodec("libvpx");
|
||||
public static Codec LibTheora => FFMpeg.GetCodec("libtheora");
|
||||
public static Codec Png => FFMpeg.GetCodec("png");
|
||||
public static Codec MpegTs => FFMpeg.GetCodec("mpegts");
|
||||
public static Codec LibaomAv1 => FFMpeg.GetCodec("libaom-av1");
|
||||
|
||||
public static class Image
|
||||
{
|
||||
public static Codec Png => FFMpeg.GetCodec("png");
|
||||
public static Codec Jpg => FFMpeg.GetCodec("mjpeg");
|
||||
public static Codec Bmp => FFMpeg.GetCodec("bmp");
|
||||
public static Codec Webp => FFMpeg.GetCodec("webp");
|
||||
|
||||
public static Codec GetByExtension(string path)
|
||||
{
|
||||
var ext = Path.GetExtension(path);
|
||||
switch (ext)
|
||||
{
|
||||
case FileExtension.Image.Png:
|
||||
return Png;
|
||||
case FileExtension.Image.Jpg:
|
||||
return Jpg;
|
||||
case FileExtension.Image.Bmp:
|
||||
return Bmp;
|
||||
case FileExtension.Image.Webp:
|
||||
return Webp;
|
||||
default: throw new NotSupportedException($"Unsupported image extension: {ext}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class AudioCodec
|
||||
|
|
|
|||
|
|
@ -10,7 +10,10 @@
|
|||
"libxvpx" => WebM,
|
||||
"libxtheora" => Ogv,
|
||||
"mpegts" => Ts,
|
||||
"png" => Png,
|
||||
"png" => Image.Png,
|
||||
"jpg" => Image.Jpg,
|
||||
"bmp" => Image.Bmp,
|
||||
"webp" => Image.Webp,
|
||||
_ => throw new Exception("The extension for this video type is not defined.")
|
||||
};
|
||||
}
|
||||
|
|
@ -18,8 +21,16 @@
|
|||
public static readonly string Ts = VideoType.MpegTs.Extension;
|
||||
public static readonly string Ogv = VideoType.Ogv.Extension;
|
||||
public static readonly string WebM = VideoType.WebM.Extension;
|
||||
public static readonly string Png = ".png";
|
||||
public static readonly string Mp3 = ".mp3";
|
||||
public static readonly string Gif = ".gif";
|
||||
|
||||
public static class Image
|
||||
{
|
||||
public const string Png = ".png";
|
||||
public const string Jpg = ".jpg";
|
||||
public const string Bmp = ".bmp";
|
||||
public const string Webp = ".webp";
|
||||
public static readonly List<string> All = [Png, Jpg, Bmp, Webp];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,16 +20,11 @@ namespace FFMpegCore
|
|||
/// <returns>Bitmap with the requested snapshot.</returns>
|
||||
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.Combine(Path.GetDirectoryName(output), Path.GetFileNameWithoutExtension(output) + FileExtension.Png);
|
||||
}
|
||||
CheckSnapshotOutputExtension(output, FileExtension.Image.All);
|
||||
|
||||
var source = FFProbe.Analyse(input);
|
||||
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
|
||||
|
||||
return arguments
|
||||
.OutputToFile(output, true, outputOptions)
|
||||
return SnapshotProcess(input, output, source, size, captureTime, streamIndex, inputFileIndex)
|
||||
.ProcessSynchronously();
|
||||
}
|
||||
/// <summary>
|
||||
|
|
@ -44,47 +39,55 @@ namespace FFMpegCore
|
|||
/// <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 = null, int inputFileIndex = 0)
|
||||
{
|
||||
if (Path.GetExtension(output) != FileExtension.Png)
|
||||
{
|
||||
output = Path.Combine(Path.GetDirectoryName(output), Path.GetFileNameWithoutExtension(output) + FileExtension.Png);
|
||||
}
|
||||
CheckSnapshotOutputExtension(output, FileExtension.Image.All);
|
||||
|
||||
var source = await FFProbe.AnalyseAsync(input).ConfigureAwait(false);
|
||||
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildSnapshotArguments(input, source, size, captureTime, streamIndex, inputFileIndex);
|
||||
|
||||
return await arguments
|
||||
.OutputToFile(output, true, outputOptions)
|
||||
return await SnapshotProcess(input, output, source, size, captureTime, streamIndex, inputFileIndex)
|
||||
.ProcessAsynchronously();
|
||||
}
|
||||
|
||||
public static bool GifSnapshot(string input, string output, Size? size = null, TimeSpan? captureTime = null, TimeSpan? duration = null, int? streamIndex = null)
|
||||
{
|
||||
if (Path.GetExtension(output)?.ToLower() != FileExtension.Gif)
|
||||
{
|
||||
output = Path.Combine(Path.GetDirectoryName(output), Path.GetFileNameWithoutExtension(output) + FileExtension.Gif);
|
||||
}
|
||||
CheckSnapshotOutputExtension(output, [FileExtension.Gif]);
|
||||
|
||||
var source = FFProbe.Analyse(input);
|
||||
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildGifSnapshotArguments(input, source, size, captureTime, duration, streamIndex);
|
||||
|
||||
return arguments
|
||||
.OutputToFile(output, true, outputOptions)
|
||||
return GifSnapshotProcess(input, output, source, size, captureTime, duration, streamIndex)
|
||||
.ProcessSynchronously();
|
||||
}
|
||||
|
||||
public static async Task<bool> GifSnapshotAsync(string input, string output, Size? size = null, TimeSpan? captureTime = null, TimeSpan? duration = null, int? streamIndex = null)
|
||||
{
|
||||
if (Path.GetExtension(output)?.ToLower() != FileExtension.Gif)
|
||||
{
|
||||
output = Path.Combine(Path.GetDirectoryName(output), Path.GetFileNameWithoutExtension(output) + FileExtension.Gif);
|
||||
}
|
||||
CheckSnapshotOutputExtension(output, [FileExtension.Gif]);
|
||||
|
||||
var source = await FFProbe.AnalyseAsync(input).ConfigureAwait(false);
|
||||
|
||||
return await GifSnapshotProcess(input, output, source, size, captureTime, duration, streamIndex)
|
||||
.ProcessAsynchronously();
|
||||
}
|
||||
|
||||
private static FFMpegArgumentProcessor SnapshotProcess(string input, string output, IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, int? streamIndex = null, int inputFileIndex = 0)
|
||||
{
|
||||
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildSnapshotArguments(input, output, source, size, captureTime, streamIndex, inputFileIndex);
|
||||
|
||||
return arguments.OutputToFile(output, true, outputOptions);
|
||||
}
|
||||
|
||||
private static FFMpegArgumentProcessor GifSnapshotProcess(string input, string output, IMediaAnalysis source, Size? size = null, TimeSpan? captureTime = null, TimeSpan? duration = null, int? streamIndex = null)
|
||||
{
|
||||
var (arguments, outputOptions) = SnapshotArgumentBuilder.BuildGifSnapshotArguments(input, source, size, captureTime, duration, streamIndex);
|
||||
|
||||
return await arguments
|
||||
.OutputToFile(output, true, outputOptions)
|
||||
.ProcessAsynchronously();
|
||||
return arguments.OutputToFile(output, true, outputOptions);
|
||||
}
|
||||
|
||||
private static void CheckSnapshotOutputExtension(string output, List<string> extensions)
|
||||
{
|
||||
if (!extensions.Contains(Path.GetExtension(output).ToLower()))
|
||||
{
|
||||
throw new ArgumentException(
|
||||
$"Invalid snapshot output extension: {output}, needed: {string.Join(",", FileExtension.Image.All)}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ namespace FFMpegCore
|
|||
public FFMpegArgumentProcessor OutputToUrl(string uri, Action<FFMpegArgumentOptions>? addArguments = null) => ToProcessor(new OutputUrlArgument(uri), addArguments);
|
||||
public FFMpegArgumentProcessor OutputToUrl(Uri uri, Action<FFMpegArgumentOptions>? addArguments = null) => ToProcessor(new OutputUrlArgument(uri.ToString()), addArguments);
|
||||
public FFMpegArgumentProcessor OutputToPipe(IPipeSink reader, Action<FFMpegArgumentOptions>? addArguments = null) => ToProcessor(new OutputPipeArgument(reader), addArguments);
|
||||
|
||||
public FFMpegArgumentProcessor OutPutToSegmentedFiles(SegmentArgument segmentArgument, Action<FFMpegArgumentOptions>? addArguments = null) => ToProcessor(new OutputSegmentArgument(segmentArgument), addArguments);
|
||||
private FFMpegArgumentProcessor ToProcessor(IOutputArgument argument, Action<FFMpegArgumentOptions>? addArguments)
|
||||
{
|
||||
var args = new FFMpegArgumentOptions();
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ namespace FFMpegCore
|
|||
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 OutPutToSegmentedFiles(SegmentArgument segmentArgument, Action<FFMpegArgumentOptions>? addArguments = null) => AddOutput(new OutputSegmentArgument(segmentArgument), addArguments);
|
||||
public FFMpegMultiOutputOptions AddOutput(IOutputArgument argument, Action<FFMpegArgumentOptions>? addArguments)
|
||||
{
|
||||
var args = new FFMpegArgumentOptions();
|
||||
|
|
|
|||
|
|
@ -9,6 +9,30 @@ public static class SnapshotArgumentBuilder
|
|||
{
|
||||
public static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildSnapshotArguments(
|
||||
string input,
|
||||
string output,
|
||||
IMediaAnalysis source,
|
||||
Size? size = null,
|
||||
TimeSpan? captureTime = null,
|
||||
int? streamIndex = null,
|
||||
int inputFileIndex = 0)
|
||||
{
|
||||
return BuildSnapshotArguments(input, VideoCodec.Image.GetByExtension(output), source, size, captureTime, streamIndex, inputFileIndex);
|
||||
}
|
||||
|
||||
public static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildSnapshotArguments(
|
||||
string input,
|
||||
IMediaAnalysis source,
|
||||
Size? size = null,
|
||||
TimeSpan? captureTime = null,
|
||||
int? streamIndex = null,
|
||||
int inputFileIndex = 0)
|
||||
{
|
||||
return BuildSnapshotArguments(input, VideoCodec.Image.Png, source, size, captureTime, streamIndex, inputFileIndex);
|
||||
}
|
||||
|
||||
private static (FFMpegArguments, Action<FFMpegArgumentOptions> outputOptions) BuildSnapshotArguments(
|
||||
string input,
|
||||
Codec codec,
|
||||
IMediaAnalysis source,
|
||||
Size? size = null,
|
||||
TimeSpan? captureTime = null,
|
||||
|
|
@ -26,7 +50,7 @@ public static class SnapshotArgumentBuilder
|
|||
.Seek(captureTime)),
|
||||
options => options
|
||||
.SelectStream((int)streamIndex, inputFileIndex)
|
||||
.WithVideoCodec(VideoCodec.Png)
|
||||
.WithVideoCodec(codec)
|
||||
.WithFrameOutputCount(1)
|
||||
.Resize(size));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ namespace FFMpegCore
|
|||
/// </summary>
|
||||
public string TemporaryFilesFolder { get; set; } = Path.GetTempPath();
|
||||
|
||||
[JsonIgnore]
|
||||
/// <summary>
|
||||
/// Encoding web name used to persist encoding <see cref="Encoding"/>
|
||||
/// </summary>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue