From 7817ae4db0de1e47155c86e6edf3b660a07f0239 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 27 Jul 2022 19:06:54 +0300 Subject: [PATCH] convert mp4 to mp3 --- User-Menu-Cookbook.md | 68 ++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/User-Menu-Cookbook.md b/User-Menu-Cookbook.md index 72f3a55..0751f2a 100644 --- a/User-Menu-Cookbook.md +++ b/User-Menu-Cookbook.md @@ -40,34 +40,54 @@ Simple example of running bash scripts with help of `TerminalRun`. const isMp3 = (a) => /\.mp3$/.test(a); export default { - 'F - Convert flac to mp3': async ({DOM, CloudCmd}) => { - const {IO, CurrentInfo} = DOM; +const isMp3 = (a) => /\.mp3$/.test(a); - const root = CloudCmd.config('root'); - const cwd = `${root}${CurrentInfo.dirPath}`; - - const convert = 'for f in *.flac; do ffmpeg -vsync 2 -i "$f" -b:a 320k "${f%flac}mp3"; done'; - const exitCode = await CloudCmd.TerminalRun.show({ - cwd, - command: `bash -c '${convert}'`, +export default { + 'F - Convert flac to mp3': async ({DOM, CloudCmd}) => { + const command = 'for f in *.flac; do ffmpeg -vsync 2 -i "$f" -b:a 320k "${f%flac}mp3"; done'; + convert(command, { + DOM, + CloudCmd, + }); + }, + 'M - Convert mp4 to mp3': async ({DOM, CloudCmd}) => { + const command = 'for f in *.mp4; do ffmpeg -i "$f" "${f%mp4}mp3"; done'; + convert(command, { + DOM, + CloudCmd, }); - - if (exitCode) - return; - - await IO.createDirectory('/mp3'); - await CloudCmd.refresh(); - - const names = DOM.getFilenames(CurrentInfo.files); - const mp3Names = names.filter(isMp3); - - const from = CurrentInfo.dirPath; - const to = `${from}mp3` - - await IO.move(from, to, mp3Names); - await CloudCmd.refresh(); }, }; + +async function convert(command, {DOM, CloudCmd}) { + const {IO, Dialog, CurrentInfo} = DOM; + + const root = CloudCmd.config('root'); + const cwd = `${root}${CurrentInfo.dirPath}`; + + const exitCode = await CloudCmd.TerminalRun.show({ + cwd, + command: `bash -c '${command}'`, + }); + + if (exitCode === -1) + return Dialog.alert(`☝️ Looks like Terminal is disabled, start Cloud Coammnder with '--terminal' flag.`); + + if (exitCode) + return Dialog.alert(`☝️ Looks like something bad happend. Exit code: ${exitCode}`); + + await IO.createDirectory(`${cwd}/mp3`); + await CloudCmd.refresh(); + + const names = DOM.getFilenames(CurrentInfo.files); + const mp3Names = names.filter(isMp3); + + const from = CurrentInfo.dirPath; + const to = `${from}mp3` + + await IO.move(from, to, mp3Names); + await CloudCmd.refresh(); +} ``` ## Create User Menu File