Trong bài viết này, mình sẽ chia sẻ cách bạn có thể tự tạo công cụ Text to Speech miễn phí, chỉ cần lấy API Key từ OpenAI hoặc Google AI Studio, là đã có thể chuyển đổi bất kỳ văn bản nào thành giọng đọc tự nhiên và tải về dưới dạng file MP3.
Lợi ích của công cụ Text to Speech AI
Chuyển văn bản thành giọng nói nhanh chóng.Hỗ trợ nhiều ngôn ngữ và giọng đọc tự nhiên.
Ứng dụng trong: làm video YouTube, podcast, thuyết minh tài liệu, học ngoại ngữ, v.v.
Hoạt động trực tiếp trên trình duyệt, không cần cài thêm phần mềm.
Chuẩn bị trước khi bắt đầu
API Key OpenAI: bạn có thể lấy tại OpenAI Platform.API Key Google AI Studio: đăng ký và lấy key tại Google AI Studio.
Trình duyệt web (Chrome/Edge/Firefox).
Code HTML + CSS + JavaScript mẫu
Bạn có thể copy toàn bộ đoạn code sau, lưu lại với tên txt-to-mp3.html rồi mở bằng trình duyệt:<html lang="vi"><head><meta charset="UTF-8"></meta><title>Text to Speech Tool</title><style>/* Reset + font */body {font-family: 'Inter', sans-serif;background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);color: #e5e5e5;margin: 0;padding: 0;min-height: 100vh;display: flex;justify-content: center;align-items: center;}/* Container chính */.container {width: 95%;max-width: 850px;background: rgba(255, 255, 255, 0.05);backdrop-filter: blur(12px);border-radius: 20px;padding: 30px;box-shadow: 0 8px 24px rgba(0,0,0,0.4);}/* Tiêu đề */h2 {text-align: center;font-size: 1.8rem;margin-bottom: 20px;background: linear-gradient(90deg, #00c6ff, #0072ff);-webkit-background-clip: text;-webkit-text-fill-color: transparent;}/* Label */label {font-weight: 500;margin-bottom: 6px;display: block;}/* Input chung */input, select, textarea {width: 100%;padding: 12px 14px;border: 1px solid rgba(255,255,255,0.15);border-radius: 12px;background: rgba(0,0,0,0.35);color: #fff;font-size: 1rem;transition: all 0.2s ease-in-out;margin-bottom: 18px;}input:focus, select:focus, textarea:focus {border-color: #00c6ff;box-shadow: 0 0 8px rgba(0,198,255,0.6);outline: none;}/* Textarea */textarea {min-height: 120px;resize: vertical;}/* Nút chính */button {width: 100%;padding: 14px;border: none;border-radius: 14px;background: linear-gradient(90deg, #00c6ff, #0072ff);color: #fff;font-size: 1.1rem;font-weight: 600;cursor: pointer;transition: transform 0.2s ease, box-shadow 0.2s ease;}button:hover {transform: translateY(-2px);box-shadow: 0 8px 20px rgba(0,114,255,0.5);}button:active {transform: translateY(0);box-shadow: none;}/* Responsive */@media (max-width: 600px) {.container {padding: 20px;}h2 {font-size: 1.5rem;}}</style></head><body><div class="container"><h2>🗣️ Text to Speech Tool</h2><textarea id="inputText" placeholder="Nhập hoặc dán văn bản ở đây..."></textarea><br /><select id="engine"><option value="openai">OpenAI TTS</option><option value="google">Google TTS</option></select><br /><button onclick="convertTTS()">▶️ Chuyển thành MP3</button><div id="result"></div></div><script>const OPENAI_API_KEY = "OPENAI_API_KEY"; // Thay bằng API Key OpenAIconst GOOGLE_API_KEY = "GOOGLE_API_KEY"; // Thay bằng API Key Google AI Studioasync function convertTTS() {const text = document.getElementById("inputText").value.trim();const engine = document.getElementById("engine").value;const resultDiv = document.getElementById("result");if (!text) {alert("❌ Hãy nhập văn bản!");return;}resultDiv.innerHTML = "⏳ Đang xử lý...";try {let audioBlob;if (engine === "openai") {// OpenAI TTS (gpt-4o-mini-tts)const response = await fetch("https://api.openai.com/v1/audio/speech", {method: "POST",headers: {"Authorization": `Bearer ${OPENAI_API_KEY}`,"Content-Type": "application/json"},body: JSON.stringify({model: "gpt-4o-mini-tts",voice: "alloy", // các giọng: alloy, verse, coral, ...input: text})});const arrayBuffer = await response.arrayBuffer();audioBlob = new Blob([arrayBuffer], { type: "audio/mp3" });} else if (engine === "google") {// Google TTSconst response = await fetch(`https://texttospeech.googleapis.com/v1/text:synthesize?key=${GOOGLE_API_KEY}`, {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({input: { text: text },voice: { languageCode: "vi-VN", name: "vi-VN-Wavenet-A" },audioConfig: { audioEncoding: "MP3" }})});const data = await response.json();const audioBytes = data.audioContent;audioBlob = new Blob([Uint8Array.from(atob(audioBytes), c => c.charCodeAt(0))], { type: "audio/mp3" });}// Tạo URL cho audioconst url = URL.createObjectURL(audioBlob);resultDiv.innerHTML = `✅ Hoàn tất!<br><audio controls src="${url}"></audio><br><a href="${url}" download="speech.mp3">⬇️ Tải về MP3</a>`;} catch (error) {console.error(error);resultDiv.innerHTML = "❌ Lỗi: " + error.message;}}</script></body></html>
Video sẽ hướng dẫn bạn từ A đến Z, bao gồm:
- Cách đăng ký và lấy API Key từ Google AI Studio.
- Cách lấy API Key từ OpenAI một cách đơn giản.
- Code mẫu chi tiết giúp bạn dễ dàng tích hợp Text to Speech vào dự án cá nhân.
- Xuất file âm thanh .mp3 với giọng đọc tự nhiên, nhiều ngôn ngữ hỗ trợ.
- Ứng dụng thực tế: đọc truyện, tạo video thuyết minh, tạo giọng đọc nhân vật, và nhiều hơn nữa.
Demo sử dụng
- Nhập văn bản (tiếng Việt hoặc tiếng Anh).
- Chọn engine: OpenAI TTS hoặc Google AI TTS.
- Bấm Chuyển thành MP3 → Hệ thống sẽ tạo file giọng đọc.
- Có thể nghe trực tiếp hoặc tải về MP3.
Kết luận
Với vài bước đơn giản, bạn đã có thể tạo công cụ Text to Speech miễn phí cực kỳ tiện lợi, áp dụng cho học tập, làm video, thuyết minh hay tạo podcast.👉 Hãy thử ngay và khám phá sức mạnh AI!
Bạn có thể tùy chỉnh giọng đọc, ngôn ngữ, tốc độ đọc... để phù hợp hơn với nhu cầu cá nhân.
Đăng nhận xét