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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import tempfile
|
| 6 |
+
import shutil
|
| 7 |
+
import time
|
| 8 |
+
|
| 9 |
+
st.set_page_config(page_title="TikTok Video Generator", layout="centered")
|
| 10 |
+
st.title("🎥 TikTok Video Generator - Completo")
|
| 11 |
+
|
| 12 |
+
st.markdown("Envie seus vídeos e gere conteúdo com efeitos, filtros e controle total de duração!")
|
| 13 |
+
|
| 14 |
+
# Upload dos vídeos
|
| 15 |
+
cortes = st.file_uploader("Envie os vídeos de cortes", type=["mp4"], accept_multiple_files=True)
|
| 16 |
+
video_tutorial = st.file_uploader("Envie o vídeo pronto/tutorial (será inserido automaticamente)", type="mp4")
|
| 17 |
+
|
| 18 |
+
# Configurações
|
| 19 |
+
num_videos_finais = st.number_input("Quantos vídeos finais gerar?", min_value=1, max_value=10, value=1)
|
| 20 |
+
duracao_final = st.number_input("Duração final do vídeo (em segundos)", min_value=10, max_value=300, value=30)
|
| 21 |
+
duracao_corte = st.number_input("Duração de cada corte aleatório (s)", min_value=1, max_value=30, value=5)
|
| 22 |
+
zoom = st.slider("Zoom Central (1.0 = normal)", 1.0, 2.0, 1.2, 0.1)
|
| 23 |
+
blur_strength = st.slider("Intensidade do Blur", 1, 50, 10)
|
| 24 |
+
velocidade_cortes = st.slider("Velocidade dos cortes", 0.5, 2.0, 1.0, 0.1)
|
| 25 |
+
velocidade_final = st.slider("Velocidade final do vídeo", 0.5, 2.0, 1.0, 0.1)
|
| 26 |
+
crf_value = st.slider("Qualidade CRF (menor = melhor qualidade)", 18, 30, 23)
|
| 27 |
+
|
| 28 |
+
st.write("### Filtros no Fundo")
|
| 29 |
+
ativar_blur_fundo = st.checkbox("Ativar vídeo de fundo com Blur", value=True)
|
| 30 |
+
ativar_sepia = st.checkbox("Sépia", value=False)
|
| 31 |
+
ativar_granulado = st.checkbox("Granulado", value=False)
|
| 32 |
+
ativar_pb = st.checkbox("Preto e Branco Leve", value=False)
|
| 33 |
+
ativar_vignette = st.checkbox("Vignette", value=False)
|
| 34 |
+
|
| 35 |
+
st.write("### Outros")
|
| 36 |
+
ativar_espelhar = st.checkbox("Espelhar Vídeo", value=True)
|
| 37 |
+
ativar_filtro_cor = st.checkbox("Filtro de Cor (Contraste/Saturação)", value=True)
|
| 38 |
+
remover_borda = st.checkbox("Remover bordas do vídeo centralizado")
|
| 39 |
+
tamanho_borda = st.slider("Tamanho da borda (px)", 0, 200, 0, 10)
|
| 40 |
+
|
| 41 |
+
if st.button("Gerar Vídeo(s)"):
|
| 42 |
+
if not cortes or not video_tutorial:
|
| 43 |
+
st.error("❌ Envie os vídeos para continuar.")
|
| 44 |
+
else:
|
| 45 |
+
with st.spinner('🎥 Seu vídeo está sendo gerado...'):
|
| 46 |
+
progresso = st.progress(0)
|
| 47 |
+
temp_dir = tempfile.mkdtemp()
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
tutorial_path = os.path.join(temp_dir, "tutorial_raw.mp4")
|
| 51 |
+
with open(tutorial_path, "wb") as f:
|
| 52 |
+
f.write(video_tutorial.read())
|
| 53 |
+
|
| 54 |
+
tutorial_mp4 = os.path.join(temp_dir, "tutorial.mp4")
|
| 55 |
+
subprocess.run([
|
| 56 |
+
"ffmpeg", "-i", tutorial_path, "-vf", "scale=720:1280,fps=30", "-c:v", "libx264",
|
| 57 |
+
"-preset", "ultrafast", "-crf", str(crf_value), "-y", tutorial_mp4
|
| 58 |
+
], check=True, stderr=subprocess.PIPE)
|
| 59 |
+
|
| 60 |
+
progresso.progress(10)
|
| 61 |
+
|
| 62 |
+
cortes_names = []
|
| 63 |
+
for idx, corte in enumerate(cortes):
|
| 64 |
+
nome = os.path.join(temp_dir, f"corte_{idx}_{random.randint(1000,9999)}.mp4")
|
| 65 |
+
with open(nome, "wb") as f:
|
| 66 |
+
f.write(corte.read())
|
| 67 |
+
cortes_names.append(nome)
|
| 68 |
+
|
| 69 |
+
progresso.progress(20)
|
| 70 |
+
|
| 71 |
+
for n in range(num_videos_finais):
|
| 72 |
+
cortes_prontos = []
|
| 73 |
+
random.shuffle(cortes_names)
|
| 74 |
+
progresso.progress(20 + int(20 * (n / num_videos_finais)))
|
| 75 |
+
|
| 76 |
+
tempo_total = 0
|
| 77 |
+
while tempo_total < duracao_final:
|
| 78 |
+
for c in cortes_names:
|
| 79 |
+
dur_proc = subprocess.run([
|
| 80 |
+
"ffprobe", "-v", "error", "-show_entries", "format=duration",
|
| 81 |
+
"-of", "default=noprint_wrappers=1:nokey=1", c
|
| 82 |
+
], stdout=subprocess.PIPE)
|
| 83 |
+
|
| 84 |
+
dur = dur_proc.stdout.decode().strip()
|
| 85 |
+
try:
|
| 86 |
+
d = float(dur)
|
| 87 |
+
if d > duracao_corte:
|
| 88 |
+
ini = random.uniform(0, d - duracao_corte)
|
| 89 |
+
out = os.path.join(temp_dir, f"cut_{n}_{random.randint(1000,9999)}.mp4")
|
| 90 |
+
subprocess.run([
|
| 91 |
+
"ffmpeg", "-ss", str(ini), "-i", c, "-t", str(duracao_corte),
|
| 92 |
+
"-an", "-c:v", "libx264", "-preset", "ultrafast", "-crf", "30", out
|
| 93 |
+
], check=True, stderr=subprocess.PIPE)
|
| 94 |
+
cortes_prontos.append(out)
|
| 95 |
+
tempo_total += duracao_corte / velocidade_cortes
|
| 96 |
+
if tempo_total >= duracao_final:
|
| 97 |
+
break
|
| 98 |
+
except:
|
| 99 |
+
continue
|
| 100 |
+
|
| 101 |
+
lista = os.path.join(temp_dir, f"lista_{n}.txt")
|
| 102 |
+
with open(lista, "w") as f:
|
| 103 |
+
for c in cortes_prontos:
|
| 104 |
+
f.write(f"file '{c}'\n")
|
| 105 |
+
|
| 106 |
+
video_raw = os.path.join(temp_dir, f"video_raw_{n}.mp4")
|
| 107 |
+
subprocess.run([
|
| 108 |
+
"ffmpeg", "-f", "concat", "-safe", "0", "-i", lista, "-c:v", "libx264",
|
| 109 |
+
"-preset", "ultrafast", "-crf", "30", video_raw
|
| 110 |
+
], check=True, stderr=subprocess.PIPE)
|
| 111 |
+
|
| 112 |
+
progresso.progress(40 + int(20 * (n / num_videos_finais)))
|
| 113 |
+
|
| 114 |
+
filtros_main = []
|
| 115 |
+
if zoom != 1.0:
|
| 116 |
+
filtros_main.append(f"scale=iw*{zoom}:ih*{zoom}")
|
| 117 |
+
filtros_main.append(f"setpts=PTS/{velocidade_cortes}")
|
| 118 |
+
if ativar_espelhar:
|
| 119 |
+
filtros_main.append("hflip")
|
| 120 |
+
if remover_borda and tamanho_borda > 0:
|
| 121 |
+
filtros_main.append(f"crop=in_w-{tamanho_borda*2}:in_h-{tamanho_borda*2}")
|
| 122 |
+
if ativar_filtro_cor:
|
| 123 |
+
filtros_main.append("eq=contrast=1.1:saturation=1.2")
|
| 124 |
+
|
| 125 |
+
if ativar_blur_fundo:
|
| 126 |
+
filtro_complex = "[0:v]split=2[main][bg];"
|
| 127 |
+
if filtros_main:
|
| 128 |
+
filtro_complex += f"[main]{','.join(filtros_main)}[zoomed];"
|
| 129 |
+
else:
|
| 130 |
+
filtro_complex += "[main][zoomed];"
|
| 131 |
+
filtro_complex += f"[bg]scale=720:1280,boxblur={blur_strength}:1"
|
| 132 |
+
if ativar_sepia:
|
| 133 |
+
filtro_complex += ",colorchannelmixer=.393:.769:.189:0:.349:.686:.168:0:.272:.534:.131"
|
| 134 |
+
if ativar_granulado:
|
| 135 |
+
filtro_complex += ",noise=alls=20:allf=t+u"
|
| 136 |
+
if ativar_pb:
|
| 137 |
+
filtro_complex += ",hue=s=0.3"
|
| 138 |
+
if ativar_vignette:
|
| 139 |
+
filtro_complex += ",vignette"
|
| 140 |
+
filtro_complex += "[blur];[blur][zoomed]overlay=(W-w)/2:(H-h)/2"
|
| 141 |
+
else:
|
| 142 |
+
filtro_complex = "[0:v]scale=720:1280[main];"
|
| 143 |
+
if filtros_main:
|
| 144 |
+
filtro_complex += f"[main]{','.join(filtros_main)}[zoomed];"
|
| 145 |
+
else:
|
| 146 |
+
filtro_complex += "[main][zoomed];"
|
| 147 |
+
filtro_complex += "color=black:size=720x1280:d=1[bgblack];[bgblack][zoomed]overlay=(W-w)/2:(H-h)/2"
|
| 148 |
+
|
| 149 |
+
video_editado = os.path.join(temp_dir, f"video_editado_{n}.mp4")
|
| 150 |
+
subprocess.run([
|
| 151 |
+
"ffmpeg", "-i", video_raw, "-filter_complex", filtro_complex, "-c:v", "libx264",
|
| 152 |
+
"-preset", "ultrafast", "-crf", str(crf_value), video_editado
|
| 153 |
+
], check=True, stderr=subprocess.PIPE)
|
| 154 |
+
|
| 155 |
+
part1 = os.path.join(temp_dir, f"part1_{n}.mp4")
|
| 156 |
+
part2 = os.path.join(temp_dir, f"part2_{n}.mp4")
|
| 157 |
+
|
| 158 |
+
dur_proc = subprocess.run([
|
| 159 |
+
"ffprobe", "-v", "error", "-show_entries", "format=duration",
|
| 160 |
+
"-of", "default=noprint_wrappers=1:nokey=1", video_editado
|
| 161 |
+
], stdout=subprocess.PIPE)
|
| 162 |
+
|
| 163 |
+
dur_f = float(dur_proc.stdout.decode().strip())
|
| 164 |
+
pt = dur_f / 2 if dur_f < 10 else random.uniform(5, dur_f - 5)
|
| 165 |
+
|
| 166 |
+
subprocess.run(["ffmpeg", "-ss", "0", "-i", video_editado, "-t", str(pt), "-c:v", "libx264",
|
| 167 |
+
"-preset", "ultrafast", part1], check=True, stderr=subprocess.PIPE)
|
| 168 |
+
subprocess.run(["ffmpeg", "-ss", str(pt), "-i", video_editado, "-c:v", "libx264",
|
| 169 |
+
"-preset", "ultrafast", part2], check=True, stderr=subprocess.PIPE)
|
| 170 |
+
|
| 171 |
+
final_txt = os.path.join(temp_dir, f"final_{n}.txt")
|
| 172 |
+
with open(final_txt, "w") as f:
|
| 173 |
+
f.write(f"file '{part1}'\nfile '{tutorial_mp4}'\nfile '{part2}'\n")
|
| 174 |
+
|
| 175 |
+
video_final_raw = os.path.join(temp_dir, f"video_final_raw_{n}.mp4")
|
| 176 |
+
subprocess.run(["ffmpeg", "-f", "concat", "-safe", "0", "-i", final_txt, "-c:v", "libx264",
|
| 177 |
+
"-preset", "ultrafast", "-crf", str(crf_value), video_final_raw], check=True, stderr=subprocess.PIPE)
|
| 178 |
+
|
| 179 |
+
timestamp = int(time.time())
|
| 180 |
+
video_final = f"video_final_{n}_{timestamp}.mp4"
|
| 181 |
+
subprocess.run(["ffmpeg", "-y", "-i", video_final_raw, "-filter:v", f"setpts=PTS/{velocidade_final}",
|
| 182 |
+
"-t", str(duracao_final), "-c:v", "libx264", "-preset", "ultrafast",
|
| 183 |
+
"-crf", str(crf_value), video_final], check=True, stderr=subprocess.PIPE)
|
| 184 |
+
|
| 185 |
+
progresso.progress(90 + int(10 * (n / num_videos_finais)))
|
| 186 |
+
|
| 187 |
+
st.video(video_final)
|
| 188 |
+
with open(video_final, "rb") as f:
|
| 189 |
+
st.download_button(f"📅 Baixar Vídeo {n+1}", f, file_name=video_final)
|
| 190 |
+
|
| 191 |
+
progresso.progress(100)
|
| 192 |
+
st.success("✅ Todos os vídeos foram gerados com sucesso!")
|
| 193 |
+
|
| 194 |
+
except subprocess.CalledProcessError as e:
|
| 195 |
+
st.error(f"Erro durante processamento: {e.stderr.decode()}")
|
| 196 |
+
|
| 197 |
+
finally:
|
| 198 |
+
shutil.rmtree(temp_dir)
|