Text to Speech Video Creator

Text to Speech Video Creator


// Select elements const textInput = document.getElementById('text-input'); const speakButton = document.getElementById('speak-btn'); const downloadButton = document.getElementById('download-btn'); const audioOutput = document.getElementById('audio-output'); // Text-to-Speech function function textToSpeech(text) { const speechSynthesis = window.speechSynthesis; const utterance = new SpeechSynthesisUtterance(text); utterance.onend = () => { console.log('Speech synthesis completed.'); }; speechSynthesis.speak(utterance); // Convert to audio blob const mediaRecorder = new MediaRecorder(new MediaStream()); mediaRecorder.start(); mediaRecorder.ondataavailable = function (event) { const audioBlob = event.data; const audioUrl = URL.createObjectURL(audioBlob); audioOutput.src = audioUrl; // Enable download button downloadButton.addEventListener('click', () => { const a = document.createElement('a'); a.href = audioUrl; a.download = 'speech.wav'; a.click(); }); }; utterance.addEventListener('start', () => { mediaRecorder.start(); }); utterance.addEventListener('end', () => { mediaRecorder.stop(); }); } // Event listener for the speak button speakButton.addEventListener('click', () => { const text = textInput.value; if (text) { textToSpeech(text); } else { alert('Please enter some text to convert to speech.'); } });

Comments

Popular posts from this blog