Skip to content

Commit

Permalink
Add Bitrate Validation To Downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrluckechuck committed Jan 25, 2025
1 parent 1f0dd3f commit 242dfb2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jinja2
pybase62
pylint-django
pyyaml
tinytag
# spotdl
git+https://github.com/Kyrluckechuck/spotify-downloader.git@add-yt-dlp-extractor-support#egg=spotdl

22 changes: 20 additions & 2 deletions spotify_library_sync/downloader/spotdl_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import asyncio
from argparse import Namespace
import logging
import pathlib
import traceback
from tinytag import TinyTag

from . import __version__
from . import utils
Expand All @@ -29,6 +31,9 @@
from spotdl.utils.config import create_settings
from spotdl.utils.spotify import SpotifyClient

class BitrateException(Exception):
pass

# Apply monkeypatches to Spotdl for compatibility
# See spotdl_override module for more information
Spotdl.__init__ = spotdl_override.__init__
Expand Down Expand Up @@ -203,10 +208,23 @@ def execute(
song_success, output_path = self.spotdl.download(SpotdlSong.from_url(track['external_urls']['spotify']))
if (song_success is None or output_path is None):
raise Exception("Failed to download correctly")

# Validate the song is in the correct audio bitrate
# Validate premium successfully applied, for example
expected_bitrate = 127 if config.cookies_location is not None and config.po_token is not None else 255

tag = TinyTag.get(output_path)
if (tag.bitrate < expected_bitrate):
pathlib.Path.unlink(output_path)
raise BitrateException(f"File was downloaded successfully but not in the correct bitrate ({tag.bitrate} found, but {expected_bitrate} is minimum expected)")
except BitrateException as exception:
self.logger.erorr("Critical bitrate exception occurred, halting download queue")
self.logger.error(f'({current_track}) Failed to download "{track["name"]}"')
self.logger.error(f"exception: {exception}")
raise exception
except Exception as exception:
error_count += 1
self.logger.error(
f'({current_track}) Failed to download "{track["name"]}"')
self.logger.error(f'({current_track}) Failed to download "{track["name"]}"')
self.logger.error(f"exception: {exception}")
if (config.print_exceptions):
self.logger.error(traceback.format_exc())
Expand Down

0 comments on commit 242dfb2

Please sign in to comment.