Compare commits

..

No commits in common. "8e5d9d990071c4e301d22f0c014283833719a6ef" and "8471db3ea16597db61ae01b8432eb41a152ee473" have entirely different histories.

View File

@ -10,7 +10,7 @@ import ffmpy
import argparse
# multiprocessing stuff
from multiprocessing import Pool, Value, parent_process
from multiprocessing import Pool
# executing some commands
import subprocess
@ -27,7 +27,6 @@ from random import randint
# typing hints
from typing import Any, Optional
# temporary file/directory management
import tempfile
# working with sound files
@ -39,9 +38,6 @@ import pyloudnorm
# file copy
import shutil
# signal handling
import signal
"""
Normalize loudness of all music files in a given directory and its subdirectories.
"""
@ -49,18 +45,6 @@ Normalize loudness of all music files in a given directory and its subdirectorie
musicfile_extensions = (".flac", ".wav", ".mp3", ".m4a", ".aac", ".opus")
class CleanupRequired(Exception):
pass
def sigint_handler(signum, frame):
# set workers to clean up
cleanup_required.value = 1
# Output only once
if parent_process() is None:
print("\nReceived KeyboardInterrupt. Process cleaning up and stopping...")
def loudnorm(inputfile: str, outputfile: str):
"""
Normalize audio to EBU R 128 standard using pyloudnorm
@ -75,10 +59,6 @@ def loudnorm(inputfile: str, outputfile: str):
meter = pyloudnorm.Meter(rate=rate)
loudness = meter.integrated_loudness(data=data)
# cleanup check
if bool(cleanup_required.value):
raise CleanupRequired()
# normalize audio
file_normalized = pyloudnorm.normalize.loudness(
data=data, input_loudness=loudness, target_loudness=-18.0
@ -96,9 +76,6 @@ def ffmpeg_to_wav(inputfile: str, outputfile: str):
inputfile (str): Path to input file
outputfile (str): Path to output file
"""
# cleanup check
if bool(cleanup_required.value):
raise CleanupRequired()
# convert to wav in temporary directory
with tempfile.TemporaryDirectory() as tempdir:
@ -122,10 +99,6 @@ def ffmpeg_to_wav(inputfile: str, outputfile: str):
subprocess.run(ff.cmd, shell=True, capture_output=True)
# cleanup check
if bool(cleanup_required.value):
raise CleanupRequired()
# normalize loudness
loudnorm(inputfile=temp_input, outputfile=temp_output)
@ -134,10 +107,6 @@ def ffmpeg_to_wav(inputfile: str, outputfile: str):
outputfile: "-c:a libopus" " " "-b:a 192k" " " "-compression_level 10"
}
# cleanup check
if bool(cleanup_required.value):
raise CleanupRequired()
ff = ffmpy.FFmpeg(
inputs={temp_output: None}, outputs=outputcmd, global_options=("-y")
)
@ -154,9 +123,6 @@ def ffmpeg_copy_metadata(inputfile: str, outputfile: str):
inputfile (str): Path to input file
outputfile (str): Path to output file
"""
# cleanup check
if bool(cleanup_required.value):
raise CleanupRequired()
# store output file as temporary file. FFMPEG can't work on files in-place
with tempfile.NamedTemporaryFile() as temp_audio:
@ -188,13 +154,10 @@ def main(inputfile: str) -> Optional[list[Any]]:
Output:
dynamically normalised audio files (list)
"""
# set output folder to parent path + "normalized"
outputfolder = os.path.join(os.path.dirname(inputfile), "normalized")
# cleanup check
if bool(cleanup_required.value):
raise CleanupRequired()
# NOTE create output folder
# because multiple parallel processes are at work here,
# there might be conflicts with one trying to create the directory although it already exists
@ -238,11 +201,6 @@ if __name__ == "__main__":
"""
Handle arguments and other details for interactive usage
"""
# global cleanup variable
cleanup_required = Value("i", 0)
# handle KeyboardInterrupt
signal.signal(signal.SIGINT, sigint_handler)
# start time of program
starttime = time.time()
@ -284,6 +242,9 @@ if __name__ == "__main__":
# file where last run timestamp is stored
timefile = os.path.join(srcfolder, "run.time")
# list of non-linear normalizations
nonlinear_all: Optional[list[Any]] = []
# get time of previous run
if reset:
timeprev = 0
@ -304,11 +265,8 @@ if __name__ == "__main__":
if os.path.getmtime(filepath) >= float(timeprev):
musicfiles.append(os.path.join(root, file))
# process pool
with Pool(cpu) as p:
result = p.map_async(main, musicfiles)
# wait for all processes to finish
result.wait()
nonlinear_all: Optional[list[Any]] = p.map(main, musicfiles)
# write this run's time into file
with open(timefile, "w") as file: