pop2piano / utils.py
sweetcocoa's picture
update libs
5332e66
raw
history blame
1.91 kB
import numpy as np
import pydub
import yt_dlp
import yt_dlp.options
def mp3_write(f: str, sr: int, x: np.ndarray, normalized: bool = False):
channels = 2 if (x.ndim == 2 and x.shape[1] == 2) else 1
if normalized: # normalized array - each item should be a float in [-1, 1)
y = np.int16(x * 2**15)
else:
y = np.int16(x)
song = pydub.AudioSegment(
y.tobytes(), frame_rate=sr, sample_width=2, channels=channels
)
song.export(f, format="mp3", bitrate="256k")
def normalize(
audio: np.ndarray, min_y: float = -1.0, max_y: float = 1.0, eps: float = 1e-8
):
max_y -= eps
min_y += eps
amax = audio.max()
amin = audio.min()
audio = (max_y - min_y) * (audio - amin) / (amax - amin) + min_y
return audio
# yt_dlp script copied from https://github.com/yt-dlp/yt-dlp/blob/28d485714fef88937c82635438afba5db81f9089/devscripts/cli_to_api.py
create_parser = yt_dlp.options.create_parser
def parse_patched_options(opts):
patched_parser = create_parser()
patched_parser.defaults.update(
{
"ignoreerrors": False,
"retries": 0,
"fragment_retries": 0,
"extract_flat": False,
"concat_playlist": "never",
}
)
yt_dlp.options.create_parser = lambda: patched_parser
try:
return yt_dlp.parse_options(opts)
finally:
yt_dlp.options.create_parser = create_parser
default_opts = parse_patched_options([]).ydl_opts
def cli_to_api(opts, cli_defaults=False):
opts = (yt_dlp.parse_options if cli_defaults else parse_patched_options)(
opts
).ydl_opts
diff = {k: v for k, v in opts.items() if default_opts[k] != v}
if "postprocessors" in diff:
diff["postprocessors"] = [
pp
for pp in diff["postprocessors"]
if pp not in default_opts["postprocessors"]
]
return diff