import os import librosa import soundfile import gradio as gr from scipy.io import wavfile import subprocess from pathlib import Path import time import random from datetime import datetime from scipy.io.wavfile import write from pydub import AudioSegment import requests import re import yt_dlp high_quality = True ''' os.system("git clone https://github.com/google/REAPER.git") os.system("cd REAPER") os.system("mkdir build") os.system("cd build") os.system("cmake ..") os.system("make") os.system("cd ..") os.system("cd ..") ''' def svc(audio_src, audio_ref, singing_check): wav1, sr1 = librosa.load(audio_src, sr=24000) soundfile.write("audio_source.wav", wav1, sr1) wav2, sr2 = librosa.load(audio_ref, sr=24000) soundfile.write("audio_reference.wav", wav2, sr2) if singing_check == True: os.system("python infer.py --src_wav_path audio_source.wav --ref_wav_path audio_reference.wav --out_path output.wav") else: os.system("python infer.py --src_wav_path audio_source.wav --ref_wav_path audio_reference.wav --out_path output.wav --speech_enroll") return "output.wav" headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4302.0 Safari/537.36" } pattern = r'//www\.bilibili\.com/video[^"]*' def find_first_appearance_with_neighborhood(text, pattern): match = re.search(pattern, text) if match: return match.group() else: return None def search_bilibili(keyword): req = requests.get("https://search.bilibili.com/all?keyword={}&duration=1&tids=3&page=1".format(keyword), headers=headers).text video_link = "https:" + find_first_appearance_with_neighborhood(req, pattern) return video_link def youtube_downloader( song_name, start_time, end_time, is_full_song, output_filename="track.wav", num_attempts=5, url_base="", quiet=False, force=True, ): video_identifier = search_bilibili(song_name) if is_full_song: ydl_opts = { 'noplaylist': True, 'format': 'bestaudio/best', 'postprocessors': [{ 'key': 'FFmpegExtractAudio', 'preferredcodec': 'wav', }], "outtmpl": 'dl_audio/youtube_audio', } with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([video_identifier]) audio_path = "dl_audio/youtube_audio.wav" return audio_path else: output_path = Path(output_filename) if output_path.exists(): if not force: return output_path else: output_path.unlink() quiet = "--quiet --no-warnings" if quiet else "" command = f""" yt-dlp {quiet} -x --audio-format wav -f bestaudio -o "{output_filename}" --download-sections "*{start_time}-{end_time}" "{url_base}{video_identifier}" # noqa: E501 """.strip() attempts = 0 while True: try: _ = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT) except subprocess.CalledProcessError: attempts += 1 if attempts == num_attempts: return None else: break if output_path.exists(): return output_path else: return None def audio_separated(audio_input, progress=gr.Progress()): # start progress progress(progress=0, desc="Starting...") time.sleep(0.1) # check file input if audio_input is None: # show progress for i in progress.tqdm(range(100), desc="Please wait..."): time.sleep(0.01) return (None, None, 'Please input audio.') # create filename filename = str(random.randint(10000,99999))+datetime.now().strftime("%d%m%Y%H%M%S") # progress progress(progress=0.10, desc="Please wait...") # make dir output os.makedirs("output", exist_ok=True) # progress progress(progress=0.20, desc="Please wait...") # write if high_quality: write(filename+".wav", audio_input[0], audio_input[1]) else: write(filename+".mp3", audio_input[0], audio_input[1]) # progress progress(progress=0.50, desc="Please wait...") # demucs process if high_quality: command_demucs = "python3 -m demucs --two-stems=vocals "+filename+".wav -o output" else: command_demucs = "python3 -m demucs --two-stems=vocals --mp3 --mp3-bitrate 128 "+filename+".mp3 -o output" os.system(command_demucs) # progress progress(progress=0.70, desc="Please wait...") # remove file audio if high_quality: command_delete = "rm -v ./"+filename+".wav" else: command_delete = "rm -v ./"+filename+".mp3" os.system(command_delete) # progress progress(progress=0.80, desc="Please wait...") # progress for i in progress.tqdm(range(80,100), desc="Please wait..."): time.sleep(0.1) if high_quality: return "./output/htdemucs/"+filename+"/vocals.wav","./output/htdemucs/"+filename+"/no_vocals.wav","Successfully..." else: return "./output/htdemucs/"+filename+"/vocals.mp3","./output/htdemucs/"+filename+"/no_vocals.mp3","Successfully..." # mix vocal and non-vocal def mix(audio1, audio2): sound1 = AudioSegment.from_file(audio1) sound2 = AudioSegment.from_file(audio2) length = len(sound1) mixed = sound1[:length].overlay(sound2) mixed.export("song.wav", format="wav") return "song.wav" app = gr.Blocks() with app: gr.Markdown("#