UNPKG

base64-audio-mime

Version:
31 lines (27 loc) 784 B
// index.js const atob = require('atob'); const Buffer = require('buffer/').Buffer; const mimeTypes = { mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg', aac: 'audio/aac' } function getAudioMime(base64Encoded) { if (base64Encoded.startsWith('data:')) { const found = base64Encoded.match(/data:\S*;base64/g); return found && found[0].slice('data:'.length, ';base64'.length * -1); } else { const prefix = atob(base64Encoded.slice(0, 60)); const found = prefix.match(/(mp3)|(wav)|(ogg)|(aac)/gi); if (!found) { return null; } else { const type = found[0].toLocaleLowerCase(); return mimeTypes[type]; } } } module.exports = { getAudioMime: getAudioMime };