import argparse import gc import hashlib import json import os import shlex import subprocess from contextlib import suppress from urllib.parse import urlparse, parse_qs import gradio as gr import librosa import numpy as np import soundfile as sf import sox import yt_dlp from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter from pedalboard.io import AudioFile from pydub import AudioSegment output_dir = os.path.join(BASE_DIR, 'song_output') def get_youtube_video_id(url, ignore_playlist=True): """ Examples: http://youtu.be/SA2iWivDJiE http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu http://www.youtube.com/embed/SA2iWivDJiE http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US """ query = urlparse(url) if query.hostname == 'youtu.be': if query.path[1:] == 'watch': return query.query[2:] return query.path[1:] if query.hostname in {'www.youtube.com', 'youtube.com', 'music.youtube.com'}: if not ignore_playlist: # use case: get playlist id not current video in playlist with suppress(KeyError): return parse_qs(query.query)['list'][0] if query.path == '/watch': return parse_qs(query.query)['v'][0] if query.path[:7] == '/watch/': return query.path.split('/')[1] if query.path[:7] == '/embed/': return query.path.split('/')[2] if query.path[:3] == '/v/': return query.path.split('/')[2] # returns None for invalid YouTube url return None def yt_download(link): ydl_opts = { 'format': 'bestaudio', 'outtmpl': '%(title)s', 'nocheckcertificate': True, 'ignoreerrors': True, 'no_warnings': True, 'quiet': True, 'extractaudio': True, 'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3'}], } with yt_dlp.YoutubeDL(ydl_opts) as ydl: result = ydl.extract_info(link, download=True) download_path = ydl.prepare_filename(result, outtmpl='%(title)s.mp3') return download_path def raise_exception(error_msg, is_webui): if is_webui: raise gr.Error(error_msg) else: raise Exception(error_msg) with gr.Blocks(theme=gr.themes.Soft() as app: gr.HTML("