-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: music download api using yt-dlp instead of pytube
- Loading branch information
1 parent
abdf9a3
commit 5c86036
Showing
9 changed files
with
225 additions
and
29 deletions.
There are no files selected for viewing
Binary file modified
BIN
+0 Bytes
(100%)
backend/user/api/v1/__pycache__/app.cpython-310.pyc
100755 → 100644
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
2024-07-11 05:14:52,072 - INFO - Music link for query 'Halo Beyonce': https://www.youtube.com/watch?v=bnVUHWCynig | ||
2024-07-11 05:15:01,385 - ERROR - Exception on /api/v1/user/music/download [POST] | ||
Traceback (most recent call last): | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/__main__.py", line 181, in fmt_streams | ||
extract.apply_signature(stream_manifest, self.vid_info, self.js) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/extract.py", line 409, in apply_signature | ||
cipher = Cipher(js=js) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/cipher.py", line 43, in __init__ | ||
self.throttling_plan = get_throttling_plan(js) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/cipher.py", line 405, in get_throttling_plan | ||
raw_code = get_throttling_function_code(js) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code | ||
name = re.escape(get_throttling_function_name(js)) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/cipher.py", line 296, in get_throttling_function_name | ||
raise RegexMatchError( | ||
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple | ||
|
||
During handling of the above exception, another exception occurred: | ||
|
||
Traceback (most recent call last): | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/flask/app.py", line 1473, in wsgi_app | ||
response = self.full_dispatch_request() | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/flask/app.py", line 882, in full_dispatch_request | ||
rv = self.handle_user_exception(e) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/flask_cors/extension.py", line 161, in wrapped_function | ||
return cors_after_request(app.make_response(f(*args, **kwargs))) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/flask/app.py", line 880, in full_dispatch_request | ||
rv = self.dispatch_request() | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/flask/app.py", line 865, in dispatch_request | ||
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] | ||
File "/home/johnmkagunda/repositories/Portfolio-website/backend/user/api/v1/views/recommendations_download.py", line 31, in download_music | ||
audio_stream = yt.streams.filter(only_audio=True).first() | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/__main__.py", line 296, in streams | ||
return StreamQuery(self.fmt_streams) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/__main__.py", line 188, in fmt_streams | ||
extract.apply_signature(stream_manifest, self.vid_info, self.js) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/extract.py", line 409, in apply_signature | ||
cipher = Cipher(js=js) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/cipher.py", line 43, in __init__ | ||
self.throttling_plan = get_throttling_plan(js) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/cipher.py", line 405, in get_throttling_plan | ||
raw_code = get_throttling_function_code(js) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/cipher.py", line 311, in get_throttling_function_code | ||
name = re.escape(get_throttling_function_name(js)) | ||
File "/home/johnmkagunda/.local/lib/python3.10/site-packages/pytube/cipher.py", line 296, in get_throttling_function_name | ||
raise RegexMatchError( | ||
pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
4 changes: 4 additions & 0 deletions
4
backend/user/external_apis/music_downloader/music_download_link.log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
2024-05-23 22:21:11,716 - ERROR - regex_search: could not find match for (?:v=|\/)([0-9A-Za-z_-]{11}).* | ||
2024-07-11 05:21:36,974 - ERROR - get_throttling_function_name: could not find match for multiple | ||
2024-07-11 05:31:19,587 - ERROR - get_throttling_function_name: could not find match for multiple | ||
2024-07-11 05:40:08,317 - ERROR - [0;31mERROR:[0m Postprocessing: ffprobe and ffmpeg not found. Please install or provide the path using --ffmpeg-location | ||
2024-07-11 05:42:52,552 - ERROR - [0;31mERROR:[0m [generic] '#!/usr/bin/python3' is not a valid URL. Set --default-search "ytsearch" (or run yt-dlp "ytsearch:#!/usr/bin/python3" ) to search YouTube |
47 changes: 28 additions & 19 deletions
47
backend/user/external_apis/music_downloader/music_download_link.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,42 @@ | ||
#!/usr/bin/python3 | ||
"""Downloads the audio of a YouTube video given its URL.""" | ||
"""Provides a direct link to download the audio of a YouTube video given its URL.""" | ||
import logging | ||
from pytube import YouTube | ||
import yt_dlp | ||
|
||
def music_download_link(url): | ||
"""downloads the audio of a YouTube video given its URL.""" | ||
if url == "" or url is None: | ||
def get_audio_download_link(url): | ||
"""Gets the direct download link of the audio of a YouTube video given its URL.""" | ||
if not url: | ||
raise ValueError("Invalid URL") | ||
try: | ||
# Create a YouTube object with the given URL | ||
video = YouTube(url) | ||
ydl_opts = { | ||
'format': 'bestaudio/best', | ||
'quiet': True, | ||
'extract_flat': True, | ||
'postprocessors': [{ | ||
'key': 'FFmpegExtractAudio', | ||
'preferredcodec': 'mp3', | ||
'preferredquality': '192', | ||
}], | ||
} | ||
|
||
# Get the audio stream with the highest bitrate | ||
audio_stream = video.streams.get_audio_only() | ||
|
||
# Download the audio stream | ||
audio_stream.download() | ||
# Return the path of the downloaded audio file | ||
return audio_stream.default_filename | ||
# pylint: disable=broad-except | ||
with yt_dlp.YoutubeDL(ydl_opts) as ydl: | ||
info_dict = ydl.extract_info(url, download=False) | ||
audio_url = info_dict['url'] | ||
return audio_url | ||
except Exception as e: | ||
# Configure logging | ||
logging.basicConfig(filename='./music_download_link.log', | ||
level=logging.ERROR, | ||
format='%(asctime)s - %(levelname)s - %(message)s') | ||
level=logging.ERROR, | ||
format='%(asctime)s - %(levelname)s - %(message)s') | ||
|
||
# Log the error | ||
logging.error(str(e)) | ||
return None | ||
|
||
if __name__=="__main__": | ||
if __name__ == "__main__": | ||
download_url = input("Enter the URL of the YouTube video: ") | ||
music_download_link(download_url) | ||
audio_link = get_audio_download_link(download_url) | ||
if audio_link: | ||
print(f"Direct download link for audio: {audio_link}") | ||
else: | ||
print("Failed to get the download link for the audio.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ python-dotenv>=1.0.1 | |
bs4>=0.0.1 | ||
selenium>=4.19.0 | ||
webdriver-manager>=4.0.1 | ||
yt-dlp>=2024.4.9 |