Created
December 3, 2020 12:26
-
-
Save MateusAquino/c1a204abeb76c2b2a51bb9987e5a763e to your computer and use it in GitHub Desktop.
This is a working example of how to use shazam's api + generate raw data with fluent-ffmpeg.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* How to use: | |
(async () => { | |
const Shazam = require('this_file.js') | |
const api = Shazam('your_token') | |
try { | |
const song_path = 'example.mp4' // input | |
const rawb64_path = 'example.raw' // output | |
const songName = await api.identify(song_path, rawb64_path) | |
console.log(songName) | |
} catch (err) { | |
console.error(err) | |
} finally { | |
// you might delete (unlink) 'example.raw' and 'example.mp4' here. | |
} | |
})(); | |
*/ | |
const ffmpeg = require('fluent-ffmpeg'); | |
const axios = require('axios'); | |
const fs = require('fs'); | |
class Shazam { | |
constructor(token) { | |
this.token = token; | |
} | |
static convertToRAWbase64(input, output, resolve, reject) { | |
ffmpeg(input) | |
.inputOptions([ | |
'-sseof', '-8s' | |
]) | |
.output(output) | |
.outputOptions([ | |
'-t', '5s', | |
'-acodec', 'pcm_s16le', | |
'-f', 's16le', | |
'-ac', '1', | |
'-ar', '44100' | |
]) | |
.on('error', function(err) { | |
console.error('An error occurred (FFMPEG): ' + err.message); | |
reject(); | |
}) | |
.on('end', async function() { | |
console.log('[Shazam] Processing finished!'); | |
const data = await fs.readFileSync(output).toString('base64'); | |
resolve(data); | |
}) | |
.run(); | |
} | |
search(rawBase64, resolve, reject) { | |
axios({ | |
'method': 'POST', | |
'url': 'https://shazam.p.rapidapi.com/songs/detect', | |
'headers': { | |
'x-rapidapi-host': 'shazam.p.rapidapi.com', | |
'x-rapidapi-key': this.token, | |
'useQueryString': true, | |
'content-type': 'text/plain' | |
}, | |
'data': rawBase64 | |
}) | |
.then(response=>{ | |
if (response.data && response.data.track) { // Musica encontrada | |
const {title, subtitle} = response.data.track; | |
resolve(subtitle + ' - ' + title); | |
} else if (response.data) { // Song not found | |
reject('Song not found') | |
} else { | |
console.error('Unexpected error from Shazam API') | |
reject(response ? response : 'Unexpected error from Shazam API') | |
} | |
}) | |
.catch(error=>{ | |
console.error('API Key failed:') | |
if (error.response && error.response.data) { | |
const msg = error.response.data.message; | |
console.error(msg); | |
if (msg && msg.includes('this API') || msg.includes('limit')) | |
reject('API Limit reached'); | |
else | |
reject('API offline'); | |
} | |
}); | |
} | |
async identify(input, output) { | |
try { | |
return new Promise((resolve, reject)=>{ | |
try { | |
this.convertToRAWbase64(input, | |
output, | |
data64 => this.search(data64, resolve, reject)); | |
} catch (err) { | |
reject(err.message); | |
} | |
}) | |
} catch (err) { | |
throw err; | |
} | |
} | |
} | |
module.exports = Shazam; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment