-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·73 lines (62 loc) · 2.11 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env node
const ytdl = require('ytdl-core');
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs');
const path = require('path');
// Parse argv TODO: finetune
const argv = process.argv.splice(2);
const videoUrl = new URL(argv[0]); //'https://www.youtube.com/watch?v=EBYsx1QWF9A'
const interval = argv[1];
// Get info about video
const GetVideoInfo = async (url) => {
const res = await ytdl.getBasicInfo(url);
let info = {
title: res.videoDetails.title,
lengthSeconds: res.videoDetails.lengthSeconds,
formats: res.formats,
};
return info;
};
const GetFramePlan = async (interval, framesAmount) => {
let result = [];
for (let i = 1; i <= framesAmount; i++) {
result.push(i * interval);
}
return result;
};
const stuff = async (url, interval) => {
const videoInfo = await GetVideoInfo(url);
const framesAmount = Math.floor(videoInfo.lengthSeconds / interval);
const framePlan = await GetFramePlan(interval, framesAmount);
// Inform about task
console.log(`Start taking screenshots for '${videoInfo.title}'`);
process.stdout.write('>');
// choose format TODO: format needs to be smarter
const format = ytdl.chooseFormat(videoInfo.formats, { quality: '137' });
// Create folder if not exists
const framesFolder = path.join(__dirname, "frames");
if (!fs.existsSync(framesFolder)) {
fs.mkdirSync(framesFolder);
}
// Create screenshots
ffmpeg(format.url)
.on('error', (err) => {
console.log('Somthing went wrong!', err.message);
process.exit();
})
.on('progress', (progress) => {
process.stdout.write('-');
})
.on('end', () => {
process.stdout.write('>');
console.log('\nDone!');
process.exit();
})
.screenshots({
folder: framesFolder,
filename: videoInfo.title + '_%0i.png',
timestamps: framePlan,
});
}
const takeScreenshots = stuff(videoUrl.toString(), interval);
//console.log(process.argv, videoUrl, interval);