added youtube song import
Browse files
app.py
CHANGED
@@ -12,13 +12,74 @@ import gradio as gr
|
|
12 |
from scipy.io.wavfile import write
|
13 |
|
14 |
from audiocraft.models import MusicGen
|
15 |
-
|
16 |
import os
|
17 |
from audiocraft.data.audio import audio_write
|
18 |
|
19 |
|
20 |
MODEL = None
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def split_process(audio, chosen_out_track):
|
23 |
os.makedirs("out", exist_ok=True)
|
24 |
write('test.wav', audio[0], audio[1])
|
@@ -99,6 +160,10 @@ with gr.Blocks(css=css) as demo:
|
|
99 |
|
100 |
with gr.Column():
|
101 |
uploaded_sound = gr.Audio(type="numpy", label="Input", source="upload")
|
|
|
|
|
|
|
|
|
102 |
with gr.Row():
|
103 |
chosen_track = gr.Radio(["vocals", "bass", "drums", "other", "all-in"], label="Track", info="Which track from your audio do you want to mashup ?", value="vocals")
|
104 |
load_sound_btn = gr.Button('Load your chosen track')
|
@@ -161,6 +226,7 @@ with gr.Blocks(css=css) as demo:
|
|
161 |
inputs=[music_prompt, melody, duration, cfg_coef],
|
162 |
outputs=[output]
|
163 |
)
|
|
|
164 |
load_sound_btn.click(split_process, inputs=[uploaded_sound, chosen_track], outputs=[melody], api_name="splt_trck")
|
165 |
submit.click(predict, inputs=[music_prompt, melody, duration, cfg_coef], outputs=[output])
|
166 |
|
|
|
12 |
from scipy.io.wavfile import write
|
13 |
|
14 |
from audiocraft.models import MusicGen
|
15 |
+
import tempfile
|
16 |
import os
|
17 |
from audiocraft.data.audio import audio_write
|
18 |
|
19 |
|
20 |
MODEL = None
|
21 |
|
22 |
+
import yt_dlp as youtube_dl
|
23 |
+
from moviepy.editor import VideoFileClip
|
24 |
+
|
25 |
+
YT_LENGTH_LIMIT_S = 480 # limit to 1 hour YouTube files
|
26 |
+
|
27 |
+
def download_yt_audio(yt_url, filename):
|
28 |
+
info_loader = youtube_dl.YoutubeDL()
|
29 |
+
|
30 |
+
try:
|
31 |
+
info = info_loader.extract_info(yt_url, download=False)
|
32 |
+
except youtube_dl.utils.DownloadError as err:
|
33 |
+
raise gr.Error(str(err))
|
34 |
+
|
35 |
+
file_length = info["duration_string"]
|
36 |
+
file_h_m_s = file_length.split(":")
|
37 |
+
file_h_m_s = [int(sub_length) for sub_length in file_h_m_s]
|
38 |
+
|
39 |
+
if len(file_h_m_s) == 1:
|
40 |
+
file_h_m_s.insert(0, 0)
|
41 |
+
if len(file_h_m_s) == 2:
|
42 |
+
file_h_m_s.insert(0, 0)
|
43 |
+
file_length_s = file_h_m_s[0] * 3600 + file_h_m_s[1] * 60 + file_h_m_s[2]
|
44 |
+
|
45 |
+
if file_length_s > YT_LENGTH_LIMIT_S:
|
46 |
+
yt_length_limit_hms = time.strftime("%HH:%MM:%SS", time.gmtime(YT_LENGTH_LIMIT_S))
|
47 |
+
file_length_hms = time.strftime("%HH:%MM:%SS", time.gmtime(file_length_s))
|
48 |
+
raise gr.Error(f"Maximum YouTube length is {yt_length_limit_hms}, got {file_length_hms} YouTube video.")
|
49 |
+
|
50 |
+
ydl_opts = {"outtmpl": filename, "format": "worstvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best"}
|
51 |
+
|
52 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
53 |
+
try:
|
54 |
+
ydl.download([yt_url])
|
55 |
+
except youtube_dl.utils.ExtractorError as err:
|
56 |
+
raise gr.Error(str(err))
|
57 |
+
|
58 |
+
|
59 |
+
def convert_to_mp3(input_path, output_path):
|
60 |
+
try:
|
61 |
+
video_clip = VideoFileClip(input_path)
|
62 |
+
audio_clip = video_clip.audio
|
63 |
+
print("Converting to MP3...")
|
64 |
+
audio_clip.write_audiofile(output_path)
|
65 |
+
except Exception as e:
|
66 |
+
print("Error:", e)
|
67 |
+
|
68 |
+
def load_youtube_audio(yt_link):
|
69 |
+
|
70 |
+
gr.Info("Loading your YouTube link ... ")
|
71 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
72 |
+
filepath = os.path.join(tmpdirname, "video.mp4")
|
73 |
+
download_yt_audio(yt_link, filepath)
|
74 |
+
|
75 |
+
mp3_output_path = "video_sound.mp3"
|
76 |
+
convert_to_mp3(filepath, mp3_output_path)
|
77 |
+
print("Conversion complete. MP3 saved at:", mp3_output_path)
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
return mp3_output_path
|
82 |
+
|
83 |
def split_process(audio, chosen_out_track):
|
84 |
os.makedirs("out", exist_ok=True)
|
85 |
write('test.wav', audio[0], audio[1])
|
|
|
160 |
|
161 |
with gr.Column():
|
162 |
uploaded_sound = gr.Audio(type="numpy", label="Input", source="upload")
|
163 |
+
with.gr.Row():
|
164 |
+
youtube_link = gr.Textbox(show_label=False, placeholder="you can also paste YT link and load it")
|
165 |
+
yt_load_btn = gr.Button("Load YT song")
|
166 |
+
|
167 |
with gr.Row():
|
168 |
chosen_track = gr.Radio(["vocals", "bass", "drums", "other", "all-in"], label="Track", info="Which track from your audio do you want to mashup ?", value="vocals")
|
169 |
load_sound_btn = gr.Button('Load your chosen track')
|
|
|
226 |
inputs=[music_prompt, melody, duration, cfg_coef],
|
227 |
outputs=[output]
|
228 |
)
|
229 |
+
yt_load_btn.click(fn=load_youtube_audio, inputs=[youtube_link], outputs=[uploaded_sound], queue=False)
|
230 |
load_sound_btn.click(split_process, inputs=[uploaded_sound, chosen_track], outputs=[melody], api_name="splt_trck")
|
231 |
submit.click(predict, inputs=[music_prompt, melody, duration, cfg_coef], outputs=[output])
|
232 |
|