Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
@@ -11,43 +11,25 @@ def get_video_duration(video_path):
|
|
11 |
return duration
|
12 |
|
13 |
def merge_videos(video1_path, video2_path):
|
14 |
-
"""Mescla dois vídeos verticalmente em uma proporção de aspecto de 9:16."""
|
15 |
-
|
16 |
output_filename = f"{uuid4()}_merged.mp4"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
-
try:
|
19 |
-
# Determine as propriedades do vídeo de entrada
|
20 |
-
video1_info = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", video1_path], capture_output=True, text=True, check=True).stdout.split()[4:]
|
21 |
-
video2_info = subprocess.run(["ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height", video2_path], capture_output=True, text=True, check=True).stdout.split()[4:]
|
22 |
-
video1_width, video1_height = int(video1_info[0]), int(video1_info[1])
|
23 |
-
video2_width, video2_height = int(video2_info[0]), int(video2_info[1])
|
24 |
-
|
25 |
-
# Calcule as dimensões de saída apropriadas enquanto mantém a proporção de aspecto
|
26 |
-
min_width = min(video1_width, video2_width)
|
27 |
-
output_width = min_width * 2 # Duplique a largura para posicionamento lado a lado
|
28 |
-
output_height = int(output_width * 9 / 16) # Mantenha a proporção de aspecto de 9:16
|
29 |
-
|
30 |
-
# Calcule os valores de preenchimento para centralizar cada vídeo verticalmente
|
31 |
-
video1_pad_top = int((output_height - video1_height) / 2)
|
32 |
-
video2_pad_top = int((output_height - video2_height) / 2)
|
33 |
-
|
34 |
-
# Construa o comando FFmpeg com preenchimento dinâmico e manipulação da proporção de aspecto
|
35 |
-
ffmpeg_cmd = (
|
36 |
-
f'ffmpeg -i "{video1_path}" -i "{video2_path}" '
|
37 |
-
f'-filter_complex '
|
38 |
-
f'"[0:v]scale=-2:{output_width}:force_original_aspect_ratio=decrease,pad={output_width}:{output_height}:{video1_pad_top}:0,setsar=1[v0];'
|
39 |
-
f'[1:v]scale=-2:{output_width}:force_original_aspect_ratio=decrease,pad={output_width}:{output_height}:{video2_pad_top}:0,setsar=1[v1];'
|
40 |
-
f'[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[v][a]" '
|
41 |
-
f'-map "[v]" -map "[a]" -c:v libx264 -c:a aac -aspect 9:16 {output_filename}'
|
42 |
-
)
|
43 |
-
|
44 |
-
# Execute o comando FFmpeg e retorne o nome do arquivo de saída
|
45 |
-
subprocess.run(ffmpeg_cmd, shell=True, check=True)
|
46 |
-
return output_filename
|
47 |
-
|
48 |
-
except Exception as e:
|
49 |
-
print(f"Erro ao mesclar vídeos: {e}")
|
50 |
-
return None
|
51 |
|
52 |
def gradio_interface(video1, video2):
|
53 |
# Os vídeos já são passados como caminhos de arquivo temporário, então não precisamos abrir e salvar
|
@@ -61,4 +43,4 @@ iface = gr.Interface(fn=gradio_interface,
|
|
61 |
description="Faça upload de dois vídeos para mesclá-los verticalmente em um estilo adequado para TikTok.")
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
iface.launch()
|
|
|
11 |
return duration
|
12 |
|
13 |
def merge_videos(video1_path, video2_path):
|
|
|
|
|
14 |
output_filename = f"{uuid4()}_merged.mp4"
|
15 |
+
|
16 |
+
# Altura e largura desejadas para o vídeo de saída
|
17 |
+
output_height = 1280
|
18 |
+
output_width = 720
|
19 |
+
|
20 |
+
ffmpeg_cmd = (
|
21 |
+
f'ffmpeg -i "{video1_path}" -i "{video2_path}" '
|
22 |
+
f'-filter_complex '
|
23 |
+
f'"[0:v]scale=-1:{output_height}:force_original_aspect_ratio=decrease,pad={output_width}:{output_height}:(ow-iw)/2:(oh-ih)/2,setsar=1[v0];'
|
24 |
+
f'[1:v]scale=-1:{output_height}:force_original_aspect_ratio=decrease,pad={output_width}:{output_height}:(ow-iw)/2:(oh-ih)/2,setsar=1[v1];'
|
25 |
+
f'[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[v][a]" '
|
26 |
+
f'-map "[v]" -map "[a]" -c:v libx264 -c:a aac -aspect 9:16 {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
|
|
|
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()
|