22using System . IO ;
33using System . Windows ;
44using System . Windows . Controls ;
5- using System . Windows . Data ;
6- using System . Windows . Documents ;
7- using System . Windows . Input ;
85using System . Windows . Media ;
9- using System . Windows . Media . Imaging ;
10- using System . Windows . Navigation ;
11- using System . Windows . Shapes ;
126using Microsoft . VisualBasic . FileIO ;
7+ using Xabe . FFmpeg ;
8+ using Xabe . FFmpeg . Downloader ;
9+ using System . Threading . Tasks ;
1310
1411namespace 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!\n Salvo 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}
0 commit comments