header={ "recipe_version": "1.5", "title": "Extracts audio from video as MP3", "description": "Extracts the audio in MP3 encoding", "category": "video", "chef": "BeatRig", "spice": "BQ==:G+Hy36kJcnQQYYavb/JV2Il6eCYr6h4hRJHCF9rRbhX5BcazL6cuTgUMdz0xA50VWA5PmmV0Q7aWkiUtNFtbk991l+t0e8IBXMReMIT1GNjnZZDv7n2YRAo0xcSQbY0ixjG6LLH4wGKnspKAoA/v69iLn6N2IH0XzIZwTGdHSo4=", "palette": "Clean Slate", "flavour": "V0qYwFMjhE2ntrarDBMj9zSOLKY4DItUwb4cdGJoHMVnLyLghYvDD9zC4irlVaQ/N2JyCQuqY8yok/4DMp8CFtZo9jE99e/dO5Do3z4GYnheDjeV7ASZ64cCzDW9FK4zLZlvLSaU19hoMIQ9v06+3VATwFbKtfe7H20E1oMJVw4=", "time": 1670570388, "core_version": "0.4.0", "magnetron_version": "1.0.138", "type": "demo", "os": "windows,macOS", "functions": "main", "dependencies": "ffprobe,ffmpeg", "tags": "video,audio,mp3,rip", "uuid": "491fec178c6e4ce8b6ef01148e53568a", "instructions": "Drop file(s) here" }; //---------------------------------------------------------------------------- var config = { "totalsec": 0, }; //---------------------------------------------------------------------------- function cmd_callback(cmd_name, cmd_output) { if(cmd_name == "ffmpeg" && cmd_output.length > 0) { var progress = calculateSeconds(getStringFromTo(cmd_output, "time=", " bitrate")); progress = Math.round(progress / config.totalsec * 100); setProgress(progress); } return { "terminate": isCanceled() }; } function getStringFromTo(source, from, to) { var wildcard = "(" + from + ")(.*)(" + to + ")"; var r = searchRegEx(source, wildcard, "i", 0); if (typeof(r[0]) == "string") return r[0].substring(from.length, r[0].length - to.length); else return ""; } function calculateSeconds(dur_tc) { var hours = (dur_tc.substring(0, 2)); var minutes = (dur_tc.substring(3, 3+2)); var seconds = (dur_tc.substring(6, 6+2)); if (hours.substring(0, 1) == "0") hours = hours.substring(1,2); if (minutes.substring(0, 1) == "0") minutes = minutes.substring(1,2); if (seconds.substring(0, 1) == "0") seconds = seconds.substring(1,2); hours = parseInt(hours); minutes = parseInt(minutes); seconds = parseInt(seconds); totalsec = (hours * 60 * 60) + (minutes * 60) + seconds; return totalsec; } function getFileProps(infile) { var probeoutput = cmd("ffprobe", [infile]); echo(probeoutput); var dur_tc = getStringFromTo(probeoutput, "Duration: ", ", s"); var extension = searchRegEx(probeoutput, "(?:Audio: )(.*?...)", "i", 1)[0]; config.totalsec = calculateSeconds(dur_tc); config.fps = parseInt(getStringFromTo(probeoutput, "kb/s, ", " fps")); config.extension = extension; echo("dur:" + config.totalsec); echo("extension:" + extension); return dur_tc; } function setupArgs(infile, cutItt) { var args1 = ["-y", "-i", infile]; if (gvar.demo) { echo("overlay"); args1.push("-i"); args1.push(config.overlay_file); } args1.push("-map"); args1.push("a"); args1.push("-q:a"); args1.push("0"); args1.push(config.outfile); return args1; } //---------------------------------------------------------------------------- function main() { // check if ffmpeg is installed if (fileExists(getAllowedApps("ffmpeg")) == false) abort("no ffmpeg installed"); if (fileExists(getAllowedApps("ffprobe")) == false) abort("no ffprobe installed"); setMainMessage("starting"); setProgress(0); var now = getCurrentEpoch(); // secs since 1 jan 1970 var files = getFiles(); var numFiles = files.length; if (numFiles <= 0) abort("drop a file in the list to encode"); if (gvar.demo && numFiles > 1) { numFiles = 1; dialog(header.title, "demo only supports one file at a time", "w"); } for (i = 0; i < numFiles; i++) { // base path var infile = files[i].path; var pathinfo = getPathInfo(infile); var filesize = getFileBytes(infile); echo(infile); echo("file size:" + bytesToDescription(filesize)); setFileIcon(files[i].path, files[i].index, "HourglassHalf"); setFileIconColor(files[i].path, files[i].index, "FF5299D3"); setFileStatus(files[i].path, files[i].index, "busy"); // calc the duration in sec getFileProps(infile); if (config.totalsec <= 0) abort("no content found in this file"); var newfilesize = 0; { setProgress(101); var userOuputPath = pathinfo.folder + gvar.pss + pathinfo.basename + "-[magnetron.app].mp3"; userOuputPath = saveFileDialog("Select a output path", userOuputPath, "*.mp3"); config.outfile = userOuputPath; if (config.outfile.length <= 0) abort("cancelled"); var msg = ("busy please wait"); setMainMessage(msg); echo(msg); var args1 = setupArgs(infile, config.partItt); echo(cmd("ffmpeg", args1)); // perform cmd newfilesize = getFileBytes(config.outfile); echo("new file size:" + bytesToDescription(newfilesize)); if(isCanceled()) abort("cancelled"); } if (newfilesize <= 0) { setFileIcon(files[i].path, files[i].index, "ExclamationTriangle"); setFileIconColor(files[i].path, files[i].index, "FFb11414"); setFileStatus(files[i].path, files[i].index, "can't encode this file", "w"); } else { setFileIcon(files[i].path, files[i].index, "CheckCircleO"); setFileIconColor(files[i].path, files[i].index, "FF3aac4d"); setFileStatus(files[i].path, files[i].index, "done"); } } var then = getCurrentEpoch(); // secs since 1 jan 1970 setMainMessage("done in " + (then - now) + " sec"); echo(objectToString(config)); setProgress(100); }