Skip to content

Commit

Permalink
output mp3 file to save space
Browse files Browse the repository at this point in the history
  • Loading branch information
zak-45 authored Nov 6, 2023
1 parent e0bf3be commit 5cc1058
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 50 deletions.
126 changes: 82 additions & 44 deletions Spleeter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
author: zak45
date: 07/02/2023
version:1.1.0
version:1.2.0
Chataigne Module for Deezer /spleeter
Expand All @@ -22,13 +22,16 @@ var winHOME = "";
var sequence = "";
var targetFile = "";
var model = "";

var newAudio = "";

// spleeter cmd file name (required)
var spleeterCMDName = "spleeter.cmd";

// check spleeter
var spleeterIsRunning = false;

// CMD options
var splOutputOptions = " separate ";
var splOutputOptions = " separate -c mp3 ";

// verbose
var verbose = " -verbose";
Expand All @@ -42,8 +45,14 @@ var splStems = "spleeter:2stems";
// TEMP dir (required)
var tempDIR = "";

// increase timeout as we want to run Spleeter in synchronus way (blocking)
script.setExecutionTimeout(240);
// init test
var isInit = true;

//SCAnalyzer
var SCAexist = root.modules.getItemWithName("SCAnalyzer");

// increase timeout
script.setExecutionTimeout(300);

//We create necessary entries in modules & sequences. We need OS / sound card and Sequence with Trigger / Audio.
function init()
Expand All @@ -52,7 +61,7 @@ function init()
script.log("-- Custom command called init()");

// we check required TMP folder
spleeterTMP();
spTmpPath();
script.log("Temp folder : "+tempDIR);
if (tempDIR == "")
{
Expand Down Expand Up @@ -110,10 +119,9 @@ function init()

homeDIR = util.getEnvironmentVariable("$HOME");
}

local.parameters.spleeterParams.spleeterCommand.set(homeDIR+"/Chataigne/modules/Spleeter/spleeter.cmd");
}

/*
// execution depend on the user response
function messageBoxCallback (id, result)
{
Expand All @@ -127,6 +135,9 @@ function messageBoxCallback (id, result)
}
}
*/


function scriptParameterChanged (param)
{
script.log("Script Param changed : "+param.name);
Expand All @@ -146,16 +157,35 @@ function moduleValueChanged (value)
script.log("Module value changed : "+value);
}


function update()
{
// start long pocess on it's own thread to run in blocking mode but not block the main UI
if(shouldProcessSpl)
// init once
if (isInit)
{
shouldProcessSpl = false;
runSpleeter (sequence, targetFile, model);
}
isInit = false;
local.parameters.spleeterParams.spleeterCommand.set(homeDIR+"/Chataigne/modules/Spleeter/spleeter.cmd");
if (SCAexist.name == "sCAnalyzer")
{
script.log("SCAnalyzer present");
// avoid Chataigne Crash
//root.modules.sCAnalyzer.scripts.sCAnalyzer.reload.trigger();
var Spleetercontainer = SCAexist.parameters.getChild("Spleeter Params");
Spleetercontainer.setCollapsed(false);

} else {

script.log('No SCAnalyzer found');
}

} else {

// start long pocess on it's own thread to run in blocking mode but not block the main UI
if (shouldProcessSpl)
{
shouldProcessSpl = false;
runSpleeter (sequence, targetFile, model);
}
}
}

// check to see if something to do
Expand All @@ -171,8 +201,8 @@ function Spleeter (insequence, intargetFile, inmodel)
targetFile = intargetFile;
model = inmodel;

util.showYesNoCancelBox("confirmSpleeter", "This Should take a while", "Do you want to continue ?", "warning", "Yes", "No", "Don't care...");

util.showMessageBox("Spleeter", "This could take a while ...", "warning", "ok");
shouldProcessSpl = true;
}
}

