-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaraoke_generator.py
60 lines (49 loc) · 1.82 KB
/
karaoke_generator.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import shutil
import sys
from pydub import AudioSegment
from spleeter.separator import Separator
import yt_dlp
def download_youtube_audio(youtube_url, output_wav):
"""
Download audio from a YouTube video and save it as a WAV file.
"""
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
'preferredquality': '192',
}],
'outtmpl': output_wav.replace('.wav', ''),
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([youtube_url])
def generate_karaoke(input_wav, output_mp3):
"""
Generate a karaoke version of the input WAV file using Spleeter.
"""
# Initialize the Spleeter separator with the 2-stems model
separator = Separator('spleeter:2stems')
# Separate the audio
separator.separate_to_file(input_wav, 'output')
# Load the generated accompaniment file
accompaniment_path = os.path.join('output', os.path.splitext(os.path.basename(input_wav))[0], 'accompaniment.wav')
accompaniment = AudioSegment.from_wav(accompaniment_path)
# Export the accompaniment as an MP3 file
accompaniment.export(output_mp3, format="mp3")
# Clean up the output directory
shutil.rmtree(os.path.join('output', os.path.splitext(os.path.basename(input_wav))[0]))
os.rmdir('output')
print(f"Karaoke version saved as {output_mp3}")
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python karaoke.py <youtube_url> <output_wav> <output_mp3>")
sys.exit(1)
youtube_url = sys.argv[1]
output_wav = sys.argv[2]
output_mp3 = sys.argv[3]
# Download YouTube audio as WAV
download_youtube_audio(youtube_url, output_wav)
# Generate karaoke version
generate_karaoke(output_wav, output_mp3)