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)


only list of lanugaes and support speed and mode(male/female) information fro above?
ChatGPT said:

The gTTS (Google Text-to-Speech) library is a wrapper over the Google Translate TTS API, and has the following characteristics in terms of languages, speed, and voice types:


Supported Languages (with codes)

Here are some commonly supported languages and their codes used with gTTS(lang=...):

LanguageCodeTLD Option (Accent)
Englishencom, co.uk, co.in
Hindihi-
Telugute-
Tamilta-
Kannadakn-
Malayalamml-
Gujaratigu-
Bengalibn-
Marathimr-
Urduur-
Spanishescom.mx, es
Frenchfrfr, ca
Germande-
Japaneseja-
Chinese (Mandarin)zh-CN-
Russianru-

To get the complete list via code:

python
from gtts.lang import tts_langs print(tts_langs())

🐢 Speed Support

gTTS supports two speed modes only:

  • slow=False → Normal speed (default)

  • slow=True → Slower voice output (about 60% of original)

No support for precise or dynamic speed control.


👤 Voice Mode (Male/Female)

  • Only one voice (Female) is supported per language.

  • You cannot switch between male/female voices using gTTS.

  • Voice tone and accent might vary slightly with tld (region) but still female.


🔁 Summary Table

FeatureOptions / Notes
Languages~100+ (English, Telugu, Hindi, etc.)
Speedslow=True (slow), slow=False (normal)
GenderFemale only
Accent controlUse tld='com', 'co.uk', 'co.in', etc.

Popular posts from this blog

SAP CPI : camle expression in sap cpi , cm, router, filter and groovy script. format

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024