artificialguybr commited on
Commit
03d88b9
1 Parent(s): 0bedcb0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import os
4
+ from uuid import uuid4
5
+
6
+ def get_video_duration(video_path):
7
+ """Obtém a duração do vídeo usando ffprobe."""
8
+ cmd = f"ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 \"{video_path}\""
9
+ result = subprocess.run(cmd, shell=True, text=True, capture_output=True)
10
+ duration = float(result.stdout)
11
+ return duration
12
+
13
+ def merge_videos(video1_path, video2_path):
14
+ output_filename = f"{uuid4()}_merged.mp4"
15
+
16
+ # Não limitaremos a duração ao vídeo 1, pois queremos o vídeo completo mesclado.
17
+ # A largura comum permanece, mas agora lidaremos com aspect ratios variados de forma mais dinâmica.
18
+ common_width = 720
19
+
20
+ ffmpeg_cmd = (
21
+ f'ffmpeg -i "{video1_path}" -i "{video2_path}" '
22
+ f'-filter_complex '
23
+ f'"[0:v]scale={common_width}:-2:force_original_aspect_ratio=decrease,pad={common_width}:(ih*2):(ow-iw)/2:0,setdar=dar[v0];'
24
+ f'[1:v]scale={common_width}:-2:force_original_aspect_ratio=decrease,pad={common_width}:(ih*2):(ow-iw)/2:ih,setdar=dar[v1];'
25
+ f'[v0][v1]concat=n=2:v=1:a=0[v]" '
26
+ f'-map "[v]" -map 0:a? -c:v libx264 -c:a aac {output_filename}'
27
+ )
28
+
29
+ subprocess.run(ffmpeg_cmd, shell=True, check=True)
30
+
31
+ return output_filename
32
+
33
+
34
+ def gradio_interface(video1, video2):
35
+ # Os vídeos já são passados como caminhos de arquivo temporário, então não precisamos abrir e salvar
36
+ output_video = merge_videos(video1, video2)
37
+ return output_video
38
+
39
+ iface = gr.Interface(fn=gradio_interface,
40
+ inputs=[gr.Video(label="Video 1"), gr.Video(label="Video 2")],
41
+ outputs=gr.Video(label="Vídeo Mesclado"),
42
+ title="Mesclador de Vídeos para TikTok",
43
+ description="Faça upload de dois vídeos para mesclá-los verticalmente em um estilo adequado para TikTok.")
44
+
45
+ if __name__ == "__main__":
46
+ iface.launch()