added convertion of the audio itself vie ffmpeg

This commit is contained in:
Hellow 2023-04-05 17:40:22 +02:00
parent 4efef86793
commit 36ee456612
6 changed files with 61 additions and 39 deletions

View File

@ -12,3 +12,4 @@ SQLAlchemy
setuptools~=60.2.0 setuptools~=60.2.0
tqdm~=4.65.0 tqdm~=4.65.0
peewee~=3.15.4 peewee~=3.15.4
ffmpeg-python~=0.2.0

View File

@ -43,6 +43,7 @@ def download_audio():
pages.Musify.download_song(song) pages.Musify.download_song(song)
def real_download(): def real_download():
search = pages.Search() search = pages.Search()
search.search_url("https://musify.club/release/children-of-the-night-2018-1079829") search.search_url("https://musify.club/release/children-of-the-night-2018-1079829")
@ -50,4 +51,4 @@ def real_download():
if __name__ == "__main__": if __name__ == "__main__":
music_kraken.cli(genre="test") music_kraken.cli(genre="dsbm")

View File

@ -2,37 +2,33 @@ from pathlib import Path
import ffmpeg 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 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: if not target.exists:
LOGGER.warning(f"Target doesn't exist: {target.file_path}") LOGGER.warning(f"Target doesn't exist: {target.file_path}")
return
output_file = Path(str(target.file_path) + ".out") bitrate_b = int(bitrate_kb / 1024)
# https://www.audioranger.com/audio-formats.php output_target = Target(
# https://kkroening.github.io/ffmpeg-python/index.html?highlight=audio#ffmpeg.output path=target._path,
file=str(target._file) + "." + audio_format
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),
) )
.overlay(overlay_file.hflip())
.drawbox(50, 50, 120, 120, color='red', thickness=5) stream = ffmpeg.input(target.file_path)
.output('out.mp4') stream = stream.audio
.run() 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()

View File

@ -21,7 +21,7 @@ from ..objects import (
Label, Label,
AlbumType AlbumType
) )
from ..audio import write_metadata_to_target from ..audio import write_metadata_to_target, correct_codec
from ..utils import shared from ..utils import shared
@ -541,6 +541,7 @@ class Page:
@classmethod @classmethod
def _post_process_targets(cls, song: Song, temp_target: Target) -> DownloadResult: def _post_process_targets(cls, song: Song, temp_target: Target) -> DownloadResult:
correct_codec(temp_target)
write_metadata_to_target(song.metadata, temp_target) write_metadata_to_target(song.metadata, temp_target)
r = DownloadResult() r = DownloadResult()

View File

@ -19,6 +19,7 @@ class DefaultTarget:
album: str = DEFAULT_VALUES["album"] album: str = DEFAULT_VALUES["album"]
album_type: str = DEFAULT_VALUES["album_type"] album_type: str = DEFAULT_VALUES["album_type"]
song: str = DEFAULT_VALUES["song"] song: str = DEFAULT_VALUES["song"]
audio_format: str = DEFAULT_VALUES["audio_format"]
def __setattr__(self, __name: str, __value: str) -> None: def __setattr__(self, __name: str, __value: str) -> None:
if __name in DEFAULT_VALUES: if __name in DEFAULT_VALUES:
@ -36,9 +37,9 @@ class DefaultTarget:
return Target( return Target(
relative_to_music_dir=True, relative_to_music_dir=True,
path=DOWNLOAD_PATH.format(genre=self.genre, label=self.label, artist=self.artist, album=self.album, 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, 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): def song_object(self, song: Song):

View File

@ -1,4 +1,4 @@
from typing import List from typing import List, Set
import logging import logging
import tempfile import tempfile
import os import os
@ -94,6 +94,28 @@ proxies = {
'https': 'socks5h://127.0.0.1:9150' 'https': 'socks5h://127.0.0.1:9150'
} if TOR else {} } 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: available variables:
- genre - genre
@ -104,17 +126,17 @@ available variables:
- album_type - album_type
""" """
DOWNLOAD_PATH = "{genre}/{artist}/{album_type}/{album}" DOWNLOAD_PATH = "{genre}/{artist}/{album_type}/{album}"
DOWNLOAD_FILE = "{song}.mp3" DOWNLOAD_FILE = "{song}.{audio_format}"
DEFAULT_VALUES = { DEFAULT_VALUES = {
"genre": "Various Genre", "genre": "Various Genre",
"label": "Various Labels", "label": "Various Labels",
"artist": "Various Artists", "artist": "Various Artists",
"album": "Various Album", "album": "Various Album",
"song": "Various Song", "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 # size of the chunks that are streamed
CHUNK_SIZE = 1024 CHUNK_SIZE = 1024