Created
April 4, 2018 07:00
-
-
Save zerdnem/d52840f119e8df9d7dc8d4f533f313c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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