-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp3ToGoogleDoc.js
104 lines (90 loc) · 3.59 KB
/
mp3ToGoogleDoc.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
94
95
96
97
98
99
100
101
102
103
104
const musicFolderID = process.env.MUSIC_FOLDER_ID;
const fs = require('fs');
const { google } = require('googleapis');
const Promise = require('bluebird');
const scopes = ['https://www.googleapis.com/auth/drive'];
let credentials;
let drive;
const ffmpeg = require('fluent-ffmpeg');
let track = process.argv[2];
let slashSplit = track.split('/');
let fileName = slashSplit[slashSplit.length - 1].split('.mp3')[0];
/*
Next steps:
- Make the file a script that accepts an MP3 file path as an argument- DONE
- Match the title of the files uploaded to Google Docs with the audio file name- DONE
- At the end of the script, delete the intermediate .wav and .bin files- DONE
Upload script sufficient for now. Next goal: dynamic endpoint on Daily Page, such as
/mp3/filename_without_extension.
It will get the list of files from the music folder, find the ones that match the file
name, download them, concatenate them, decode the base-64 string, and serve up
the resulting WAV file. Later, figure out how to make the player navigate
automatically from track to track.
*/
ffmpeg(track)
.toFormat('wav')
.on('error', (err) => {
console.log('An error occurred: ' + err.message);
})
.on('progress', (progress) => {
// console.log(JSON.stringify(progress));
console.log('Processing: ' + progress.targetSize + ' KB converted');
})
.on('end', () => {
console.log('Processing finished!');
var bitmap = fs.readFileSync(`./${fileName}.wav`);
fs.writeFileSync(`${fileName}.bin`, Buffer.from(bitmap).toString('base64'));
credentials = JSON.parse(fs.readFileSync('./credentials.json'));
auth = new google.auth.JWT(credentials.client_email, null, credentials.private_key, scopes);
drive = google.drive({ version: 'v3', auth });
var fileMetadata = {
'name': 'audioAsText',
driveId: musicFolderID,
parents: [musicFolderID],
mimeType: 'application/vnd.google-apps.document',
};
let txt = fs.readFileSync(`${fileName}.bin`);
(async () => {
const chunkSize = 1500000;
const chunks = [];
const numChunks = Math.ceil(txt.toString().length / chunkSize);
for (let index = 0; index < numChunks; index += 1) {
chunks.push(txt.toString().slice(index*chunkSize, (index + 1) * chunkSize));
}
console.log('how many chunks?');
console.log(chunks.length);
console.log('how many chars to chunk?');
console.log(txt.toString().length);
chunks.forEach(myChunk => {
console.log(`indiv chunk length: ${myChunk.length}`);
console.log(`start of chunk: ${myChunk.slice(0, 8)}`);
console.log(`chunk end: ${myChunk.slice(myChunk.length - 8, myChunk.length)}`);
});
await Promise.each(chunks, async (chunk, index) => {
fileMetadata.parents = [musicFolderID];
console.log(`creating file for chunk #${index}`);
fileMetadata.name = `${fileName}_${index}`;
let createdFile = await drive.files.create({
resource: fileMetadata,
// media: media,
fields: 'id',
supportsAllDrives: true
});
delete fileMetadata.parents;
await drive.files.update({
fileId: createdFile.data.id,
resource: fileMetadata,
uploadType: 'resumable',
media: {
body: chunk,
mimeType: 'text/plain'
}
});
});
fs.unlinkSync(`${fileName}.wav`);
fs.unlinkSync(`${fileName}.bin`);
console.log('entire byte size:');
console.log(Buffer.from(bitmap).byteLength);
})();
})
.save(`./${fileName}.wav`);//path where you want to save your file