I had to make a few changes. For one thing, because I have Node-RED running in a docker container, I couldn't figure out how to tell the script to use either ffmpeg from my host system, or from another container with ffmpeg. So I finally gave that up and just entered the Node-RED container shell and installed ffmpeg:
sudo docker ps
(and I took note of the container ID)
sudo docker exec -it --user=root f4d6abb9d6d9 bash
(sub f4d6abb9d6d9 with whatever your container ID is)
apk add ffmpeg
(this Alpine command installs ffmpeg)
The other major change was the script to create the mp3. It would do funky things like try to create the following (example): /data/recordings/record_7/1/2023,-11-32-21-AM_5551239876.mp3
I think what is meant to happen is it creates a file called record_7-1-2023,-11-32-21-AM_5551239876.mp3 but it has slashes instead of dashes and because the folders record_7 and 1 don't exist, the mp3 file is never made. Plus there's the weird, random comma in there. Here is a script that will create a file such as 2023-07-01_11-32-21_5551239876.mp3 right in the /data/recordings folder (or /opt/recordings or whatever you want):
Double click on the node Clear Flow CID Variables / build command for next exec node to Rename File and convert to MP3 and change the script to this (substituting the folderPath with whatever your folder is where you want your recordings):
msg.payload = "";
var now = new Date();
var month = String(now.getMonth() + 1).padStart(2, "0");
var day = String(now.getDate()).padStart(2, "0");
var year = now.getFullYear();
var hours = String(now.getHours()).padStart(2, "0");
var minutes = String(now.getMinutes()).padStart(2, "0");
var seconds = String(now.getSeconds()).padStart(2, "0");
var timestamp = `${year}-${month}-${day}_${hours}-${minutes}-${seconds}`;
msg.now = timestamp;
// Clear the CID flow variable for this call only.
if (msg.port === "0") {
var cid = flow.get("port0cid");
flow.set("port0cid", "unknown");
}
if (msg.port === "1") {
var cid = flow.get("port1cid");
flow.set("port1cid", "unknown");
}
// Folder path
var folderPath = "/data/recordings/";
msg.folderPath = folderPath;
// Build Linux command to post-process the file, including renaming and conversion to MP3
var newName = folderPath + timestamp + "_" + cid + ".mp3";
msg.newName = newName;
msg.payload = "ffmpeg -i " + msg.filename + " " + newName + " && rm " + msg.filename;
return msg;
At this point, I'm not entirely sure what will happen with the other nodes with scripts for archiving and whatnot, but for now, it records the call automatically for me, and converts it to an mp3, which is top priority for me right now.
For reference, the original script is this:
msg.payload = "";
var now = new Date();
now = now.toLocaleString();
now = now.replace(/[ :]/g, "-");
msg.now = now;
// Clear the CID flow variable for this call only.
if (msg.port === "0") {
var cid = flow.get("port0cid");
flow.set("port0cid", "unknown");
}
if (msg.port === "1") {
var cid = flow.get("port1cid");
flow.set("port1cid", "unknown");
}
//Build Linux Command to post-process the file which includes renaming
//and possibly conversion to MP3
var newName = msg.filePath + "record_" + now + "_" + cid + ".mp3";
msg.newName = newName;
msg.payload = "ffmpeg -i " + msg.filename + " " + newName + " && rm " + msg.filename;
return msg;