Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import requests | |
| import subprocess | |
| import time | |
| # SEUS SEGREDOS (Configure isso nas Settings do Space em "Repository Secrets") | |
| IG_USER_ID = os.environ.get("IG_USER_ID") | |
| ACCESS_TOKEN = os.environ.get("IG_ACCESS_TOKEN") | |
| def converter_e_publicar(video_webm): | |
| if not video_webm: | |
| return "Nenhum vídeo recebido." | |
| # 1. CONVERSÃO (WebM -> MP4) | |
| # O gradio entrega o caminho do arquivo temporário em video_webm | |
| output_path = video_webm.replace(".webm", ".mp4") | |
| # Comando FFmpeg para converter garantindo compatibilidade com Instagram | |
| # -c:v libx264 (Vídeo H.264) | |
| # -c:a aac (Áudio AAC) | |
| # -pix_fmt yuv420p (Formato de pixel exigido pelo player mobile) | |
| command = [ | |
| "ffmpeg", "-y", "-i", video_webm, | |
| "-c:v", "libx264", "-c:a", "aac", | |
| "-pix_fmt", "yuv420p", "-movflags", "+faststart", | |
| output_path | |
| ] | |
| subprocess.run(command, check=True) | |
| # 2. GERAR URL PÚBLICA (Truque do Gradio) | |
| # O arquivo agora está salvo no servidor. Precisamos da URL completa. | |
| # Em Spaces públicos, a URL segue esse padrão. Ajuste SEU_USER e SEU_SPACE. | |
| # O Gradio expõe arquivos na pasta /file/ | |
| filename = os.path.basename(output_path) | |
| # ATENÇÃO: O caminho interno do gradio costuma ser /tmp/gradio/..., | |
| # a URL pública pega o caminho absoluto. | |
| public_url = f"https://SEU_USER-SEU_SPACE.hf.space/file={output_path}" | |
| # 3. PUBLICAR NO INSTAGRAM | |
| # Passo A: Criar Container | |
| url_container = f"https://graph.facebook.com/v19.0/{IG_USER_ID}/media" | |
| payload = { | |
| "media_type": "REELS", # Ou VIDEO | |
| "video_url": public_url, | |
| "caption": "Vídeo gerado automaticamente via Gemini App Builder 🚀", | |
| "access_token": ACCESS_TOKEN | |
| } | |
| r = requests.post(url_container, data=payload) | |
| if r.status_code != 200: | |
| return f"Erro ao criar container: {r.text}" | |
| container_id = r.json().get("id") | |
| # Passo B: Esperar processamento (Polling) | |
| status = "IN_PROGRESS" | |
| while status == "IN_PROGRESS": | |
| time.sleep(5) # Espera 5 segundos | |
| status_url = f"https://graph.facebook.com/v19.0/{container_id}?fields=status_code&access_token={ACCESS_TOKEN}" | |
| status_resp = requests.get(status_url).json() | |
| status = status_resp.get("status_code") | |
| print(f"Status do vídeo: {status}") | |
| if status == "ERROR": | |
| return "Erro no processamento do vídeo pelo Instagram." | |
| # Passo C: Publicar Oficialmente | |
| publish_url = f"https://graph.facebook.com/v19.0/{IG_USER_ID}/media_publish" | |
| pub_payload = { | |
| "creation_id": container_id, | |
| "access_token": ACCESS_TOKEN | |
| } | |
| final_r = requests.post(publish_url, data=pub_payload) | |
| if final_r.status_code == 200: | |
| return "Sucesso! Vídeo publicado no Instagram." | |
| else: | |
| return f"Erro na publicação final: {final_r.text}" | |
| # Interface simples (API mode) | |
| demo = gr.Interface( | |
| fn=converter_e_publicar, | |
| inputs=gr.Video(label="Envie seu WebM"), | |
| outputs="text" | |
| ) | |
| demo.launch() |