is the following python code removing any ssml tags from the source json file before…
- User
is the following python code removing any ssml tags from the source json file before synthesizing it:
import boto3 import json import os import shutil # Import shutil for file operations from pydub import AudioSegment from pydub.exceptions import CouldntDecodeError
Configure AWS Polly client
Note: Ensure that your AWS credentials are configured in your environment.
polly_client = boto3.Session().client('polly')
Define speaker voice mapping
SPEAKER_VOICES = { "Speaker 1": "Stephen", # Replace with desired neural voices "Speaker 2": "Ruth" }
Define directories for temporary files
AUDIO_OUTPUT_DIR = r'D:\Podcast\audio_output' TEMP_AUDIO_DIR = r'D:\Podcast\temp_audio'
Create directories if they don't exist
os.makedirs(AUDIO_OUTPUT_DIR, exist_ok=True) os.makedirs(TEMP_AUDIO_DIR, exist_ok=True)
def synthesize_speech(text, voice_id, output_filename): """ Synthesizes speech using Amazon Polly and saves it to a file. """ try: response = polly_client.synthesize_speech( Text=text, VoiceId=voice_id, OutputFormat='mp3', Engine='generative' # Use the 'neural' engine for Polly ) # Save the audio stream returned by Amazon Polly with open(output_filename, 'wb') as file: file.write(response['AudioStream'].read()) return output_filename except Exception as e: print(f"Error synthesizing speech: {e}") return None
def process_segment(segment): """ Processes a segment by synthesizing audio for the content item. """ segment_audio = None content_items = segment.get('content', []) if not content_items: return None # Assuming only one content item per segment content = content_items[0] if content.get('type') == 'dialogue': speaker_id = content.get('speaker_id') text = content.get('text') voice_id = SPEAKER_VOICES.get(speaker_id, 'Joanna') temp_audio_file = os.path.join( TEMP_AUDIO_DIR, f"segment_{segment['segment_number']}_{speaker_id}.mp3") synthesize_speech(text, voice_id, temp_audio_file) try: segment_audio = AudioSegment.from_mp3(temp_audio_file) except CouldntDecodeError: print(f"Could not decode audio file {temp_audio_file}") segment_audio = None # Annotations are skipped return segment_audio
def main(): # Load the JSON data with open(r'D:\Podcast\podcast_script_natural.json', 'r', encoding='utf-8') as file: podcast_data = json.load(file)
code - Assistant