From 6670e6ca0ebe21d95940df71c8ac535ec3b056c4 Mon Sep 17 00:00:00 2001 From: Justin Donofrio Date: Thu, 7 Nov 2024 10:17:06 -0500 Subject: [PATCH] Output download to raw format before converting --- src/onthespot/api/deezer.py | 2 +- src/onthespot/downloader.py | 31 +++++++++++++++++++++---------- src/onthespot/gui/mainui.py | 7 +++---- src/onthespot/post_download.py | 4 ++-- src/onthespot/utils.py | 9 --------- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/onthespot/api/deezer.py b/src/onthespot/api/deezer.py index 37e92c6..56dde61 100644 --- a/src/onthespot/api/deezer.py +++ b/src/onthespot/api/deezer.py @@ -2,10 +2,10 @@ import json import html.parser import uuid +from binascii import a2b_hex, b2a_hex import requests from Cryptodome.Hash import MD5 from Cryptodome.Cipher import AES, Blowfish -from binascii import a2b_hex, b2a_hex from ..otsconfig import config from ..runtimedata import get_logger, account_pool from ..utils import conv_list_format, make_call diff --git a/src/onthespot/downloader.py b/src/onthespot/downloader.py index 121bbba..847e85f 100644 --- a/src/onthespot/downloader.py +++ b/src/onthespot/downloader.py @@ -93,7 +93,6 @@ def run(self): directory, file_name = os.path.split(file_path) temp_file_path = os.path.join(directory, '~' + file_name) - os.makedirs(os.path.dirname(file_path), exist_ok=True) item['file_path'] = file_path @@ -168,6 +167,8 @@ def run(self): try: if item_service == "spotify": account = account_pool[config.get('parsing_acc_sn')]['login']['session'] + default_format = ".ogg" + temp_file_path += default_format if item_type == "track": audio_key = TrackId.from_base62(item_id) elif item_type == "episode": @@ -193,19 +194,20 @@ def run(self): self.progress.emit(item, self.tr("Downloading"), int((downloaded / total_size) * 100)) if len(data) == 0: break - default_format = ".ogg" + bitrate = "320k" if quality == AudioQuality.VERY_HIGH else "160k" elif item_service == "soundcloud": + bitrate = "128k" + default_format = ".mp3" + temp_file_path += default_format + # Don't know how to emit progress from ffmpeg command = [config.get('_ffmpeg_bin_path'), "-loglevel", "error", "-i", f"{item_metadata['file_url']}", "-c", "copy", temp_file_path] if os.name == 'nt': subprocess.check_call(command, shell=False, creationflags=subprocess.CREATE_NO_WINDOW) else: subprocess.check_call(command, shell=False) - default_format = ".mp3" - bitrate = "128k" - elif item_service == 'deezer': song = get_song_info_from_deezer_website(item['item_id']) @@ -226,6 +228,7 @@ def run(self): song_quality = 5 song_format = 'MP3_256' bitrate = "256k" + temp_file_path += default_format headers = { 'Origin': 'https://www.deezer.com', @@ -307,22 +310,30 @@ def run(self): if isinstance(extra_metadata, dict): item_metadata.update(extra_metadata) + if config.get('force_raw'): + file_path += default_format + elif item_type == "track": + file_path += "." + config.get("media_format") + elif item_type == "episode": + file_path += "." + config.get("podcast_media_format") + + os.rename(temp_file_path, file_path) + # Convert file format and embed metadata if not config.get('force_raw'): + item['item_status'] = 'Converting' if self.gui: self.progress.emit(item, self.tr("Converting"), 99) - convert_audio_format(temp_file_path, item_metadata, bitrate, default_format) + + convert_audio_format(file_path, item_metadata, bitrate, default_format) # Thumbnail if config.get('save_album_cover') or config.get('embed_cover'): item['item_status'] = 'Setting Thumbnail' if self.gui: self.progress.emit(item, self.tr("Setting Thumbnail"), 99) - set_music_thumbnail(temp_file_path, item_metadata) - - # Temp file finished, convert to regular format - os.rename(temp_file_path, file_path) + set_music_thumbnail(file_path, item_metadata) item['item_status'] = 'Downloaded' logger.info("Item Successfully Downloaded") diff --git a/src/onthespot/gui/mainui.py b/src/onthespot/gui/mainui.py index cbfd28e..e620786 100644 --- a/src/onthespot/gui/mainui.py +++ b/src/onthespot/gui/mainui.py @@ -170,7 +170,6 @@ def bind_button_inputs(self): self.inp_download_queue_show_cancelled.stateChanged.connect(self.update_table_visibility) self.inp_download_queue_show_completed.stateChanged.connect(self.update_table_visibility) - self.inp_download_queue_show_waiting.stateChanged.connect(self.update_table_visibility) self.inp_download_queue_show_failed.stateChanged.connect(self.update_table_visibility) self.inp_download_queue_show_cancelled.stateChanged.connect(self.update_table_visibility) @@ -181,7 +180,7 @@ def set_table_props(self): window_width = self.width() logger.info(f"Setting table item properties {window_width}") # Sessions table - self.tbl_sessions.setSortingEnabled(True) + #self.tbl_sessions.setSortingEnabled(True) self.tbl_sessions.horizontalHeader().setSectionsMovable(True) self.tbl_sessions.horizontalHeader().setSectionsClickable(True) self.tbl_sessions.horizontalHeader().resizeSection(0, 16) @@ -192,7 +191,7 @@ def set_table_props(self): self.tbl_sessions.horizontalHeader().setSectionResizeMode(5, QHeaderView.ResizeMode.Stretch) self.tbl_sessions.horizontalHeader().setSectionResizeMode(6, QHeaderView.ResizeMode.Stretch) # Search results table - self.tbl_search_results.setSortingEnabled(True) + #self.tbl_search_results.setSortingEnabled(True) self.tbl_search_results.horizontalHeader().setSectionsMovable(True) self.tbl_search_results.horizontalHeader().setSectionsClickable(True) self.tbl_search_results.horizontalHeader().setSectionResizeMode(0, QHeaderView.ResizeMode.Stretch) @@ -201,7 +200,7 @@ def set_table_props(self): self.tbl_search_results.horizontalHeader().setSectionResizeMode(3, QHeaderView.ResizeMode.Stretch) self.tbl_search_results.horizontalHeader().setSectionResizeMode(4, QHeaderView.ResizeMode.Stretch) # Download progress table - self.tbl_dl_progress.setSortingEnabled(True) + #self.tbl_dl_progress.setSortingEnabled(True) self.tbl_dl_progress.horizontalHeader().setSectionsMovable(True) self.tbl_dl_progress.horizontalHeader().setSectionsClickable(True) if config.get("debug_mode"): diff --git a/src/onthespot/post_download.py b/src/onthespot/post_download.py index 7294a6b..64600c5 100644 --- a/src/onthespot/post_download.py +++ b/src/onthespot/post_download.py @@ -17,7 +17,7 @@ def convert_audio_format(filename, metadata, bitrate, default_format): filetype = os.path.splitext(file_name)[1] file_stem = os.path.splitext(file_name)[0] - temp_name = os.path.join(os.path.dirname(target_path), "." + file_stem + filetype) + temp_name = os.path.join(os.path.dirname(target_path), "~" + file_stem + filetype) if os.path.isfile(temp_name): os.remove(temp_name) @@ -200,7 +200,7 @@ def set_music_thumbnail(filename, metadata): filetype = os.path.splitext(file_name)[1] file_stem = os.path.splitext(file_name)[0] - temp_name = os.path.join(os.path.dirname(target_path), "." + file_stem + filetype) + temp_name = os.path.join(os.path.dirname(target_path), "~" + file_stem + filetype) # Fetch thumbnail image_path = os.path.join(os.path.dirname(filename), 'cover') diff --git a/src/onthespot/utils.py b/src/onthespot/utils.py index fda323e..abb6042 100644 --- a/src/onthespot/utils.py +++ b/src/onthespot/utils.py @@ -123,13 +123,4 @@ def format_track_path(item_metadata, item_service, item_type, parent_category, p playlist_owner=sanitize_data(playlist_by), ) - if item_service == 'soundcloud' and config.get("force_raw"): - item_path += ".mp3" - elif item_service == 'spotify' and config.get("force_raw"): - item_path += ".ogg" - elif item_type == 'track': - item_path += "." + config.get("media_format") - elif item_type == 'episode': - item_path += "." + config.get("podcast_media_format") - return item_path