-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
147 lines (121 loc) · 5.62 KB
/
api.py
File metadata and controls
147 lines (121 loc) · 5.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from flask import Flask, request, send_from_directory, jsonify
from flask_cors import CORS
import os
import yt_dlp
from moviepy.editor import VideoFileClip, CompositeVideoClip, ImageClip
from PIL import Image, ImageDraw, ImageFont
from textwrap import wrap
import numpy as np
import requests
import time
app = Flask(__name__)
CORS(app) # Habilitar CORS
# Diretório para armazenar os vídeos processados
VIDEO_DIR = os.path.join('static', 'videos')
os.makedirs(VIDEO_DIR, exist_ok=True)
# Função para baixar vídeo do YouTube usando yt-dlp
def download_video(url, output_path):
ydl_opts = {
'format': 'bestvideo+bestaudio/best', # Baixa o melhor formato disponível
'outtmpl': output_path,
'merge_output_format': 'mp4'
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
# Função para gerar legendas automáticas com AssemblyAI
def generate_subtitles(audio_path):
API_KEY = "181e4113234a4ebab72186d67ff11b22" # Insira sua chave de API do AssemblyAI
headers = {'authorization': API_KEY}
# Upload do arquivo de áudio para AssemblyAI
upload_url = 'https://api.assemblyai.com/v2/upload'
with open(audio_path, 'rb') as f:
response = requests.post(upload_url, headers=headers, files={'file': f})
audio_url = response.json()['upload_url']
# Solicitar transcrição
transcript_url = 'https://api.assemblyai.com/v2/transcript'
transcript_request = {'audio_url': audio_url}
transcript_response = requests.post(transcript_url, headers=headers, json=transcript_request)
transcript_id = transcript_response.json()['id']
# Aguardar conclusão da transcrição
while True:
transcript_status = requests.get(f'{transcript_url}/{transcript_id}', headers=headers).json()
if transcript_status['status'] == 'completed':
return transcript_status['words'] # Retorna palavras com timestamps
elif transcript_status['status'] == 'failed':
raise Exception('Transcription failed')
time.sleep(5)
# Função para adicionar legendas com diferentes estilos
def add_subtitles_with_style(video_path, subtitles, output_path, style='default'):
video = VideoFileClip(video_path)
def generator(txt):
wrapped_txt = "\n".join(wrap(txt.upper(), width=20))
img = Image.new("RGBA", (video.size[0], int(video.h * 0.2)), color=(0, 0, 0, 0))
d = ImageDraw.Draw(img)
fontsize = int(video.h * 0.04)
font = ImageFont.truetype("fonts/Montserrat-Black.ttf", fontsize)
left, top, right, bottom = d.multiline_textbbox((0, 0), wrapped_txt, font=font)
text_width, text_height = right - left, bottom - top
x = (img.width - text_width) // 2
y = (img.height - text_height) // 2
border_thickness = 8
border_color = "black"
# Mudança de estilo nas bordas
for i in range(-border_thickness, border_thickness + 1):
for j in range(-border_thickness, border_thickness + 1):
d.multiline_text((x + i, y + j), wrapped_txt, font=font, fill=border_color, align="center")
# Diferentes estilos para o preenchimento
if style == 'highlight':
fill_color = "yellow"
elif style == 'bold':
fill_color = "red"
else:
fill_color = "white"
d.multiline_text((x, y), wrapped_txt, font=font, fill=fill_color, align="center")
img_np = np.array(img)
return ImageClip(img_np)
# Criar legendas como clips de texto
subtitle_clips = [generator(word['text']).set_start(word['start'] / 1000).set_end(word['end'] / 1000) for word in subtitles]
# Sobrepor legendas no vídeo
final = CompositeVideoClip([video] + subtitle_clips)
final.write_videofile(output_path, fps=24)
# Função para processar vídeo para TikTok
def process_youtube_video_for_tiktok(youtube_url, start_time=None, end_time=None):
video_path = os.path.join(VIDEO_DIR, "downloaded_video.mp4")
download_video(youtube_url, video_path)
# Cortar vídeo
cut_video_path = os.path.join(VIDEO_DIR, "cut_video.mp4")
video = VideoFileClip(video_path).subclip(start_time, end_time)
# Adaptar para o formato TikTok (9:16)
tiktok_video = video.resize(height=1920).crop(x_center=video.w / 2, width=1080, height=1920)
tiktok_video.write_videofile(cut_video_path)
# Extrair áudio
audio_path = os.path.join(VIDEO_DIR, "audio.mp3")
video.audio.write_audiofile(audio_path)
# Gerar legendas
subtitles = generate_subtitles(audio_path)
# Adicionar legendas coloridas e chamativas
final_video_path = os.path.join(VIDEO_DIR, "final_tiktok_video.mp4")
add_subtitles_with_style(cut_video_path, subtitles, final_video_path, style='highlight')
return final_video_path
@app.route('/api/process_video', methods=['POST'])
def api_process_video():
data = request.get_json()
youtube_url = data.get('url')
start_time = data.get('start_time')
end_time = data.get('end_time')
# Converter para inteiro, se fornecido
start_time = int(start_time) if start_time else None
end_time = int(end_time) if end_time else None
try:
video_path = process_youtube_video_for_tiktok(youtube_url, start_time, end_time)
return jsonify({
'message': 'Vídeo processado com sucesso',
'video_path': f'http://localhost:8000/videos/{os.path.basename(video_path)}'
}), 200
except Exception as e:
return jsonify({'error': str(e)}), 400
@app.route('/videos/<filename>')
def get_video(filename):
return send_from_directory(VIDEO_DIR, filename)
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000, debug=True)