Skip to content

Commit

Permalink
Output download to raw format before converting
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Nov 7, 2024
1 parent 84709ad commit 6670e6c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/onthespot/api/deezer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 21 additions & 10 deletions src/onthespot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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'])

Expand All @@ -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',
Expand Down Expand Up @@ -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")
Expand Down
7 changes: 3 additions & 4 deletions src/onthespot/gui/mainui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"):
Expand Down
4 changes: 2 additions & 2 deletions src/onthespot/post_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
9 changes: 0 additions & 9 deletions src/onthespot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6670e6c

Please sign in to comment.