Expand All @@ -183,7 +213,9 @@ function runSpleeter (sequence, targetFile, model)
spleeterCMDName = local.parameters.spleeterParams.spleeterCommand.getAbsolutePath();
// check to see if Spleeter exe exist
if (util.fileExists(spleeterCMDName) == 1)
{
{
spleeterIsRunning = true;

// options to add
splOutputOptions = local.parameters.spleeterParams.spleeterOptions.get();

Expand Down Expand Up @@ -223,44 +255,46 @@ function runSpleeter (sequence, targetFile, model)

} else {

script.log('not able to create start point');
script.log('Not able to define start point');
script.logError("Something wrong with Spleeter....");
spleeterIsRunning = false;
return;
}

// get audio name
var songname = getFilename(targetFile).replace(".mp3","");

// check to see if separation files already exist
var vocals = util.fileExists(splOutputFolder + "/" + songname + "/vocals.wav");
var vocals = util.fileExists(splOutputFolder + "/" + songname + "/vocals.mp3");
if (model.contains("spleeter:2stems"))
{
script.log("Check files for 2 stems");

var accompaniment = util.fileExists(splOutputFolder + "/" + songname + "/accompaniment.wav");
var accompaniment = util.fileExists(splOutputFolder + "/" + songname + "/accompaniment.mp3");

} else if ( model.contains("spleeter:4stems") || model.contains("spleeter:5stems")){

script.log("Check files for 4/5 stems");

var drums = util.fileExists(splOutputFolder + "/" + songname + "/drums.wav");
var bass = util.fileExists(splOutputFolder + "/" + songname + "/bass.wav");
var other = util.fileExists(splOutputFolder + "/" + songname + "/other.wav");
var drums = util.fileExists(splOutputFolder + "/" + songname + "/drums.mp3");
var bass = util.fileExists(splOutputFolder + "/" + songname + "/bass.mp3");
var other = util.fileExists(splOutputFolder + "/" + songname + "/other.mp3");

}
if ( model.contains("spleeter:5stems") )
{

script.log("Check files for 5 stems");

var piano = util.fileExists(splOutputFolder + "/" + songname + "/piano.wav");
var piano = util.fileExists(splOutputFolder + "/" + songname + "/piano.mp3");
}

// run spleeter only on Force or missing file
// run spleeter only on Force or missing file, this will execute ffprobe also
if ((local.parameters.spleeterParams.force.get() == 1) ||
(model.contains("spleeter:2stems") && (vocals == 0 ||accompaniment == 0)) ||
((model.contains("spleeter:4stems") || model.contains("spleeter:5stems")) && (drums == 0 || bass == 0 || other == 0)) ||
(model.contains("spleeter:5stems") && piano == 0))
(model.contains("spleeter:2stems") && (vocals == 0 || accompaniment == 0)) ||
(model.contains("spleeter:4stems") && (vocals == 0 || drums == 0 || bass == 0 || other == 0)) ||
(model.contains("spleeter:5stems") && (vocals == 0 || drums == 0 || bass == 0 || other == 0 || piano == 0))
)
{
// run Spleeter
if (local.parameters.spleeterParams.verbose.get() == 1)
Expand All @@ -273,25 +307,26 @@ function runSpleeter (sequence, targetFile, model)
script.log('command to run : '+ spleeterCMDName + exeOPT);
// we execute the Spleeter command in blocking mode to wait end of execution;
var launchresult = root.modules.os.launchProcess(spleeterCMDName + exeOPT, true);
script.log(launchresult);

} else {

splOutputOptions = "ffprobeOnly";

var exeOPT = " " + splOutputOptions + " -o " + '"' + splOutputFolder + '" -p ' + splStems+' "' + targetFile + '" ' + verbose;
script.log('command to run : '+ spleeterCMDName + exeOPT);
// we execute the Spleeter command file for only ffprobe
var launchresult = root.modules.os.launchProcess(spleeterCMDName + exeOPT, true);

// we execute the Spleeter command file for ffprobe only
var launchresult = root.modules.os.launchProcess(spleeterCMDName + exeOPT, true);
script.log(launchresult);
}

// set sequence to artist name /audio to title
// set sequence to artist name /audio to title: info come from ffprobe
newSequence.setName(readMp3Tags("artist"));
newAudio.setName(readMp3Tags("title"));

// read the result
script.log("we read from : " + splOutputFolder);
var newtargetFile = splOutputFolder + "/" + songname + "/vocals.wav";
var newtargetFile = splOutputFolder + "/" + songname + "/vocals.mp3";

// vocals is common file to all stems
var newvocals = util.fileExists(newtargetFile);
Expand All @@ -315,7 +350,7 @@ function runSpleeter (sequence, targetFile, model)
script.log("Check files for 2 stems");

