Compare commits

...

25 commits

Author SHA1 Message Date
Malte Rosenbjerg
35c21b0813
Merge 1f164472c7 into 53b3e0fbd0 2025-10-16 10:09:51 +02:00
Malte Rosenbjerg
1f164472c7
Merge branch 'main' into bugfix/ensure-ffmpeg-not-found-throws-ffmpegexception 2025-10-16 10:09:50 +02:00
Malte Rosenbjerg
53b3e0fbd0
Merge pull request #566 from GorobVictor/snapshot-formats
FEAT: added more extensions for snapshot(jpg, bmp, webp)
2025-10-16 10:08:25 +02:00
Malte Rosenbjerg
bfefa9560e
Merge branch 'main' into snapshot-formats 2025-10-16 10:00:56 +02:00
Malte Rosenbjerg
37b4119e13
Merge pull request #576 from rosenbjerg/migrate-to-macos-latest-test-runner-instead-of-macos-13
CI: Install ffmpeg using brew if running on arm64 macos
2025-10-16 09:59:48 +02:00
Malte Rosenbjerg
7e135a78d3 Use install step for installing ffmpeg 2025-10-16 09:55:50 +02:00
Malte Rosenbjerg
91c1629215 Add name labels to ffmpeg install steps 2025-10-16 09:53:09 +02:00
Malte Rosenbjerg
7036ad6df2 Switch on matrix.os instead of runner.os 2025-10-16 09:47:53 +02:00
Malte Rosenbjerg
5ba8122d00 Fix specified ffmpeg version 2025-10-16 09:41:34 +02:00
Malte Rosenbjerg
665c9f9213 Use full reference 2025-10-16 09:39:51 +02:00
Malte Rosenbjerg
fb10b78e35 Include github token to avoid rate limiting 2025-10-16 09:37:42 +02:00
Malte Rosenbjerg
99181e9f65 Include github token to avoid rate limiting 2025-10-16 09:37:27 +02:00
Malte Rosenbjerg
af6587d8fe Reference workflow action by reference 2025-10-16 09:37:04 +02:00
Malte Rosenbjerg
9c636d4059 Migrate to AnimMouse/setup-ffmpeg@v1 2025-10-16 09:32:23 +02:00
Malte Rosenbjerg
5d8d346598 Only lint on ubuntu-latest to not waste time 2025-10-16 09:32:11 +02:00
Malte Rosenbjerg
918ca9a9ab Remove accessibility modifiers on interface properties (IDE0040) 2025-10-16 09:22:09 +02:00
Malte Rosenbjerg
abb9f15eeb Use brew if running on arm64 macos 2025-10-16 09:19:05 +02:00
Malte Rosenbjerg
193ed43f1d
Merge branch 'main' into snapshot-formats 2025-09-09 08:23:41 +02:00
Victor Horobchuk
3d21599c5d FIX: for dotnet format 2025-04-17 13:17:52 +03:00
Victor Horobchuk
4025b82fbf FIX: small moments 2025-04-17 13:02:27 +03:00
Victor Horobchuk
aa1051b268 FEAT: added more extensions for snapshot(jpg, bmp, webp) 2025-04-17 12:29:06 +03:00
Malte Rosenbjerg
f31dc2b1cd
Merge branch 'main' into bugfix/ensure-ffmpeg-not-found-throws-ffmpegexception 2024-12-04 20:58:27 +01:00
Malte Rosenbjerg
d43ea98e0e
Merge branch 'main' into bugfix/ensure-ffmpeg-not-found-throws-ffmpegexception 2024-12-04 20:55:04 +01:00
Malte Rosenbjerg
508cce8827 Change test path to non-existing directory 2023-02-23 22:00:19 +01:00
Malte Rosenbjerg
349b6044d1 Wrap Instances exception for expected behaviour 2023-02-23 19:18:34 +01:00
10 changed files with 182 additions and 44 deletions

View file

@ -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

View file

@ -1,6 +1,9 @@
using System.Reflection;
using FFMpegCore.Arguments;
using FFMpegCore.Exceptions;
using FFMpegCore.Helpers;
using FluentAssertions;
using Instances.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace FFMpegCore.Test
@ -99,5 +102,12 @@ namespace FFMpegCore.Test
var arg = new AudibleEncryptionKeyArgument("62689101");
arg.Text.Should().Be($"-activation_bytes 62689101");
}
[TestMethod]
public void Throws_FFMpegException_when_ffmpeg_not_found()
{
var exception = Assert.ThrowsException<FFMpegException>(() => FFMpegHelper.VerifyFFMpegExists(new FFOptions { BinaryFolder = "./folder/that/does/not/exist" }));
Assert.IsInstanceOfType<InstanceFileNotFoundException>(exception.InnerException);
}
}
}

View file

@ -467,7 +467,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 +480,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()
{

View file

@ -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);
}
}

View file

@ -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

View file

@ -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

View file

@ -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];
}
}
}

View file

@ -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>

View file

@ -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));
}

View file

@ -42,8 +42,16 @@ namespace FFMpegCore.Helpers
return;
}
var result = Instance.Finish(GlobalFFOptions.GetFFMpegBinaryPath(ffMpegOptions), "-version");
_ffmpegVerified = result.ExitCode == 0;
try
{
var result = Instance.Finish(GlobalFFOptions.GetFFMpegBinaryPath(ffMpegOptions), "-version");
_ffmpegVerified = result.ExitCode == 0;
}
catch (Exception e)
{
throw new FFMpegException(FFMpegExceptionType.Operation, "ffmpeg was not found on your system", e);
}
if (!_ffmpegVerified)
{
throw new FFMpegException(FFMpegExceptionType.Operation, "ffmpeg was not found on your system");