Spaces:
Sleeping
Sleeping
import subprocess | |
import uuid | |
import ffmpeg | |
import gradio as gr | |
import os | |
import re | |
# Função para baixar o clipe da Twitch usando twitch-dl | |
def download_twitch_clip(url, auth_token): | |
# Gerar um UUID para o nome do arquivo | |
unique_id = uuid.uuid4() | |
output_filename = f"{unique_id}.mkv" | |
# Comando para baixar o vídeo | |
command = ["twitch-dl", "download", url, "-q", "source", "-o", output_filename] | |
# Adiciona o token de autenticação, se fornecido | |
if auth_token.strip(): | |
command.extend(["-a", auth_token]) | |
# Executa o comando de download | |
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
stdout, stderr = process.communicate() | |
if process.returncode != 0: | |
raise Exception(f"Erro no download do vídeo: {stderr.decode('utf-8')}") | |
return output_filename | |
# Função para converter MKV para MP4 usando ffmpeg-python | |
def convert_to_mp4(input_file): | |
output_file = input_file.replace('.mkv', '.mp4') | |
try: | |
( | |
ffmpeg | |
.input(input_file) | |
.output(output_file, vcodec='libx264', acodec='aac') | |
.run(overwrite_output=True) | |
) | |
return output_file | |
except ffmpeg.Error as e: | |
print(f"Erro ao converter o arquivo: {e}") | |
return None | |
# Interface Gradio | |
def gradio_interface(url, auth_token=""): | |
mkv_file = download_twitch_clip(url, auth_token) | |
mp4_file = convert_to_mp4(mkv_file) | |
return mp4_file | |
iface = gr.Interface( | |
fn=gradio_interface, | |
inputs=[ | |
gr.Textbox(label="URL do Clipe da Twitch"), | |
gr.Textbox(label="Token de Autenticação (opcional)") | |
], | |
outputs=gr.Video() | |
) | |
# Executar a interface | |
iface.launch() | |