// instrumental file
var newtargetFile = splOutputFolder + "/" + songname + "/accompaniment.wav";
var newtargetFile = splOutputFolder + "/" + songname + "/accompaniment.mp3";
var newaccompaniment = util.fileExists(newtargetFile);
if (newaccompaniment)
{
Expand All @@ -336,7 +371,7 @@ function runSpleeter (sequence, targetFile, model)
script.log("Check files for 4/5 stems");

// bass common for 4 or 5
var newtargetFile = splOutputFolder + "/" + songname + "/bass.wav";
var newtargetFile = splOutputFolder + "/" + songname + "/bass.mp3";
var newbass = util.fileExists(newtargetFile);
if (newbass)
{
Expand All @@ -353,7 +388,7 @@ function runSpleeter (sequence, targetFile, model)
}

// drums common for 4 or 5
var newtargetFile = splOutputFolder + "/" + songname + "/drums.wav";
var newtargetFile = splOutputFolder + "/" + songname + "/drums.mp3";
var newdrums = util.fileExists(newtargetFile);
if (newdrums)
{
Expand All @@ -370,7 +405,7 @@ function runSpleeter (sequence, targetFile, model)
}

// other common for 4 or 5
var newtargetFile = splOutputFolder + "/" + songname + "/other.wav";
var newtargetFile = splOutputFolder + "/" + songname + "/other.mp3";
var newother = util.fileExists(newtargetFile);
if (newother)
{
Expand All @@ -392,7 +427,7 @@ function runSpleeter (sequence, targetFile, model)
script.log("Check files for 5 stems");

// piano for 5 stems
var newtargetFile = splOutputFolder + "/" + songname + "/piano.wav";
var newtargetFile = splOutputFolder + "/" + songname + "/piano.mp3";
var newpiano = util.fileExists(newtargetFile);
if (newpiano)
{
Expand All @@ -413,14 +448,18 @@ function runSpleeter (sequence, targetFile, model)

} else {

spleeterIsRunning = false;

script.log("Spleeter cmd not found");
util.showMessageBox("Spleeter !", "spleeter not found", "warning", "OK");
script.logError("Something wrong with Spleeter....");
}

spleeterIsRunning = false;
}

// retreive temp location
function spleeterTMP ()
function spTmpPath ()
{
script.log("Retreive temp folder");
// TMP, TMPDIR, and TEMP environment variables
Expand Down Expand Up @@ -462,9 +501,9 @@ function readMp3Tags(type)
{
var tag = "";

if (util.fileExists(tempDIR + "/spleeterfileinfo.json"))
if (util.fileExists(tempDIR + "/ffprobefileinfo.json"))
{
var SCAJSONContent = util.readFile(tempDIR + "/spleeterfileinfo.json",true);
var SCAJSONContent = util.readFile(tempDIR + "/ffprobefileinfo.json",true);
if ( type == "artist" )
{
tag = SCAJSONContent.format.tags.artist;
Expand All @@ -480,15 +519,14 @@ function readMp3Tags(type)

} else {

tag = "undefined";
script.log('File does not exist : ' + tempDIR+"/spleeterfileinfo.json");
tag = "nofile";
script.log('File does not exist : ' + tempDIR + "/ffprobefileinfo.json");

}

return tag;
}


// used for value/expression testing .......
function testScript(songname)
{
Expand Down
4 changes: 2 additions & 2 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "Empty",
"path": "Custom",

"version": "1.1.0",
"version": "1.2.0",
"description": "Music separation by Deezer / Spleeter.
Need python / tensorflow / ffmpeg
The models available are:
Expand Down Expand Up @@ -35,7 +35,7 @@
"Spleeter options": {
"type": "String",
"description": "Command options ",
"default":" separate "
"default":" separate -c mp3 "
},
"Output Folder": {
"type": "File",
Expand Down
8 changes: 4 additions & 4 deletions spleeter.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
Rem this is the cmd file for spleeter
Rem need to be adapted depend on the running OS, this one is for Win
Rem will receive these parameters on entry " " + SPLoutputOptions + " -o " + '"' + SPLoutputFolder + '" -p ' + SPLstems+' "' + targetFile + '" ' + verbose
Rem which will give " separate -o audio_output -p spleeter:2stems audio_example.mp3 --verbose" for example
Rem which will give " separate -c mp3 -o audio_output -p spleeter:2stems audio_example.mp3 --verbose" for example

call "%USERPROFILE%\Documents\Chataigne\PySpleeter\WPy64-31090\scripts\env_for_icons.bat"
call "%USERPROFILE%\Documents\Chataigne\xtra\PySpleeter\WPy64-31090\scripts\env_for_icons.bat"

cd %WINPYDIRBASE%

ffprobe -show_format -print_format json %6 > %TMP%/spleeterfileinfo.json
ffprobe -show_format -print_format json %8 > %TMP%/ffprobefileinfo.json

if NOT %1 == "ffprobeOnly" spleeter.exe %1 %2 %3 %4 %5 %6 %7 %8 %9
if NOT "%1"=="ffprobeOnly" spleeter.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

0 comments on commit 5cc1058

Please sign in to comment.