Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import whisper
|
|
| 3 |
import yt_dlp
|
| 4 |
import os
|
| 5 |
import tempfile
|
|
|
|
|
|
|
| 6 |
|
| 7 |
models = {}
|
| 8 |
|
|
@@ -38,8 +40,102 @@ def devanagari_to_roman(text):
|
|
| 38 |
return ''.join(result)
|
| 39 |
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def download_from_url(url):
|
| 42 |
-
"""
|
|
|
|
|
|
|
|
|
|
| 43 |
tmp_dir = tempfile.mkdtemp()
|
| 44 |
output_path = os.path.join(tmp_dir, 'audio.%(ext)s')
|
| 45 |
ydl_opts = {
|
|
@@ -47,7 +143,6 @@ def download_from_url(url):
|
|
| 47 |
'outtmpl': output_path,
|
| 48 |
'quiet': True,
|
| 49 |
'no_warnings': True,
|
| 50 |
-
'extract_flat': False,
|
| 51 |
'postprocessors': [{
|
| 52 |
'key': 'FFmpegExtractAudio',
|
| 53 |
'preferredcodec': 'mp3',
|
|
@@ -55,16 +150,9 @@ def download_from_url(url):
|
|
| 55 |
}],
|
| 56 |
'http_headers': {
|
| 57 |
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
|
| 58 |
-
'Accept-Language': 'en-US,en;q=0.9',
|
| 59 |
-
'Accept': '*/*',
|
| 60 |
-
'Referer': 'https://www.instagram.com/',
|
| 61 |
},
|
| 62 |
-
'
|
| 63 |
-
'instagram': {'api_version': 'v1'},
|
| 64 |
-
},
|
| 65 |
-
'socket_timeout': 30,
|
| 66 |
'retries': 3,
|
| 67 |
-
'ignoreerrors': False,
|
| 68 |
'geo_bypass': True,
|
| 69 |
}
|
| 70 |
try:
|
|
@@ -73,21 +161,19 @@ def download_from_url(url):
|
|
| 73 |
title = info.get('title', 'video')
|
| 74 |
except Exception as e:
|
| 75 |
err = str(e)
|
| 76 |
-
if '
|
| 77 |
-
raise Exception("Instagram blocked this request. Try again in a few seconds, or make sure the post is public.")
|
| 78 |
-
elif 'private' in err.lower():
|
| 79 |
raise Exception("This account is private. Only public posts can be downloaded.")
|
| 80 |
elif 'not found' in err.lower() or '404' in err:
|
| 81 |
raise Exception("Video not found. Check the URL and make sure the post still exists.")
|
| 82 |
else:
|
| 83 |
raise Exception(f"Download failed: {err}")
|
| 84 |
|
| 85 |
-
# Find the downloaded mp3
|
| 86 |
for f in os.listdir(tmp_dir):
|
| 87 |
if f.endswith('.mp3'):
|
| 88 |
return os.path.join(tmp_dir, f), title
|
| 89 |
raise Exception("Download succeeded but audio file not found.")
|
| 90 |
|
|
|
|
| 91 |
def transcribe(file, model_name, language, show_timestamps, translate):
|
| 92 |
if file is None:
|
| 93 |
return "Please upload a video or audio file.", ""
|
|
@@ -161,7 +247,7 @@ def save_transcript(text):
|
|
| 161 |
|
| 162 |
|
| 163 |
def download_video_only(url):
|
| 164 |
-
"""Download video
|
| 165 |
if not url or not url.strip():
|
| 166 |
return None, "Please paste a valid URL."
|
| 167 |
url = url.strip()
|
|
@@ -169,6 +255,13 @@ def download_video_only(url):
|
|
| 169 |
'x.com','facebook.com','fb.watch']
|
| 170 |
if not any(s in url.lower() for s in supported):
|
| 171 |
return None, "Unsupported URL."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
tmp_dir = tempfile.mkdtemp()
|
| 173 |
output_path = os.path.join(tmp_dir, 'video.%(ext)s')
|
| 174 |
ydl_opts = {
|
|
@@ -522,4 +615,4 @@ by Kalpi Edition
|
|
| 522 |
download_btn.click(fn=save_transcript, inputs=plain_output, outputs=download_file)
|
| 523 |
|
| 524 |
if __name__ == "__main__":
|
| 525 |
-
demo.launch(css=custom_css)
|
|
|
|
| 3 |
import yt_dlp
|
| 4 |
import os
|
| 5 |
import tempfile
|
| 6 |
+
import requests
|
| 7 |
+
import requests
|
| 8 |
|
| 9 |
models = {}
|
| 10 |
|
|
|
|
| 40 |
return ''.join(result)
|
| 41 |
|
| 42 |
|
| 43 |
+
RAPIDAPI_KEY = "47b2f0d88bmsh7842ac99f4b2a3ep12df5djsn6f61065d9692"
|
| 44 |
+
RAPIDAPI_HOST = "instagram-reels-downloader-api.p.rapidapi.com"
|
| 45 |
+
|
| 46 |
+
def download_instagram_audio(url):
|
| 47 |
+
"""Download Instagram reel via RapidAPI then extract audio"""
|
| 48 |
+
headers = {
|
| 49 |
+
"x-rapidapi-key": RAPIDAPI_KEY,
|
| 50 |
+
"x-rapidapi-host": RAPIDAPI_HOST,
|
| 51 |
+
"Content-Type": "application/json"
|
| 52 |
+
}
|
| 53 |
+
resp = requests.get(
|
| 54 |
+
f"https://{RAPIDAPI_HOST}/download",
|
| 55 |
+
headers=headers,
|
| 56 |
+
params={"url": url},
|
| 57 |
+
timeout=30
|
| 58 |
+
)
|
| 59 |
+
if resp.status_code != 200:
|
| 60 |
+
raise Exception(f"RapidAPI error {resp.status_code}: {resp.text[:200]}")
|
| 61 |
+
|
| 62 |
+
data = resp.json()
|
| 63 |
+
video_url = None
|
| 64 |
+
if isinstance(data, dict):
|
| 65 |
+
video_url = (data.get('url') or data.get('video_url') or
|
| 66 |
+
data.get('download_url') or
|
| 67 |
+
(data.get('data') or {}).get('url') or
|
| 68 |
+
(data.get('data') or {}).get('video_url'))
|
| 69 |
+
elif isinstance(data, list) and len(data) > 0:
|
| 70 |
+
item = data[0]
|
| 71 |
+
video_url = item.get('url') or item.get('video_url') or item.get('download_url')
|
| 72 |
+
|
| 73 |
+
if not video_url:
|
| 74 |
+
raise Exception(f"No download URL in response: {str(data)[:300]}")
|
| 75 |
+
|
| 76 |
+
tmp_dir = tempfile.mkdtemp()
|
| 77 |
+
video_path = os.path.join(tmp_dir, 'ig_video.mp4')
|
| 78 |
+
audio_path = os.path.join(tmp_dir, 'audio.mp3')
|
| 79 |
+
|
| 80 |
+
vid_resp = requests.get(video_url, timeout=60, stream=True,
|
| 81 |
+
headers={"User-Agent":"Mozilla/5.0"})
|
| 82 |
+
with open(video_path, 'wb') as f:
|
| 83 |
+
for chunk in vid_resp.iter_content(chunk_size=8192):
|
| 84 |
+
f.write(chunk)
|
| 85 |
+
|
| 86 |
+
os.system(f'ffmpeg -i "{video_path}" -q:a 0 -map a "{audio_path}" -y -loglevel quiet')
|
| 87 |
+
if os.path.exists(audio_path) and os.path.getsize(audio_path) > 0:
|
| 88 |
+
return audio_path, 'instagram_reel'
|
| 89 |
+
return video_path, 'instagram_reel'
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
RAPIDAPI_KEY = "47b2f0d88bmsh7842ac99f4b2a3ep12df5djsn6f61065d9692"
|
| 93 |
+
RAPIDAPI_HOST = "instagram-reels-downloader-api.p.rapidapi.com"
|
| 94 |
+
|
| 95 |
+
def download_instagram_via_rapidapi(url):
|
| 96 |
+
"""Download Instagram reel using RapidAPI — reliable, no IP blocks"""
|
| 97 |
+
headers = {
|
| 98 |
+
"x-rapidapi-key": RAPIDAPI_KEY,
|
| 99 |
+
"x-rapidapi-host": RAPIDAPI_HOST,
|
| 100 |
+
"Content-Type": "application/json"
|
| 101 |
+
}
|
| 102 |
+
params = {"url": url}
|
| 103 |
+
resp = requests.get(
|
| 104 |
+
f"https://{RAPIDAPI_HOST}/download",
|
| 105 |
+
headers=headers,
|
| 106 |
+
params=params,
|
| 107 |
+
timeout=30
|
| 108 |
+
)
|
| 109 |
+
if resp.status_code != 200:
|
| 110 |
+
raise Exception(f"RapidAPI error {resp.status_code}: {resp.text[:200]}")
|
| 111 |
+
data = resp.json()
|
| 112 |
+
# Extract direct video URL from response
|
| 113 |
+
video_url = None
|
| 114 |
+
if isinstance(data, dict):
|
| 115 |
+
video_url = (data.get('url') or data.get('download_url') or
|
| 116 |
+
data.get('video_url') or data.get('link'))
|
| 117 |
+
if not video_url and data.get('data'):
|
| 118 |
+
d = data['data']
|
| 119 |
+
if isinstance(d, list) and len(d) > 0:
|
| 120 |
+
video_url = d[0].get('url') or d[0].get('download_url')
|
| 121 |
+
elif isinstance(d, dict):
|
| 122 |
+
video_url = d.get('url') or d.get('download_url')
|
| 123 |
+
if not video_url:
|
| 124 |
+
raise Exception(f"No video URL in response: {str(data)[:300]}")
|
| 125 |
+
# Download the actual video file
|
| 126 |
+
tmp_dir = tempfile.mkdtemp()
|
| 127 |
+
tmp_path = os.path.join(tmp_dir, 'instagram.mp4')
|
| 128 |
+
video_resp = requests.get(video_url, timeout=60, stream=True)
|
| 129 |
+
with open(tmp_path, 'wb') as f:
|
| 130 |
+
for chunk in video_resp.iter_content(chunk_size=8192):
|
| 131 |
+
f.write(chunk)
|
| 132 |
+
return tmp_path, data.get('title', 'Instagram video')
|
| 133 |
+
|
| 134 |
def download_from_url(url):
|
| 135 |
+
"""Instagram → RapidAPI, everything else → yt-dlp"""
|
| 136 |
+
if 'instagram.com' in url.lower():
|
| 137 |
+
return download_instagram_audio(url)
|
| 138 |
+
|
| 139 |
tmp_dir = tempfile.mkdtemp()
|
| 140 |
output_path = os.path.join(tmp_dir, 'audio.%(ext)s')
|
| 141 |
ydl_opts = {
|
|
|
|
| 143 |
'outtmpl': output_path,
|
| 144 |
'quiet': True,
|
| 145 |
'no_warnings': True,
|
|
|
|
| 146 |
'postprocessors': [{
|
| 147 |
'key': 'FFmpegExtractAudio',
|
| 148 |
'preferredcodec': 'mp3',
|
|
|
|
| 150 |
}],
|
| 151 |
'http_headers': {
|
| 152 |
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1',
|
|
|
|
|
|
|
|
|
|
| 153 |
},
|
| 154 |
+
'socket_timeout': 60,
|
|
|
|
|
|
|
|
|
|
| 155 |
'retries': 3,
|
|
|
|
| 156 |
'geo_bypass': True,
|
| 157 |
}
|
| 158 |
try:
|
|
|
|
| 161 |
title = info.get('title', 'video')
|
| 162 |
except Exception as e:
|
| 163 |
err = str(e)
|
| 164 |
+
if 'private' in err.lower():
|
|
|
|
|
|
|
| 165 |
raise Exception("This account is private. Only public posts can be downloaded.")
|
| 166 |
elif 'not found' in err.lower() or '404' in err:
|
| 167 |
raise Exception("Video not found. Check the URL and make sure the post still exists.")
|
| 168 |
else:
|
| 169 |
raise Exception(f"Download failed: {err}")
|
| 170 |
|
|
|
|
| 171 |
for f in os.listdir(tmp_dir):
|
| 172 |
if f.endswith('.mp3'):
|
| 173 |
return os.path.join(tmp_dir, f), title
|
| 174 |
raise Exception("Download succeeded but audio file not found.")
|
| 175 |
|
| 176 |
+
|
| 177 |
def transcribe(file, model_name, language, show_timestamps, translate):
|
| 178 |
if file is None:
|
| 179 |
return "Please upload a video or audio file.", ""
|
|
|
|
| 247 |
|
| 248 |
|
| 249 |
def download_video_only(url):
|
| 250 |
+
"""Download video — uses RapidAPI for Instagram, yt-dlp for others"""
|
| 251 |
if not url or not url.strip():
|
| 252 |
return None, "Please paste a valid URL."
|
| 253 |
url = url.strip()
|
|
|
|
| 255 |
'x.com','facebook.com','fb.watch']
|
| 256 |
if not any(s in url.lower() for s in supported):
|
| 257 |
return None, "Unsupported URL."
|
| 258 |
+
# Use RapidAPI for Instagram
|
| 259 |
+
if 'instagram.com' in url.lower():
|
| 260 |
+
try:
|
| 261 |
+
tmp_path, title = download_instagram_via_rapidapi(url)
|
| 262 |
+
return tmp_path, title
|
| 263 |
+
except Exception as e:
|
| 264 |
+
return None, str(e)
|
| 265 |
tmp_dir = tempfile.mkdtemp()
|
| 266 |
output_path = os.path.join(tmp_dir, 'video.%(ext)s')
|
| 267 |
ydl_opts = {
|
|
|
|
| 615 |
download_btn.click(fn=save_transcript, inputs=plain_output, outputs=download_file)
|
| 616 |
|
| 617 |
if __name__ == "__main__":
|
| 618 |
+
demo.launch(css=custom_css)
|