-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
93 lines (88 loc) · 3.32 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const asyncPool = require('tiny-async-pool');
const poolLimit = 8;
const ffmpeg = require('fluent-ffmpeg');
const fs = require('fs')
const parse = require('csv-parse')
const getStream = require('get-stream');
const readCSVData = async filePath => (await getStream.array(fs.createReadStream(filePath).pipe(parse({ delimiter: ',' }))))
async function main() {
let res = []
for (let [pinyin, startTime, endTime] of (await readCSVData('./original/HowHow 發音標註眾包 - 發音表.csv'))) {
startTime = parseFloat(startTime) + 0.1
endTime = parseFloat(endTime) - 0.1
pinyin = pinyin == '' ? `沒有拼音 - ${Math.random().toString(36).substring(7)}` : pinyin
if (pinyin.length > 30)
pinyin = pinyin.slice(0, 30)
res.push({ pinyin, startTime, endTime })
}
res.shift();
console.log('csv 檔案解析完成')
//create folder
function rmdir(d) {
let self = arguments.callee
if (fs.existsSync(d)) {
fs.readdirSync(d).forEach(file => {
let C = d + '/' + file
if (fs.statSync(C).isDirectory()) self(C)
else fs.unlinkSync(C)
})
fs.rmdirSync(d)
}
}
rmdir('./result');
for (let folder of ['./result',]) {
fs.mkdirSync(folder);
for (let folder2 of ['/mp3/', '/mp4/']) {
fs.mkdirSync(folder + folder2);
for (let folder3 of [1, 2]) {
fs.mkdirSync(folder + folder2 + folder3);
}
}
}
console.log('ffmpeg 處理中...')
let counter
counter = 0
let parseMp3 = ({ pinyin, startTime, endTime, counter }) => new Promise((resolve, reject) => {
ffmpeg('./original/howhow.mp3')
.setStartTime(startTime)
.setDuration(endTime - startTime)
.on('error', (err) => {
console.error(`[ffmpeg] error: ${err.message}`);
reject(err);
})
.on('end', () => {
console.log(`(${counter} of ${res.length}) [ffmpeg] ${pinyin}.mp3 finished`);
resolve();
})
.save(`./result/mp3/${Math.ceil(counter / 1000)}/${pinyin}.mp3`)
});
let pool_mp3 = []
for (let { pinyin, startTime, endTime } of res) {
counter++
pool_mp3.push({ pinyin, startTime, endTime, counter })
}
await asyncPool(poolLimit, pool_mp3, parseMp3)
counter = 0
let parseMp4 = ({ pinyin, startTime, endTime, counter }) => new Promise((resolve, reject) => {
ffmpeg('./original/howhow.mp4')
.outputOptions('-movflags frag_keyframe+empty_moov+default_base_moof')
.setStartTime(startTime)
.setDuration(endTime - startTime)
.on('error', (err) => {
console.error(`[ffmpeg] error: ${err.message}`);
reject(err);
})
.on('end', () => {
console.log(`(${counter} of ${res.length}) [ffmpeg] ${pinyin}.mp4 finished`);
resolve();
})
.save(`./result/mp4/${Math.ceil(counter / 1000)}/${pinyin}.mp4`)
});
let pool_mp4 = []
for (let { pinyin, startTime, endTime } of res) {
counter++
pool_mp4.push({ pinyin, startTime, endTime, counter })
}
await asyncPool(poolLimit, pool_mp4, parseMp4)
}
main()