-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdecodeOpus.js
More file actions
83 lines (75 loc) · 2 KB
/
Copy pathdecodeOpus.js
File metadata and controls
83 lines (75 loc) · 2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
const fs = require('fs');
const path = require('path');
const opus = require('node-opus');
const rate = 48000;
const frame_size = 1920;
const channels = 2;
let total = 0;
let complete = 0;
let getDecodedFrame = (frameString, encoder, filename) => {
let buffer = Buffer.from(frameString, 'hex');
try {
buffer = encoder.decode(buffer, frame_size);
} catch (err) {
try {
buffer = encoder.decode(buffer.slice(8), frame_size);
} catch (err) {
console.log(`${filename} was unable to be decoded`);
return null;
}
}
return buffer;
};
let convertOpusStringToRawPCM = (inputPath, filename, cb) => {
total++;
let encoder = new opus.OpusEncoder(rate, channels);
const inputStream = fs.createReadStream(inputPath);
const outputStream = fs.createWriteStream(path.join(path.dirname(inputPath), `${filename}.raw_pcm`));
let data = '';
inputStream.on('data', chunk => {
data += chunk.toString();
const frames = data.split(',');
if (frames.length) {
data = frames.pop();
}
for (let frame of frames) {
if (frame !== '') {
const decodedBuffer = getDecodedFrame(frame, encoder, filename);
if (decodedBuffer) {
outputStream.write(decodedBuffer);
}
}
}
});
inputStream.on('end', () => {
outputStream.end((err) => {
if (err) {
console.error(err);
}
complete++;
console.log(`Completed ${100 * complete / total}%`);
});
});
console.log("not yet translating");
cb();
};
let convertAllOpusStringToRawPCM = (inputDirectory) => {
fs.readdir(inputDirectory, (err, files) => {
if (err) {
console.error(`Could not read input due to: ${err}`);
} else {
files.forEach((file) => {
let ext = path.extname(file);
if (ext === '.opus_string') {
convertOpusStringToRawPCM(path.join(inputDirectory, file), path.basename(file, ext));
}
});
}
});
};
//let inputDirectory = path.join('podcasts', process.argv[2]);
//convertAllOpusStringToRawPCM(inputDirectory);
module.exports = {
convertOpusStringToRawPCM: convertOpusStringToRawPCM
}