-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscript.js
More file actions
59 lines (54 loc) · 2.23 KB
/
transcript.js
File metadata and controls
59 lines (54 loc) · 2.23 KB
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
var getSubtitles = require('youtube-captions-scraper').getSubtitles;
const fs = require('fs')
// requires a file with a list of youtube videos
// write to JSON folder for json raw data from endpoint
// write to TEXT folder with .txt file with video caption.
// return 0 if completed succesfully.
async function getCaptions(youtubers, mainDirName) {
try {
if (!fs.existsSync(`../${mainDirName}`)){
fs.mkdirSync(mainDirName);
}
if (!fs.existsSync(`../${mainDirName}/JSON`)){
fs.mkdirSync(`../${mainDirName}/JSON`);
}
if (!fs.existsSync(`../${mainDirName}/TEXT`)){
fs.mkdirSync(`../${mainDirName}/TEXT`);
}
for (const video of youtubers) {
if (!video.id) {
console.error("no id found.");
continue;
}
getSubtitles({
videoID: video.id, // youtube video id
lang: 'en' // default: `en`
}).then(captions => {
var captionStrings = captions.map((cap) => {return cap.text});
var captionString = captionStrings.join(" ");
const jsonCAPTION = JSON.stringify(captions, null, '\t');
fs.writeFile(`../${mainDirName}/JSON/${video.name}.json`, jsonCAPTION, err => {
if (err) {
console.error(`Error writing file ${video.name}.json`, err)
} else {
console.log(`Successfully wrote ${video.name}.json file`)
}
})
fs.writeFile(`../${mainDirName}/TEXT/${video.name}.txt`, captionString, err => {
if (err) {
console.error(`Error writing file ${video.name}.txt`, err)
} else {
console.log(`Successfully wrote ${video.name}.txt file`)
}
})
}).catch(() => console.error(`Could not find captions for video: ${video.name}`));
}
return 0;
} catch (err) {
console.error(err);
process.exit(1);
}
}
module.exports = {
getCaptions
};