forked from fent/node-ytdl-core
/
ffmpeg.js
28 lines (27 loc) · 979 Bytes
/
ffmpeg.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
const path = require('path');
const fs = require('fs');
const ytdl = require('..');
const ffmpeg = require('fluent-ffmpeg');
var url = 'https://www.youtube.com/watch?v=TGbwL8kSpEk';
var audioOutput = path.resolve(__dirname, 'sound.mp4');
ytdl(url, { filter: function(f) {
return f.container === 'mp4' && !f.encoding; } })
// Write audio to file since ffmpeg supports only one input stream.
.pipe(fs.createWriteStream(audioOutput))
.on('finish', () => {
ffmpeg()
.input(ytdl(url, { filter: (f) => {
return f.container === 'mp4' && !f.audioEncoding; } }))
.videoCodec('copy')
.input(audioOutput)
.audioCodec('copy')
.save(path.resolve(__dirname, 'output.mp4'))
.on('error', console.error)
.on('progress', function(progress) {
process.stdout.cursorTo(0);
process.stdout.clearLine(1);
process.stdout.write(progress.timemark);
}).on('end', () => {
console.log();
});
});