Ubuntu
commited on
Commit
•
f595e63
1
Parent(s):
80c2167
use rapid api
Browse files- app.py +47 -17
- requirements.txt +1 -1
app.py
CHANGED
@@ -6,31 +6,61 @@ import json
|
|
6 |
import subprocess
|
7 |
import os
|
8 |
import tempfile # Import tempfile
|
|
|
9 |
|
10 |
# Define the FastAPI URL
|
11 |
API_URL = "http://astarwiz.com:9998"
|
|
|
12 |
|
13 |
-
def download_youtube_audio(
|
|
|
|
|
14 |
if output_dir is None:
|
15 |
output_dir = tempfile.gettempdir()
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
23 |
}
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
def run_asr(audio_file, youtube_url):
|
36 |
temp_file = None
|
|
|
6 |
import subprocess
|
7 |
import os
|
8 |
import tempfile # Import tempfile
|
9 |
+
from pydub import AudioSegment # Import AudioSegment
|
10 |
|
11 |
# Define the FastAPI URL
|
12 |
API_URL = "http://astarwiz.com:9998"
|
13 |
+
rapid_key = os.environ.get("RAPID_API_KEY")
|
14 |
|
15 |
+
def download_youtube_audio(metadata, output_dir: Optional[str] = None) -> str:
|
16 |
+
video_id = metadata['id']
|
17 |
+
|
18 |
if output_dir is None:
|
19 |
output_dir = tempfile.gettempdir()
|
20 |
|
21 |
+
output_filename = os.path.join(output_dir, f"{video_id}.mp3")
|
22 |
+
|
23 |
+
if os.path.exists(output_filename):
|
24 |
+
return output_filename # Return if the file already exists
|
25 |
+
|
26 |
+
url = "https://youtube86.p.rapidapi.com/api/youtube/links"
|
27 |
+
headers = {
|
28 |
+
'Content-Type': 'application/json',
|
29 |
+
'x-rapidapi-host': 'youtube86.p.rapidapi.com',
|
30 |
+
'x-rapidapi-key': rapid_key # Replace <key> with your actual API key
|
31 |
}
|
32 |
+
data = {
|
33 |
+
"url": metadata['url']
|
34 |
+
}
|
35 |
+
|
36 |
+
response = requests.post(url, headers=headers, json=data)
|
37 |
+
print('Fetched audio links')
|
38 |
+
|
39 |
+
if response.status_code == 200:
|
40 |
+
result = response.json()
|
41 |
+
for url in result[0]['urls']:
|
42 |
+
if url.get('isBundle'):
|
43 |
+
audio_url = url['url']
|
44 |
+
extension = url['extension']
|
45 |
+
audio_response = requests.get(audio_url)
|
46 |
+
|
47 |
+
if audio_response.status_code == 200:
|
48 |
+
temp_filename = os.path.join(output_dir, f"{video_id}.{extension}")
|
49 |
+
with open(temp_filename, 'wb') as audio_file:
|
50 |
+
audio_file.write(audio_response.content)
|
51 |
+
|
52 |
+
# Convert to MP3 and downsample to 16000 Hz
|
53 |
+
audio = AudioSegment.from_file(temp_filename, format=extension)
|
54 |
+
audio = audio.set_frame_rate(16000)
|
55 |
+
audio.export(output_filename, format="mp3", parameters=["-ar", "16000"])
|
56 |
+
|
57 |
+
os.remove(temp_filename) # Remove the temporary file
|
58 |
+
return output_filename # Return the final MP3 filename
|
59 |
+
|
60 |
+
return None # Return None if no successful download occurs
|
61 |
+
else:
|
62 |
+
print("Error:", response.status_code, response.text)
|
63 |
+
return None # Return None on failure
|
64 |
|
65 |
def run_asr(audio_file, youtube_url):
|
66 |
temp_file = None
|
requirements.txt
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
pydub
|