# app.py — DRIVE → MP4 CONVERTER (Space inayojitegemea, faili moja) # Inachukua viungo vya Google Drive (au kiungo chochote cha moja kwa moja cha video), inapakua, # inabadilisha kuwa MP4 inayocheza kwenye kila kivinjari/simu (H.264 + AAC, faststart), # na (hiari) inapakia kwenye HF Dataset ili upate kiungo cha moja kwa moja cha kuweka kwenye app. import os, re, sys, time, json, html, shutil, hashlib, threading, subprocess, tempfile, glob from pathlib import Path from urllib.parse import quote, unquote, urlparse, parse_qs # ZeroGPU Spaces zinadai angalau function moja ya @spaces.GPU wakati wa kuanza. # Converter hii inatumia CPU (ffmpeg) tu; function hii ni ya kimya na haifanyi chochote. # Ukitumia "CPU basic" (inapendekezwa) kipande hiki hakina athari. try: import spaces @spaces.GPU def _gpu_placeholder(): return None except Exception: pass WORK_DIR = os.path.join(tempfile.gettempdir(), "mp4_work") OUT_DIR = os.path.join(tempfile.gettempdir(), "mp4_out") os.makedirs(WORK_DIR, exist_ok=True) os.makedirs(OUT_DIR, exist_ok=True) VIDEO_EXTS = (".mp4", ".mkv", ".avi", ".mov", ".webm", ".flv", ".wmv", ".m4v", ".ts", ".3gp", ".mpg", ".mpeg") _lock = threading.Lock() _ffmpeg = {"ff": None} class ToolError(Exception): pass def human_size(n): for u in ["B", "KB", "MB", "GB"]: if n < 1024: return f"{n:.1f} {u}" n /= 1024 return f"{n:.1f} TB" def fmt_time(sec): sec = int(max(0, sec)) return f"{sec // 3600}:{sec % 3600 // 60:02d}:{sec % 60:02d}" def cleanup_old(max_age_h=6): now = time.time() for base in (WORK_DIR, OUT_DIR): for p in glob.glob(os.path.join(base, "*")): try: if now - os.path.getmtime(p) > max_age_h * 3600: shutil.rmtree(p, ignore_errors=True) if os.path.isdir(p) else os.remove(p) except Exception: pass # ── Viungo ─────────────────────────────────────────────────── def extract_drive_id(link): link = (link or "").strip() m = re.search(r"drive\.google\.com/(?:file/d/|open\?id=|uc\?(?:[^#]*&)?id=)([A-Za-z0-9_-]{15,})", link) if m: return m.group(1) m = re.search(r"[?&]id=([A-Za-z0-9_-]{15,})", link) if "google.com" in link else None return m.group(1) if m else None def clean_title(name): base = os.path.splitext(name)[0] base = re.sub(r"(?i)(https?://)?www\.[^\s]+", " ", base) # ondoa anwani za tovuti kwenye jina base = re.sub(r"[^\w\s\-\.\(\)]", " ", base, flags=re.UNICODE) # ondoa emoji/alama base = re.sub(r"\s+", " ", base).strip(" .-_") return (base or "video")[:80] def slug_file(title): s = re.sub(r"[^A-Za-z0-9]+", "-", title).strip("-").lower() return s or "video" def _filename_from_headers(r, fallback): cd = r.headers.get("content-disposition", "") or "" m = re.search(r"filename\*=UTF-8''([^;]+)", cd, re.I) if m: return unquote(m.group(1)).strip('"') m = re.search(r'filename="?([^";]+)"?', cd, re.I) if m: try: return m.group(1).encode("latin-1").decode("utf-8") except Exception: return m.group(1) return fallback def parse_confirm_form(page): """Ukurasa wa onyo la Drive ('haiwezi kuskani virusi') una form yenye uuid/at/confirm.""" action = re.search(r'