grok, txt to mp3 worked without google with some limitations
code:
!pip install gTTS pydub
from gtts import gTTS
from pydub import AudioSegment
import os
# Paths to the files
text_file_path = '/content/pssvisitor.txt'
output_file_path = '/content/pssvisitor_.mp3'
# Verify the file existence
if not os.path.exists(text_file_path):
raise FileNotFoundError(f"Text file {text_file_path} not found")
# Load text from the file
with open(text_file_path, 'r', encoding='utf-8') as file:
text = file.read().strip()
# Function to split text into chunks based on byte size
def split_text(text, max_bytes=5000):
chunks = []
current_chunk = ""
current_chunk_bytes = 0
for char in text:
char_bytes = len(char.encode('utf-8'))
if current_chunk_bytes + char_bytes > max_bytes:
chunks.append(current_chunk)
current_chunk = char
current_chunk_bytes = char_bytes
else:
current_chunk += char
current_chunk_bytes += char_bytes
if current_chunk:
chunks.append(current_chunk)
return chunks
# Configure voice and speed
language = 'te' # Telugu ('te'), can switch to 'en' for English
voice_gender = 'male' # gTTS does not support gender selection; defaults to neutral/male-like
speaking_rate = 1.0 # Normal speed: 0.5 (slow), 1.0 (normal), 1.5 (fast)
# Function to synthesize speech for each chunk
def synthesize_speech(text_chunk, temp_file_path):
tts = gTTS(text=text_chunk, lang=language, slow=(speaking_rate < 1.0))
tts.save(temp_file_path)
# Process text and combine audio files
text_chunks = split_text(text)
temp_files = []
for i, chunk in enumerate(text_chunks):
temp_file_path = f'/content/temp_chunk_{i}.mp3'
temp_files.append(temp_file_path)
synthesize_speech(chunk, temp_file_path)
# Combine audio files
combined = AudioSegment.empty()
for temp_file in temp_files:
audio_segment = AudioSegment.from_mp3(temp_file)
combined += audio_segment
# Export the combined audio to the final output file
combined.export(output_file_path, format='mp3')
print(f'Audio content written to file "{output_file_path}"')
# Clean up temporary files
for temp_file in temp_files:
os.remove(temp_file)