Skip to content

Commit 9958a41

Browse files
committed
adiciona convertor de videos para gif
1 parent cb795b7 commit 9958a41

4 files changed

Lines changed: 131 additions & 6 deletions

File tree

MainWindow.xaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,33 @@
5353
<TextBox x:Name="txtFahrenheit" Padding="5" TextChanged="txtFahrenheit_TextChanged" />
5454
</StackPanel>
5555
</TabItem>
56+
57+
<!-- Terceira Aba: Video para GIF -->
58+
<TabItem Header="Video para GIF">
59+
<StackPanel Margin="20">
60+
<TextBlock Text="Arquivo de Vídeo:" Margin="0,5,0,2" FontWeight="SemiBold"/>
61+
<Grid>
62+
<Grid.ColumnDefinitions>
63+
<ColumnDefinition Width="*" />
64+
<ColumnDefinition Width="Auto" />
65+
</Grid.ColumnDefinitions>
66+
<TextBox x:Name="txtVideoPath" Padding="5" />
67+
<Button Grid.Column="1" Content="..." Width="35" Margin="5,0,0,0" Click="ProcurarVideo_Click" ToolTip="Procurar vídeo" />
68+
</Grid>
69+
70+
<TextBlock Text="FPS:" Margin="0,15,0,2" FontWeight="SemiBold"/>
71+
<TextBox x:Name="txtFps" Padding="5" Text="15" />
72+
73+
<TextBlock Text="Scale / Resolução Máxima da largura (mantém proporção):" Margin="0,15,0,2" FontWeight="SemiBold"/>
74+
<TextBox x:Name="txtScale" Padding="5" Text="480" />
75+
76+
<Button Content="Converter para GIF" Click="ConverterParaGif_Click"
77+
Margin="0,20,0,10" Padding="8" Background="#007ACC"
78+
Foreground="White" BorderThickness="0" Cursor="Hand" />
79+
80+
<TextBlock x:Name="txtGifStatus" TextWrapping="Wrap" FontWeight="SemiBold" />
81+
</StackPanel>
82+
</TabItem>
5683
</TabControl>
5784
</Grid>
5885
</Window>

MainWindow.xaml.cs

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
using System.IO;
33
using System.Windows;
44
using System.Windows.Controls;
5-
using System.Windows.Data;
6-
using System.Windows.Documents;
7-
using System.Windows.Input;
85
using System.Windows.Media;
9-
using System.Windows.Media.Imaging;
10-
using System.Windows.Navigation;
11-
using System.Windows.Shapes;
126
using Microsoft.VisualBasic.FileIO;
7+
using Xabe.FFmpeg;
8+
using Xabe.FFmpeg.Downloader;
9+
using System.Threading.Tasks;
1310

1411
namespace WPF_utils;
1512

@@ -21,6 +18,14 @@ public partial class MainWindow : Window
2118
public MainWindow()
2219
{
2320
InitializeComponent();
21+
InitializeFFmpegAsync();
22+
}
23+
24+
private async void InitializeFFmpegAsync()
25+
{
26+
// Baixa os binários do FFmpeg caso não existam no diretório
27+
await FFmpegDownloader.GetLatestVersion(FFmpegVersion.Official);
28+
FFmpeg.SetExecutablesPath(".");
2429
}
2530

2631
// GERENCIAMENTO DE ARQUIVOS
@@ -168,4 +173,81 @@ private void LimparTemperaturas()
168173
txtFahrenheit.Text = "";
169174
isUpdatingTemp = false;
170175
}
176+
177+
// VÍDEO PARA GIF
178+
179+
private void ProcurarVideo_Click(object sender, RoutedEventArgs e)
180+
{
181+
var dialog = new Microsoft.Win32.OpenFileDialog
182+
{
183+
Title = "Selecione o arquivo de vídeo",
184+
Filter = "Video Files|*.mp4;*.avi;*.mkv;*.mov;*.wmv|All Files|*.*"
185+
};
186+
187+
if (dialog.ShowDialog() == true)
188+
{
189+
txtVideoPath.Text = dialog.FileName;
190+
}
191+
}
192+
193+
private async void ConverterParaGif_Click(object sender, RoutedEventArgs e)
194+
{
195+
string videoPath = txtVideoPath.Text;
196+
txtGifStatus.Text = string.Empty;
197+
198+
if (string.IsNullOrWhiteSpace(videoPath) || !File.Exists(videoPath))
199+
{
200+
txtGifStatus.Text = "Por favor, selecione um arquivo de vídeo válido.";
201+
txtGifStatus.Foreground = Brushes.Red;
202+
return;
203+
}
204+
205+
if (!int.TryParse(txtFps.Text, out int fps) || fps <= 0)
206+
{
207+
txtGifStatus.Text = "Por favor, insira um valor válido para FPS.";
208+
txtGifStatus.Foreground = Brushes.Red;
209+
return;
210+
}
211+
212+
if (!int.TryParse(txtScale.Text, out int scale) || scale <= 0)
213+
{
214+
txtGifStatus.Text = "Por favor, insira um valor válido para o Scale (largura).";
215+
txtGifStatus.Foreground = Brushes.Red;
216+
return;
217+
}
218+
219+
string outputPath = System.IO.Path.ChangeExtension(videoPath, ".gif");
220+
221+
try
222+
{
223+
txtGifStatus.Text = "Convertendo... Aguarde.";
224+
txtGifStatus.Foreground = Brushes.Orange;
225+
226+
// Se o arquivo GIF já existir, exclui-o antes de tentar converter (ou pede para sobrescrever dependendo dos parâmetros na conversão)
227+
if (File.Exists(outputPath))
228+
{
229+
File.Delete(outputPath);
230+
}
231+
232+
var mediaInfo = await FFmpeg.GetMediaInfo(videoPath);
233+
234+
// Define os argumentos personalizados para a conversão de GIF
235+
string customArgs = $"-vf \"fps={fps},scale={scale}:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse\"";
236+
237+
var conversion = FFmpeg.Conversions.New()
238+
.AddParameter($"-i \"{videoPath}\"")
239+
.AddParameter(customArgs)
240+
.SetOutput(outputPath);
241+
242+
await conversion.Start();
243+
244+
txtGifStatus.Text = $"Conversão concluída com sucesso!\nSalvo em: {outputPath}";
245+
txtGifStatus.Foreground = Brushes.Green;
246+
}
247+
catch (Exception ex)
248+
{
249+
txtGifStatus.Text = $"Erro durante a conversão: {ex.Message}";
250+
txtGifStatus.Foreground = Brushes.Red;
251+
}
252+
}
171253
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- https://go.microsoft.com/fwlink/?LinkID=208121. -->
3+
<Project>
4+
<PropertyGroup>
5+
<Configuration>Release</Configuration>
6+
<Platform>Any CPU</Platform>
7+
<PublishDir>bin\Release\net10.0-windows\publish\</PublishDir>
8+
<PublishProtocol>FileSystem</PublishProtocol>
9+
<_TargetId>Folder</_TargetId>
10+
</PropertyGroup>
11+
</Project>

WPF_Utils.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@
1313
<Resource Include="wpf_utils.ico" />
1414
</ItemGroup>
1515

16+
<ItemGroup>
17+
<PackageReference Include="Xabe.FFmpeg" Version="6.0.2" />
18+
<PackageReference Include="Xabe.FFmpeg.Downloader" Version="6.0.2" />
19+
</ItemGroup>
20+
1621
</Project>

0 commit comments

Comments
 (0)