Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

!F Adding gifski gif converter #483

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
619 changes: 619 additions & 0 deletions licenses/Gifski.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/Captura.Core/Captura.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ItemGroup>
<ProjectReference Include="..\Captura.Base\Captura.Base.csproj" />
<ProjectReference Include="..\Captura.FFmpeg\Captura.FFmpeg.csproj" />
<ProjectReference Include="..\Captura.Gifski\Captura.Gifski.csproj" />
<ProjectReference Include="..\Captura.Hotkeys\Captura.Hotkeys.csproj" />
<ProjectReference Include="..\Captura.Imgur\Captura.Imgur.csproj" />
<ProjectReference Include="..\Captura.Loc\Captura.Loc.csproj" />
Expand Down
2 changes: 2 additions & 0 deletions src/Captura.Core/CoreModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public void OnLoad(IBinder Binder)

FFmpegModule.Load(Binder);

GifskiModule.Load(Binder);

BindViewModels(Binder);
BindSettings(Binder);
BindImageWriters(Binder);
Expand Down
2 changes: 1 addition & 1 deletion src/Captura.FFmpeg/Video/FFmpegVideoConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task StartAsync(VideoConverterArgs Args, IProgress<int> Progress)
_videoCodec.AudioArgsProvider(Args.AudioQuality, output);
}

var process = FFmpegService.StartFFmpeg(argsBuilder.GetArgs(), Args.FileName, out var log);
using var process = FFmpegService.StartFFmpeg(argsBuilder.GetArgs(), Args.FileName, out var log);

log.ProgressChanged += Progress.Report;

Expand Down
25 changes: 25 additions & 0 deletions src/Captura.Gifski/ArgsBuilder/GifskiArgsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Captura.Models
{
class GifskiArgsBuilder
{
string _outputFile;
string _inputFile;

public GifskiArgsBuilder AddOutputFile(string FileName)
{
_outputFile = FileName;
return this;
}

public GifskiArgsBuilder AddInputFile(string FileName)
{
_inputFile = FileName;
return this;
}

public string GetArgs()
{
return $"-o {_outputFile} {_inputFile}";
}
}
}
18 changes: 18 additions & 0 deletions src/Captura.Gifski/Captura.Gifski.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="ArgsBuilder - Copy\**" />
<EmbeddedResource Remove="ArgsBuilder - Copy\**" />
<None Remove="ArgsBuilder - Copy\**" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Captura.Base\Captura.Base.csproj" />
<ProjectReference Include="..\Captura.FFmpeg\Captura.FFmpeg.csproj" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions src/Captura.Gifski/GifskiException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Captura.Gifski
{
public class GifskiException : Exception
{
public GifskiException(int ExitCode, Exception InnerException = null)
: base($"An Error Occurred with Gifski, Exit Code: {ExitCode}", InnerException) { }
}
}
10 changes: 10 additions & 0 deletions src/Captura.Gifski/GifskiModule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Captura.Models
{
public static class GifskiModule
{
public static void Load(IBinder Binder)
{
Binder.Bind<IVideoConverter>(() => new GifskiVideoConverter());
}
}
}
54 changes: 54 additions & 0 deletions src/Captura.Gifski/GifskiService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;

namespace Captura
{
public static class GifskiService
{
const string ExeName = "gifski.exe";

public static string ExePath => @"C:\Users\luis9\Downloads\gifski-0.9.1\win\gifski.exe";

public static Process StartGifski(string Arguments, Action<int> progress)
{
var process = new Process
{
StartInfo =
{
FileName = ExePath,
Arguments = Arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
},
EnableRaisingEvents = true
};

//Frame 2 / 90 #_............................................................ 2m
var progressRegex = new Regex(@"Frame ([0-9]+) \/ ([0-9]+)", RegexOptions.Compiled);

process.OutputDataReceived += (s, e) =>
{
if (string.IsNullOrEmpty(e.Data))
{
return;
}

var match = progressRegex.Match(e.Data);
if (match.Success && match.Groups.Count == 3)
{
var current = match.Groups[1].Value;
var total = match.Groups[2].Value;
var normalized = int.Parse(current) / (float) int.Parse(total);
progress((int) (normalized * 100));
}
};

process.Start();
process.BeginOutputReadLine();

return process;
}
}
}
54 changes: 54 additions & 0 deletions src/Captura.Gifski/GifskiVideoConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Captura.FFmpeg;
using Captura.Gifski;

namespace Captura.Models
{
class GifskiVideoConverter : IVideoConverter
{
public string Name => "Gif (Gifski)";

public string Extension => ".gif";

public async Task StartAsync(VideoConverterArgs Args, IProgress<int> Progress)
{
if (!FFmpegService.FFmpegExists)
{
throw new FFmpegNotFoundException();
}

var ffmpegArgs = new FFmpegArgsBuilder();
ffmpegArgs.AddInputFile(Args.InputFile);

var tempFolder = Path.Combine(Path.GetTempPath(), "Captura");
Directory.CreateDirectory(tempFolder);
var frameFormat = Path.Combine(tempFolder, "frame%04d.png");
ffmpegArgs.AddOutputFile(frameFormat);

using var ffmpeg = FFmpegService.StartFFmpeg(ffmpegArgs.GetArgs(), Args.FileName, out var log);
log.ProgressChanged += p => Progress.Report(p / 2);

await Task.Run(() => ffmpeg.WaitForExit());

if (ffmpeg.ExitCode != 0)
throw new FFmpegException(ffmpeg.ExitCode);

var gifskiArgs = new GifskiArgsBuilder();
gifskiArgs
.AddInputFile(Path.Combine(tempFolder, "frame*.png"))
.AddOutputFile(Args.FileName);

using var gifski = GifskiService.StartGifski(
gifskiArgs.GetArgs(), p => Progress.Report(p / 2 + 50));

await Task.Run(() => gifski.WaitForExit());

if (gifski.ExitCode != 0)
throw new GifskiException(gifski.ExitCode);

Directory.Delete(tempFolder, true);
}
}
}
8 changes: 7 additions & 1 deletion src/Captura.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28729.10
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Captura", "Captura\Captura.csproj", "{0B496E01-F328-4969-832C-C75977B4F4E6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Captura", "Captura\Captura.csproj", "{0B496E01-F328-4969-832C-C75977B4F4E6}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Screna", "Screna\Screna.csproj", "{23CB9ADB-BA9F-4618-BD0C-589A00A532E5}"
EndProject
Expand Down Expand Up @@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Captura.Audio", "Captura.Au
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Audio", "Audio", "{E645F466-C7D6-469A-B4CC-B0917703318C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Captura.Gifski", "Captura.Gifski\Captura.Gifski.csproj", "{A0800A73-EF97-4FB4-BC1B-CF119857E900}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -119,6 +121,10 @@ Global
{D29DE716-EDDF-4CFB-8430-D07CCACB0921}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D29DE716-EDDF-4CFB-8430-D07CCACB0921}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D29DE716-EDDF-4CFB-8430-D07CCACB0921}.Release|Any CPU.Build.0 = Release|Any CPU
{A0800A73-EF97-4FB4-BC1B-CF119857E900}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0800A73-EF97-4FB4-BC1B-CF119857E900}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0800A73-EF97-4FB4-BC1B-CF119857E900}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0800A73-EF97-4FB4-BC1B-CF119857E900}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down