Ubuntu
commited on
Commit
·
46e4f4a
1
Parent(s):
57c0815
update the space
Browse files
app.py
CHANGED
@@ -14,19 +14,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 |
yt_dlp_command = [
|
18 |
"yt-dlp",
|
19 |
"-f", "bestaudio/best",
|
20 |
"-x", # Extract audio
|
21 |
"--audio-format", "mp3",
|
22 |
-
"
|
23 |
-
"-o", os.path.join(output_dir, "%(id)s.%(ext)s"),
|
24 |
-
"-v",
|
25 |
-
"--print", "after_move:filepath", # Print the output filepath
|
26 |
-
"--username", "oauth2",
|
27 |
-
"--password", "",
|
28 |
-
"--no-playlist",
|
29 |
-
"--print-json",
|
30 |
url
|
31 |
]
|
32 |
|
@@ -34,18 +28,13 @@ def download_youtube_audio(url: str, output_dir: Optional[str] = None) -> str:
|
|
34 |
print(' '.join(yt_dlp_command))
|
35 |
result = subprocess.run(yt_dlp_command, capture_output=True, text=True, check=True)
|
36 |
|
37 |
-
# Parse the JSON output to get video information
|
38 |
-
video_info = json.loads(result.stdout.splitlines()[-2]) # The last line is the filepath
|
39 |
-
|
40 |
# Get the output filepath from the last line of stdout
|
41 |
-
audio_file = result.stdout.splitlines()[-1].strip()
|
42 |
-
|
43 |
-
print(f"Successfully downloaded: {
|
44 |
return audio_file
|
45 |
except subprocess.CalledProcessError as e:
|
46 |
raise Exception(f"Error downloading YouTube audio: {e.stderr}")
|
47 |
-
except json.JSONDecodeError:
|
48 |
-
raise Exception("Error parsing video information")
|
49 |
except Exception as e:
|
50 |
raise Exception(f"Unexpected error: {str(e)}")
|
51 |
|
|
|
14 |
if output_dir is None:
|
15 |
output_dir = tempfile.gettempdir()
|
16 |
|
17 |
+
# Updated command to use a different method for downloading
|
18 |
yt_dlp_command = [
|
19 |
"yt-dlp",
|
20 |
"-f", "bestaudio/best",
|
21 |
"-x", # Extract audio
|
22 |
"--audio-format", "mp3",
|
23 |
+
"-o", os.path.join(output_dir, "%(title)s.%(ext)s"), # Changed output filename to use title
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
url
|
25 |
]
|
26 |
|
|
|
28 |
print(' '.join(yt_dlp_command))
|
29 |
result = subprocess.run(yt_dlp_command, capture_output=True, text=True, check=True)
|
30 |
|
|
|
|
|
|
|
31 |
# Get the output filepath from the last line of stdout
|
32 |
+
audio_file = os.path.join(output_dir, f"{result.stdout.splitlines()[-1].strip()}.mp3") # Adjusted to match new output
|
33 |
+
|
34 |
+
print(f"Successfully downloaded: {audio_file}")
|
35 |
return audio_file
|
36 |
except subprocess.CalledProcessError as e:
|
37 |
raise Exception(f"Error downloading YouTube audio: {e.stderr}")
|
|
|
|
|
38 |
except Exception as e:
|
39 |
raise Exception(f"Unexpected error: {str(e)}")
|
40 |
|