-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract-sound.js
61 lines (55 loc) · 1.42 KB
/
extract-sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
var argv = require('minimist')(process.argv.slice(2))
const Stopwatch = require('statman-stopwatch')
console.log(argv)
const fs = require('fs')
const download = require('download')
const ffmpeg = require('fluent-ffmpeg')
const path = require('path')
const {nanoid} = require('nanoid')
const sw = new Stopwatch()
const swDL = new Stopwatch()
let downloadTime = 0
let extractTime = 0
const noop = () => { }
const {
log = noop,
transform = noop,
channel = 0,
output,
format
} = {
output: argv.output
}
let input = argv.input
const f = format || (output ? path.parse(output).ext.slice(1) : 'mp3')
;(async () => {
let fileName = ''
if (argv.url) {
fileName = `${nanoid(5)}.mp4`
swDL.start()
console.log('download')
fs.writeFileSync(`/tmp/${fileName}`, await download(argv.url))
downloadTime = swDL.read()
console.log('finish downloaded:', downloadTime)
input = `/tmp/${fileName}`
}
const cmd = ffmpeg(input)
.audioChannels(channel)
.audioFrequency(44100)
.format(f)
.on('start', (cmd) => {
console.log('start')
sw.start()
})
.on('end', () => {
console.log('finish')
extractTime = sw.read()
console.log(`${extractTime / 1000} s`)
console.log(`Total time: ${(extractTime + downloadTime)/1000}s`)
})
.on('error', (err) => {
console.error(err)
})
transform(cmd)
cmd.output(output).run()
})()