From 36ee45661203375829d28403800c3facc72f4843 Mon Sep 17 00:00:00 2001 From: Hellow Date: Wed, 5 Apr 2023 17:40:22 +0200 Subject: [PATCH] added convertion of the audio itself vie ffmpeg --- requirements.txt | 3 +- src/actual_donwload.py | 3 +- src/music_kraken/audio/codec.py | 54 +++++++++---------- src/music_kraken/pages/abstract.py | 3 +- .../pages/support_classes/default_target.py | 5 +- src/music_kraken/utils/shared.py | 32 +++++++++-- 6 files changed, 61 insertions(+), 39 deletions(-) diff --git a/requirements.txt b/requirements.txt index 199b902..bea15a9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,5 @@ pandoc~=2.3 SQLAlchemy setuptools~=60.2.0 tqdm~=4.65.0 -peewee~=3.15.4 \ No newline at end of file +peewee~=3.15.4 +ffmpeg-python~=0.2.0 \ No newline at end of file diff --git a/src/actual_donwload.py b/src/actual_donwload.py index 610aa21..2c2888f 100644 --- a/src/actual_donwload.py +++ b/src/actual_donwload.py @@ -43,6 +43,7 @@ def download_audio(): pages.Musify.download_song(song) + def real_download(): search = pages.Search() search.search_url("https://musify.club/release/children-of-the-night-2018-1079829") @@ -50,4 +51,4 @@ def real_download(): if __name__ == "__main__": - music_kraken.cli(genre="test") + music_kraken.cli(genre="dsbm") diff --git a/src/music_kraken/audio/codec.py b/src/music_kraken/audio/codec.py index b0adc55..3a8304f 100644 --- a/src/music_kraken/audio/codec.py +++ b/src/music_kraken/audio/codec.py @@ -2,37 +2,33 @@ from pathlib import Path import ffmpeg -from ..utils.shared import BITRATE, CODEX_LOGGER as LOGGER +from ..utils.shared import BITRATE, AUDIO_FORMAT, CODEX_LOGGER as LOGGER from ..objects import Target -def correct_codec(target: Target, bitrate: int = BITRATE): + +def correct_codec(target: Target, bitrate_kb: int = BITRATE, audio_format: str = AUDIO_FORMAT): if not target.exists: LOGGER.warning(f"Target doesn't exist: {target.file_path}") - - output_file = Path(str(target.file_path) + ".out") - - # https://www.audioranger.com/audio-formats.php - # https://kkroening.github.io/ffmpeg-python/index.html?highlight=audio#ffmpeg.output - - ffmpeg_out = ffmpeg.output( - ffmpeg.input(target.file_path), - output_file, - audio_bitrate=bitrate, - format="mp3" - ).run() - """ - -in_file = ffmpeg.input('input.mp4') -overlay_file = ffmpeg.input('overlay.png') -( - ffmpeg - .concat( - in_file.trim(start_frame=10, end_frame=20), - in_file.trim(start_frame=30, end_frame=40), + return + + bitrate_b = int(bitrate_kb / 1024) + + output_target = Target( + path=target._path, + file=str(target._file) + "." + audio_format ) - .overlay(overlay_file.hflip()) - .drawbox(50, 50, 120, 120, color='red', thickness=5) - .output('out.mp4') - .run() -) - """ + + stream = ffmpeg.input(target.file_path) + stream = stream.audio + stream = ffmpeg.output( + stream, + str(output_target.file_path), + audio_bitrate=bitrate_b, + format=audio_format + ) + out, err = ffmpeg.run(stream, quiet=True, overwrite_output=True) + if err != "": + LOGGER.debug(err) + + output_target.copy_content(target) + output_target.file_path.unlink() diff --git a/src/music_kraken/pages/abstract.py b/src/music_kraken/pages/abstract.py index 3758139..95c0ebf 100644 --- a/src/music_kraken/pages/abstract.py +++ b/src/music_kraken/pages/abstract.py @@ -21,7 +21,7 @@ from ..objects import ( Label, AlbumType ) -from ..audio import write_metadata_to_target +from ..audio import write_metadata_to_target, correct_codec from ..utils import shared @@ -541,6 +541,7 @@ class Page: @classmethod def _post_process_targets(cls, song: Song, temp_target: Target) -> DownloadResult: + correct_codec(temp_target) write_metadata_to_target(song.metadata, temp_target) r = DownloadResult() diff --git a/src/music_kraken/pages/support_classes/default_target.py b/src/music_kraken/pages/support_classes/default_target.py index e0b0d92..f5417df 100644 --- a/src/music_kraken/pages/support_classes/default_target.py +++ b/src/music_kraken/pages/support_classes/default_target.py @@ -19,6 +19,7 @@ class DefaultTarget: album: str = DEFAULT_VALUES["album"] album_type: str = DEFAULT_VALUES["album_type"] song: str = DEFAULT_VALUES["song"] + audio_format: str = DEFAULT_VALUES["audio_format"] def __setattr__(self, __name: str, __value: str) -> None: if __name in DEFAULT_VALUES: @@ -36,9 +37,9 @@ class DefaultTarget: return Target( relative_to_music_dir=True, path=DOWNLOAD_PATH.format(genre=self.genre, label=self.label, artist=self.artist, album=self.album, - song=self.song, album_type=self.album_type), + song=self.song, album_type=self.album_type, audio_format=self.audio_format), file=DOWNLOAD_FILE.format(genre=self.genre, label=self.label, artist=self.artist, album=self.album, - song=self.song, album_type=self.album_type) + song=self.song, album_type=self.album_type, audio_format=self.audio_format) ) def song_object(self, song: Song): diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 813c64f..a11fbfb 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Set import logging import tempfile import os @@ -94,6 +94,28 @@ proxies = { 'https': 'socks5h://127.0.0.1:9150' } if TOR else {} + +# Only the formats with id3 metadata can be used +# https://www.audioranger.com/audio-formats.php +# https://web.archive.org/web/20230322234434/https://www.audioranger.com/audio-formats.php +ALLOWED_FILE_FORMATS: Set[str] = { + "mp3", "mp2", "mp1", # MPEG-1 ID3.2 + "wav", "wave", "rmi", # RIFF (including WAV) ID3.2 + "aiff", "aif", "aifc", # AIFF ID3.2 + "aac", "aacp", # Raw AAC ID3.2 + "tta", # True Audio ID3.2 + "ape", # Monkey's Audio ID3.1 + "mpc", "mpp", "mp+", # MusePack ID3.1 + "wv", # WavPack ID3.1 + "ofr", "ofs" # OptimFrog ID3.1 +} + +# kB per second +BITRATE = 125 +AUDIO_FORMAT = "mp3" +if AUDIO_FORMAT not in ALLOWED_FILE_FORMATS: + raise ValueError(f"The Audio Format is not in {ALLOWED_FILE_FORMATS} ({AUDIO_FORMAT}).") + """ available variables: - genre @@ -104,17 +126,17 @@ available variables: - album_type """ DOWNLOAD_PATH = "{genre}/{artist}/{album_type}/{album}" -DOWNLOAD_FILE = "{song}.mp3" +DOWNLOAD_FILE = "{song}.{audio_format}" DEFAULT_VALUES = { "genre": "Various Genre", "label": "Various Labels", "artist": "Various Artists", "album": "Various Album", "song": "Various Song", - "album_type": "Other" + "album_type": "Other", + "audio_format": AUDIO_FORMAT } -# kB per second -BITRATE = 100 + # size of the chunks that are streamed CHUNK_SIZE = 1024