-
Notifications
You must be signed in to change notification settings - Fork 65
/
index.js
executable file
·48 lines (39 loc) · 1.1 KB
/
index.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
#!/usr/bin/env node
'use strict'
const ytdl = require('ytdl-core')
const FFmpeg = require('fluent-ffmpeg')
const { PassThrough } = require('stream')
const fs = require('fs')
if (!module.parent) {
const youtubeUrl = process.argv.slice(2)[0]
if (!youtubeUrl) throw new TypeError('youtube url not specified')
streamify(youtubeUrl).pipe(process.stdout)
} else {
module.exports = streamify
}
function streamify (uri, opt) {
opt = {
...opt,
videoFormat: 'mp4',
quality: 'lowest',
audioFormat: 'mp3',
filter (format) {
return format.container === opt.videoFormat && format.audioBitrate
}
}
const video = ytdl(uri, opt)
const { file, audioFormat } = opt
const stream = file ? fs.createWriteStream(file) : new PassThrough()
const ffmpeg = new FFmpeg(video)
process.nextTick(() => {
const output = ffmpeg.format(audioFormat).pipe(stream)
ffmpeg.once('error', error => stream.emit('error', error))
output.once('error', error => {
video.end()
stream.emit('error', error)
})
})
stream.video = video
stream.ffmpeg = ffmpeg
return stream
}