File size: 1,660 Bytes
c56a4ac
 
9da0152
c56a4ac
 
 
5d0af45
 
c56a4ac
 
 
 
 
 
 
 
006a4a6
 
c56a4ac
 
 
ca128e7
 
847f01f
 
8268997
 
 
 
c56a4ac
 
 
15c51fc
 
f66fa62
8268997
d720e93
c56a4ac
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
import os
import time
import random
import subprocess

def separate_audio(audio_path):
    stem_count = 2
    print(f"{audio_path=}")
    head, tail = os.path.split(audio_path)

    gradio_temp_path = head
    audio_filename = tail.split('.')[0]
    print(f"{gradio_temp_path=}")
    print(f"{audio_filename=}")
    print(f"{stem_count=}")
    os.makedirs(gradio_temp_path + f"/{audio_filename}/", exist_ok=True)
    command = f"spleeter separate -p spleeter:{stem_count}stems {audio_path} -o {gradio_temp_path}/"
    command = command.split()
    print(f"{command=}")

    result = subprocess.run(command)
    print(result)
    subprocess.run((f"ffmpeg -y -i {gradio_temp_path}/{audio_filename}/accompaniment.wav {gradio_temp_path}/{audio_filename}/accompaniment.mp3").split())
    subprocess.run((f"ffmpeg -y -i {gradio_temp_path}/{audio_filename}/vocals.wav {gradio_temp_path}/{audio_filename}/vocals.mp3").split())

    accompaniment_path = os.path.join(gradio_temp_path, audio_filename, "accompaniment.mp3")
    vocals_path = os.path.join(gradio_temp_path, audio_filename, "vocals.mp3")
    return accompaniment_path, vocals_path

iface = gr.Interface(
    fn=separate_audio,
    title="Professional(😂) One click music vocal extraction using Spleeter",
    description="Usage Example: you can import the vocal to one of those voice changers then combine instrument with the result to get the normal music but with a changed vocal!",
    inputs=[gr.Audio(type="filepath", label="Audio File")],
    outputs=[gr.Audio(type="filepath", label='Accompaniment'), gr.Audio(type="filepath", label='Vocals')],
    live=True
)

iface.launch()