Skip to content

Instantly share code, notes, and snippets.

@zerdnem
Created April 4, 2018 07:00
Show Gist options
  • Select an option

  • Save zerdnem/d52840f119e8df9d7dc8d4f533f313c0 to your computer and use it in GitHub Desktop.

Select an option

Save zerdnem/d52840f119e8df9d7dc8d4f533f313c0 to your computer and use it in GitHub Desktop.
const {exec} = require('child_process')
function download(callback) {
const {argv} = require('process')
const urls = argv.slice(2).join().replace(/,/g, ' ')
const command = `youtube-dl -f140 ${urls}`
let result = ''
// console.log(`Downloading ${urls}`)
run(command)
.then(stdout => {
result += stdout
const file = result.match(/"(.*?)"/g)
return callback(file)
})
.catch(err => console.log(err))
}
function remove(input) {
const {stat, unlink} = require('fs')
const {resolve} = require('path')
input = input.replace(/(^"|"$)/g, '')
stat(input, (err, stats) => {
if (err) {
console.log("No such file ", input)
return
}
unlink(resolve(input), err => {
if (err) console.log(err)
})
})
}
function run(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error)
} else {
resolve(stdout)
}
}).stdout.pipe(process.stdout)
})
}
function convert(data) {
const {unlink} = require('fs')
const path = require('path')
data.forEach(file => {
const output = file.replace('.m4a', '.mp3')
const input = file
const command = `ffmpeg -i ${input} ${output}`
console.log(`Converting ${file} to mp3`)
run(command)
.then(_ => {
console.log(`${output} DONE!`)
})
.then(() => {
remove(input)
})
.catch(err => console.log(err))
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment