Skip to content

Instantly share code, notes, and snippets.

@ypujante
Created October 18, 2020 18:42
Show Gist options
  • Save ypujante/a212b39c5df78b27f0a54830f5c243dc to your computer and use it in GitHub Desktop.
Save ypujante/a212b39c5df78b27f0a54830f5c243dc to your computer and use it in GitHub Desktop.
python 3 script to extract vocals from a song using the Docker researchdeezer/spleeter image
#!/usr/bin/env python3
# Example:
# > cd /tmp
# extract_vocals.py '/Users/ypujante/Music/iTunes/iTunes Music/Music/Janet Jackson/Janet/08 This Time.m4a'
# Generates /tmp/08 This Time/vocals.wav and /tmp/08 This Time/accompaniment.wav
import argparse
import os
import subprocess
spleeter_docker_image = "researchdeezer/spleeter:3.7-2stems"
parser = argparse.ArgumentParser(usage='extract_vocals.py [-h] [-o output] <input>')
parser.add_argument("-n", "--dry-run", help="Dry run (prints what it is going to do)", action="store_true", dest="dry_run")
parser.add_argument("-v", "--verbose", help="Verbose", action="store_true")
parser.add_argument("-o", "--output", help="Output directory (default to current dir)")
parser.add_argument("input", help="Input file to extract vocals from")
args = parser.parse_args()
input = os.path.realpath(args.input)
output = os.path.realpath(args.output if args.output else os.getcwd())
dir, filename = os.path.split(input)
verbose = [] if not args.verbose else ['--verbose']
cmd = [
'docker', 'run',
'-v', f'{dir}:/input',
'-v', f'{output}:/output',
spleeter_docker_image,
'separate', *verbose, '-i', f'/input/{filename}', '-o', '/output'
]
if args.dry_run:
print(' '.join(cmd))
else:
subprocess.run(cmd)
@ypujante
Copy link
Author

It works on macOS but you need to make sure that the Docker VM image has enough memory. If you see this kind of warnings:

2020-10-18 18:38:12.587523: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 467008080 exceeds 10% of system memory.

you should increase the size of the memory allocated to the VM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment