updated txt to mp3 pyton code , for female voice.
error :
showing, _InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.INVALID_ARGUMENT
details = "Requested male voice, but voice en-IN-Neural2-A is a female voice."
debug_error_string = "UNKNOWN:Error received from peer ipv4:74.125.20.95:443 {created_time:"2024-09-18T14:13:09.869505147+00:00", grpc_status:3, grpc_message:"Requested male voice, but voice en-IN-Neural2-A is a female voice."}"
>
The above exception was the direct cause of the following exception:
InvalidArgument Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/google/api_core/grpc_helpers.py in error_remapped_callable(*args, **kwargs)
76 return callable_(*args, **kwargs)
77 except grpc.RpcError as exc:
---> 78 raise exceptions.from_grpc_error(exc) from exc
79
80 return error_remapped_callable
InvalidArgument: 400 Requested male voice, but voice en-IN-Neural2-A is a female voice.
--
replaced with
ssml_gender=texttospeech.SsmlVoiceGender.MALE
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE
import gc
gc.collect()
!pip install google-cloud-texttospeech pydub
import json
from google.cloud import texttospeech
from pydub import AudioSegment
import os
# Paths to the files
credentials_path = '/content/credentials.json'
text_file_path = 'Cloud_తైతాకార రహస్యము త్రైతాకార బెర్ముడా_English_updated_18thsep24.txt'
output_file_path = 'Cloud_తైతాకార రహస్యము త్రైతాకార బెర్ముడా_English_updated_18thsep24_English2_audio.mp3'
# Verify the file existence
if os.path.exists(credentials_path):
print("Credentials file found.")
else:
print("Credentials file not found.")
if os.path.exists(text_file_path):
print("Text file found.")
else:
print("Text file not found.")
# Load your Google Cloud project credentials
with open(credentials_path) as f:
credentials = json.load(f)
# Configure the Text-to-Speech client
client = texttospeech.TextToSpeechClient.from_service_account_json(credentials_path)
# Load Telugu 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):
"""Split text into chunks of max_bytes bytes."""
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
voice = texttospeech.VoiceSelectionParams(
language_code='en-IN', # code : te-IN ,Telugu (India) Link:https://cloud.google.com/text-to-speech/docs/voices
name='en-IN-Neural2-A', # Male voice te-IN-Standard-B ;felmale : te-IN-Standard-A;
#en-IN-Standard-A female ,code: en-IN
#hi-IN-Standard-A FEMALE hindi , hindi code: hi-IN
#ssml_gender=texttospeech.SsmlVoiceGender.MALE
ssml_gender=texttospeech.SsmlVoiceGender.FEMALE
)
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3,
speaking_rate=1 # Normal speed, 1.0 is default 0.25,0.5,0.75,1 (normal), 1.25, 1.5 , 1.75, 2
)
# Function to synthesize speech for each chunk
def synthesize_speech(text_chunk):
synthesis_input = texttospeech.SynthesisInput(text=text_chunk)
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
return response.audio_content
# Function to save audio content to a temporary file
def save_temp_audio(audio_content, temp_file_path):
with open(temp_file_path, 'wb') as out:
out.write(audio_content)
# 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)
audio_content = synthesize_speech(chunk)
save_temp_audio(audio_content, 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)
---