Ubuntu commited on
Commit
f595e63
1 Parent(s): 80c2167

use rapid api

Browse files
Files changed (2) hide show
  1. app.py +47 -17
  2. 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(url: str, output_dir: Optional[str] = None) -> str:
 
 
14
  if output_dir is None:
15
  output_dir = tempfile.gettempdir()
16
 
17
- ydl_opts = {
18
- 'format': 'bestaudio/best',
19
- 'extractaudio': True, # Download only audio
20
- 'audioformat': 'mp3', # Convert to mp3
21
- 'outtmpl': os.path.join(output_dir, '%(title)s.%(ext)s'), # Output filename
22
- 'noplaylist': True, # Only download single video, not playlist
 
 
 
 
23
  }
24
-
25
- try:
26
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
27
- info_dict = ydl.extract_info(url, download=True) # Download audio
28
- audio_file = os.path.join(output_dir, f"{info_dict['title']}.mp3") # Construct the file path
29
-
30
- print(f"Successfully downloaded: {audio_file}")
31
- return audio_file
32
- except Exception as e:
33
- raise Exception(f"Error downloading YouTube audio: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- youtube_dl
 
1
+ pydub