« get me outta code hell

http-music - Command-line music player + utils (not a server!)
about summary refs log tree commit diff
path: root/src/downloaders.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/downloaders.js')
-rw-r--r--src/downloaders.js45
1 files changed, 29 insertions, 16 deletions
diff --git a/src/downloaders.js b/src/downloaders.js
index c41efa5..138b2d6 100644
--- a/src/downloaders.js
+++ b/src/downloaders.js
@@ -110,26 +110,39 @@ function makePowerfulDownloader(downloader, maxAttempts = 5) {
   }
 }
 
-async function makeConverter(type) {
-  let binary
-  if (await commandExists('avconv')) {
-    binary = 'avconv'
-  } else if (await commandExists('ffmpeg')) {
-    binary = 'ffmpeg'
-  } else {
-    throw new Error('avconv or ffmpeg is required for converter downloader!')
+async function makeConverter(
+  converterCommand = null, exportExtension = 'wav',
+  converterOptions = ['-i', '$in', '$out']
+) {
+  if (converterCommand === null) {
+    throw new Error(
+      'A converter is required! Try installing ffmpeg or avconv?'
+    )
   }
 
-  console.log(`Using ${binary} converter.`)
-
-  return async function(inFile) {
-    const base = path.basename(inFile, path.extname(inFile))
-    const tempDir = tempy.directory()
-    const outFile = `${tempDir}/${base}.${type}`
+  return function(converterOptions = ['-i', '$in', '$out']) {
+    return async function(inFile) {
+      const base = path.basename(inFile, path.extname(inFile))
+      const tempDir = tempy.directory()
+      const outFile = `${tempDir}/${base}.${exportExtension}`
+
+      const processedOptions = converterOptions.slice(0)
+
+      // And some people say JavaScript isn't awesome!?
+      for (const [ i, item ] of processedOptions.entries()) {
+        if (item === '$in') {
+          processedOptions[i] = inFile
+        } else if (item === '$out') {
+          processedOptions[i] = outFile
+        }
+      }
 
-    await promisifyProcess(spawn(binary, ['-i', inFile, outFile]), false)
+      await promisifyProcess(
+        spawn(converterCommand, processedOptions), false
+      )
 
-    return outFile
+      return outFile
+    }
   }
 }