Skip to content

Instantly share code, notes, and snippets.

@avirajkhare00
Created September 30, 2022 18:15
Show Gist options
  • Save avirajkhare00/b83f1cb8d54d872c260aa7a883e67bff to your computer and use it in GitHub Desktop.
Save avirajkhare00/b83f1cb8d54d872c260aa7a883e67bff to your computer and use it in GitHub Desktop.
const puppeteer = require('puppeteer');
const axios = require('axios');
const fs = require('fs');
(async () => {
const url = process.argv[2];
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const textToBeConverted = await page.evaluate(() => Array.from(document.querySelectorAll('p'), element => element.textContent));
textToBeConverted.forEach(e => {
// downloadSpeechFromFile("demo.wav", textToBeConverted)
})
downloadSpeechFromFile("demo.wav", JSON.stringify({"text": "Hello my name is Aviraj Khare. I am going to be 27 years old."}))
await browser.close();
})();
function downloadSpeechFromFile(fileName, textToBeConverted) {
const response = axios({
url: "https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/{some_uuid}/v1/synthesize",
method: 'post',
responseType: 'stream',
auth: {
username: "apikey",
password: "{password}"
},
headers: {
"Content-Type": "application/json",
"Accept": "audio/wav"
},
params: {
"voice": "en-US_MichaelV3Voice"
},
data: {
"text": "Hello my name is Aviraj Khare. I am from India."
}
}).then(function (response) {
response.data.pipe(fs.createWriteStream(fileName));
})
.catch(function (err) {
console.error(err);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment