# app.py import streamlit as st import os import base64 import json import time import shutil from datetime import datetime import yt_dlp from urllib.parse import urlparse, parse_qs # Google Cloud & Vertex AI specific imports from google.oauth2 import service_account from google.cloud import texttospeech from vertexai.preview.generative_models import GenerativeModel, GenerationConfig, Part, FinishReason, HarmCategory, HarmBlockThreshold import vertexai # MoviePy specific imports from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip from moviepy.audio.fx.all import volumex from moviepy.video.fx.all import speedx # --- Configuration Data (Copied from Notebook) --- language_mapping = { "English": "en-US", "Vietnamese": "vi-VN", "Mandarin": "cmn-CN", "Thai": "th-TH", "Indonesian": "id-ID", "Malay": "ms-MY", "Filipino": "fil-PH", "Japanese": 'ja-JP', "Korean": "ko-KR", "Portugese": "pt-BR", "Spanish": "es-ES", "Hindi": "hi-IN", "Arabic": "ar-XA", "German": "de-DE", # Added Thai again as it appeared twice in the original # Ensure keys are unique if copy-pasting } language_voice_mapping = { "Male": { "Vietnamese": "vi-VN-Wavenet-B", "Mandarin": "cmn-CN-Wavenet-B", # "Thai": "th-TH-Standard-A", # Original standard "Indonesian": "id-ID-Wavenet-B", "Malay": "ms-MY-Wavenet-B", "Filipino": "fil-PH-Wavenet-C", "English": "en-US-Journey-D", # Using Journey voice from original "Spanish": "es-ES-Standard-B", "Hindi": "hi-IN-Standard-B", # Corrected: Use Standard-B for Male Hindi "Arabic": "ar-XA-Standard-B", "German": "de-DE-Standard-B", "Thai": "th-TH-Standard-A", # Example male voice for Thai (check availability) "Japanese": "ja-JP-Wavenet-C", # Example male voice "Korean": "ko-KR-Wavenet-C", # Example male voice "Portugese": "pt-BR-Wavenet-B", # Example male voice }, "Female": { "Vietnamese": "vi-VN-Wavenet-A", "Mandarin": "cmn-CN-Wavenet-A", "Thai": "th-TH-Standard-A", # Original had Chirp, using Standard as example "Indonesian": "id-ID-Wavenet-A", "Malay": "ms-MY-Wavenet-A", "Filipino": "fil-PH-Wavenet-A", "English": "en-US-Journey-F", # Using Journey voice from original "Japanese": "ja-JP-Neural2-B", "Korean": "ko-KR-Neural2-A", "Portugese": "pt-BR-Neural2-C", "Spanish": "es-ES-Standard-A", "Hindi": "hi-IN-Neural2-A", "Arabic": "ar-XA-Standard-A", "German": "de-DE-Standard-A", # Ensure all selected languages have a mapping } } # Add mappings for languages if they are missing in the default dict for a selected gender # Example: Adding missing Portugese Male if needed (assuming pt-BR-Wavenet-B is valid) # if "Portugese" not in language_voice_mapping["Male"]: # language_voice_mapping["Male"]["Portugese"] = "pt-BR-Wavenet-B" wpm_config = { "English": "100 WPM", "Vietnamese": "100 WPM", "Mandarin": "100 WPM", # Note: Original had Chinese: 200 WPM, clarifying here "Indonesian": "100 WPM", "Malay": "100 WPM", "Filipino": "100 WPM", "Korean": "200 WPM", "Japanese": "200 WPM", # Original had Japanes, corrected spelling "Portugese": "200 WPM", "Hindi": "200 WPM", "Thai": "100 WPM", "Spanish": "100 WPM", "Arabic": "100 WPM", "German": "100 WPM", } voice_tone_prompt_config = { "Chill": "Adopt a chill, relaxing tone throughout the script", "Energetic": "Adopt a fun, energetic, casual, and engaging tone throughout the script", } type_of_app_prompt_config = { "Gaming": """Talk to your viewer about the game while you play this game as recorded in this video. The transcript should be synchronized with the video's visuals. Quickly ask them to download the game in the end.""", "Utility": """You are a senior copywriter specializing in creating engaging voice-over scripts for video advertisements. Your task is to write a transcript for a video ad campaign, highlighting the key benefits of the product. Quickly ask them to download the game in the end. Adopt a fun, energetic, casual, and engaging tone throughout the script.""" } # --- Helper Functions (Adapted from Notebook) --- # Function to load credentials safely @st.cache_resource # Cache the credentials object def load_credentials(credentials_info): try: return service_account.Credentials.from_service_account_info(credentials_info) except Exception as e: st.error(f"Error loading credentials: {e}") return None # Function to initialize Vertex AI safely def initialize_vertexai(project_id, location, credentials): try: vertexai.init(project=project_id, location=location, credentials=credentials) st.success(f"Vertex AI initialized for project '{project_id}' in '{location}'.") return True except Exception as e: st.error(f"Error initializing Vertex AI: {e}") return False def encode_file(file_path: str): try: with open(file_path, "rb") as media_file: encoded_string = base64.b64encode(media_file.read()).decode('utf-8') # Decode for JSON compatibility if needed later return encoded_string except FileNotFoundError: st.error(f"Error encoding: File not found at {file_path}") return None except Exception as e: st.error(f"Error encoding file {file_path}: {e}") return None def generate_text(app_info, locales, video_path, ad_duration_sec, voice_tone, type_of_app, custom_transcript_prompt, skip_intro=True, credentials=None): # Pass credentials """Generates the voice-over transcript for the video in multiple languages""" st.info("Generating transcripts using Gemini...") progress_bar_text = st.progress(0, text="Encoding video...") try: # Initialize the GenerativeModel client *inside* the function if not already done globally # This ensures it uses the provided credentials for this specific call model = GenerativeModel("gemini-1.5-pro-001", credentials=credentials) # Use specific model, pass credentials my_encoded_file = encode_file(video_path) if not my_encoded_file: st.error("Failed to encode video file.") return None # Or handle error appropriately progress_bar_text.progress(10, text="Video encoded. Preparing prompt...") video = Part.from_data( mime_type="video/mp4", data = base64.b64decode(my_encoded_file)) # Decode back to bytes for the Part app_prompt = type_of_app_prompt_config.get(type_of_app, "") # Use .get for safety voice_tone_prompt = voice_tone_prompt_config.get(voice_tone, "") wpm_prompt = "" for lang in locales: # Iterate through selected locales only if lang in wpm_config: wpm_prompt += f"{lang}: {wpm_config[lang]}\\n" else: st.warning(f"WPM config not found for {lang}. Using default estimate.") # Add a default or skip if needed wpm_prompt += f"{lang}: 100 WPM\\n" # Example default skip_intro_prompt = "Skip the introduction of the transcript." if skip_intro else "" locale_list_str = ", ".join(locales) prompt = f""" {app_prompt} {skip_intro_prompt} {custom_transcript_prompt} The duration of the video is approximately {ad_duration_sec:.2f} seconds. Follow the steps: 1. Calculate the maximum words for the transcript for *each requested language* based on its corresponding speaking pace (word per minute - WPM) and the video duration. Use these WPM guidelines: {wpm_prompt} 2. Generate a voice-over transcript for *each requested language* strictly within its calculated maximum word count. The tone of the transcript should be {voice_tone}. Ensure the content aligns with the video visuals. 3. Respond ONLY with a valid JSON object containing the voice-over script for all requested languages. Do not include visual cues, markdown formatting (like ```json), or any text outside the JSON object itself. The JSON keys should be the language names (e.g., "English", "Spanish"). Example format: {{ "English": "The English script goes here...", "Spanish": "El guión en español va aquí...", "French": "Le script en français va ici..." }} Here is additional information about the product/app: {app_info} Requested languages: {locale_list_str} """ progress_bar_text.progress(30, text="Prompt prepared. Calling Generative Model...") contents = [video, prompt] generation_config = GenerationConfig( max_output_tokens=8192, temperature=0.8, top_k=32, top_p=1, ) # Safety settings (as defined in notebook) safety_config = [ SafetySetting(category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT, threshold=HarmBlockThreshold.BLOCK_NONE), SafetySetting(category=HarmCategory.HARM_CATEGORY_HARASSMENT, threshold=HarmBlockThreshold.BLOCK_NONE), SafetySetting(category=HarmCategory.HARM_CATEGORY_HATE_SPEECH, threshold=HarmBlockThreshold.BLOCK_NONE), SafetySetting(category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT, threshold=HarmBlockThreshold.BLOCK_NONE), ] responses = model.generate_content( contents, generation_config=generation_config, safety_settings=safety_config, stream=False, ) progress_bar_text.progress(80, text="Response received. Processing...") # --- Response Cleaning and Parsing --- raw_response_text = responses.text st.text("Raw LLM Response:") # For debugging st.code(raw_response_text, language=None) # Attempt to clean and parse the JSON try: # Basic cleaning: remove markdown backticks and 'json' identifier cleaned_response = raw_response_text.strip().lstrip('```json').lstrip('```').rstrip('```') # Find the first '{' and last '}' to extract the JSON part start_index = cleaned_response.find('{') end_index = cleaned_response.rfind('}') if start_index != -1 and end_index != -1 and end_index > start_index: json_string = cleaned_response[start_index:end_index+1] parsed_json = json.loads(json_string) progress_bar_text.progress(100, text="Transcripts generated successfully!") st.success("Transcripts generated.") return parsed_json else: st.error("Could not find valid JSON structure in the response.") st.text("Cleaned Response Attempt:") st.code(cleaned_response, language=None) return None except json.JSONDecodeError as json_err: st.error(f"Failed to parse LLM response as JSON: {json_err}") st.text("Attempted to parse:") st.code(json_string if 'json_string' in locals() else cleaned_response, language=None) return None except Exception as e: st.error(f"An unexpected error occurred during text generation or parsing: {e}") return None except Exception as e: st.error(f"Error during transcript generation: {e}") # Log error details if running in a server environment # Log the prompt for debugging st.text("Prompt used for generation:") st.code(prompt) return None finally: # Ensure progress bar text is cleared or set to final state if 'progress_bar_text' in locals(): progress_bar_text.empty() # Remove the progress bar def generate_speech(voice_name: str, language_code: str, text: str, file_path: str, gender: str, # Pass gender to select SSML gender pitch = 0, volume_gain_db = 0, speaking_rate = 1, credentials=None) -> bool: # Pass credentials, return success status """Generates speech from text using Google Text-to-Speech""" st.info(f"Generating audio for {language_code}...") try: client = texttospeech.TextToSpeechClient(credentials=credentials) # Pass credentials input_text = texttospeech.SynthesisInput(text=text) # Determine SSML gender based on input string ssml_gender_enum = texttospeech.SsmlVoiceGender.MALE if gender == "Male" else texttospeech.SsmlVoiceGender.FEMALE voice = texttospeech.VoiceSelectionParams( language_code=language_code, name=voice_name, ssml_gender=ssml_gender_enum, ) audio_config = texttospeech.AudioConfig( audio_encoding=texttospeech.AudioEncoding.MP3, speaking_rate=speaking_rate, pitch=pitch, volume_gain_db=volume_gain_db, ) response = client.synthesize_speech( request={"input": input_text, "voice": voice, "audio_config": audio_config} ) # The response's audio_content is binary. with open(file_path, 'wb') as out: out.write(response.audio_content) st.success(f"Audio file saved: {os.path.basename(file_path)}") return True except Exception as e: st.error(f"Error generating speech for {language_code} ({voice_name}): {e}") # Check if it's a voice/gender mismatch if "Requested" in str(e) and ("voice" in str(e) or "gender" in str(e)): st.warning(f"Potential voice/gender mismatch for {language_code} - Voice: {voice_name}, Gender: {gender}. Please check 'language_voice_mapping'.") # Log the error with more details if needed with open(f'audio_error_logs.txt', 'a') as f: f.write(f'{datetime.now()} | Lang: {language_code}, Voice: {voice_name}, Error: {e}\n') return False def generate_audio_files(folder_path, transcript, locale, gender, credentials): # Pass credentials """Selects voice and calls generate_speech""" try: # Check if the locale and gender combination exists in the mapping if gender in language_voice_mapping and locale in language_voice_mapping[gender]: voice_name = language_voice_mapping[gender][locale] else: st.error(f"Voice mapping not found for Language: {locale}, Gender: {gender}. Skipping audio generation.") return None # Indicate failure # Check if the locale exists in the language code mapping if locale in language_mapping: language_code = language_mapping[locale] else: st.error(f"Language code not found for Language: {locale}. Skipping audio generation.") return None # Indicate failure file_name = f"{locale}-{gender}.mp3" file_path = os.path.join(folder_path, file_name) success = generate_speech( voice_name=voice_name, language_code=language_code, text=transcript, file_path=file_path, gender=gender, credentials=credentials # Pass credentials down ) return file_path if success else None # Return path only on success except KeyError as e: st.error(f"Configuration error: Missing mapping for {e} (Language: {locale}, Gender: {gender}). Please update 'language_mapping' or 'language_voice_mapping'.") return None except Exception as e: st.error(f"An unexpected error occurred in generate_audio_files for {locale}: {e}") return None def generate_final_video(video_path, audio_path, output_path, bg_audio_volume=0.02, fps = 24): """Combines video and audio using MoviePy""" st.info(f"Processing final video: {os.path.basename(output_path)}...") progress_bar_vid = st.progress(0, text=f"Loading media for {os.path.basename(output_path)}...") try: video_clip = VideoFileClip(video_path) audio_clip = AudioFileClip(audio_path) progress_bar_vid.progress(20, text="Media loaded. Processing audio...") # Extract background audio, reduce volume, handle potential lack of audio audio_background = None if video_clip.audio: audio_background = video_clip.audio.fx(volumex, bg_audio_volume) final_audio = CompositeAudioClip([audio_clip, audio_background]) else: st.warning(f"Original video '{os.path.basename(video_path)}' has no audio track. Using only generated voice-over.") final_audio = audio_clip # Use only the generated audio progress_bar_vid.progress(40, text="Audio mixed. Adjusting video speed...") # Adjust the duration of video to the audio ads duration target_duration = audio_clip.duration if video_clip.duration == 0: st.error("Video clip has zero duration. Cannot process.") return False # Indicate failure if target_duration == 0: st.warning("Generated audio has zero duration. Using original video duration.") target_duration = video_clip.duration # Fallback, though might be silent # Prevent division by zero or extremely small durations if target_duration > 0.01: speed = video_clip.duration / target_duration # Add safeguards for extreme speed values if necessary if speed < 0.1: st.warning(f"Calculated speed ({speed:.2f}x) is very low. Clamping to 0.1x.") speed = 0.1 elif speed > 10.0: st.warning(f"Calculated speed ({speed:.2f}x) is very high. Clamping to 10.0x.") speed = 10.0 video_adjusted = video_clip.fx(speedx, speed) else: st.warning("Audio duration too short, cannot calculate speed. Using original video speed.") video_adjusted = video_clip # Use original speed # Ensure video duration matches audio after speed adjustment (within tolerance) video_final = video_adjusted.set_duration(target_duration) progress_bar_vid.progress(60, text="Speed adjusted. Setting final audio...") # Set final audio to the video video_final = video_final.set_audio(final_audio) progress_bar_vid.progress(80, text="Writing final video file (this may take time)...") # Write the final video file video_final.write_videofile(output_path, fps=fps, codec='libx264', # Common codec audio_codec="aac", audio_bitrate="192k", threads=4, # Use multiple threads logger='bar') # Show moviepy progress bar in console/logs progress_bar_vid.progress(100, text="Video processing complete!") st.success(f"Final video saved: {os.path.basename(output_path)}") # Close clips to release resources video_clip.close() audio_clip.close() if audio_background: audio_background.close() video_adjusted.close() video_final.close() return True # Indicate success except Exception as e: st.error(f"Error processing video {os.path.basename(output_path)}: {e}") # Log error details with open(f'video_error_logs.txt', 'a') as f: f.write(f'{datetime.now()} | Video: {os.path.basename(output_path)}, Error: {e}\n') # Attempt to close clips even on error try: if 'video_clip' in locals() and video_clip: video_clip.close() if 'audio_clip' in locals() and audio_clip: audio_clip.close() if 'audio_background' in locals() and audio_background: audio_background.close() if 'video_adjusted' in locals() and video_adjusted: video_adjusted.close() if 'video_final' in locals() and video_final: video_final.close() except Exception as close_err: st.warning(f"Error closing media clips: {close_err}") return False # Indicate failure finally: # Ensure progress bar is removed if 'progress_bar_vid' in locals(): progress_bar_vid.empty() def download_video_from_url(youtube_url: str, output_path: str) -> bool: """Downloads video from URL using yt-dlp""" st.info(f"Downloading video from {youtube_url}...") # Ensure the output directory exists os.makedirs(os.path.dirname(output_path), exist_ok=True) # Add .mp4 extension if not present in output_path for clarity if not output_path.endswith('.mp4'): output_path += '.mp4' yt_opts = { 'format': 'best[ext=mp4][height<=?1080]/best[ext=mp4]/best', # Prioritize mp4, max 1080p 'outtmpl': output_path, 'force_ipv4': True, 'quiet': True, # Suppress yt-dlp console output 'noplaylist': True, # Download only the single video 'progress_hooks': [(lambda d: st_progress_hook(d, st.progress(0.0)))] # Basic progress hook } try: with yt_dlp.YoutubeDL(yt_opts) as ydl: ydl.download([youtube_url]) st.success(f"Video downloaded successfully to {os.path.basename(output_path)}") return True except yt_dlp.utils.DownloadError as e: st.error(f"yt-dlp download error: {e}") return False except Exception as e: st.error(f"An unexpected error occurred during download: {e}") return False def st_progress_hook(d, progress_bar): """Basic progress hook for yt-dlp and Streamlit progress bar.""" if d['status'] == 'downloading': # You might need to parse d['eta'], d['speed'], d['_percent_str'], etc. # For a simple approach, just update based on percentage if available if '_percent_str' in d: try: percent = float(d['_percent_str'].replace('%','').strip()) / 100.0 # progress_bar.progress(percent, text=f"Downloading... {d['_percent_str']}") # print(f"Download progress: {percent*100:.1f}%") # Debug print except: pass # Ignore if parsing fails elif d['status'] == 'finished': progress_bar.progress(1.0, text="Download finished.") time.sleep(0.5) # Keep message visible briefly progress_bar.empty() # Clear the progress bar elif d['status'] == 'error': st.error("Download hook reported an error.") progress_bar.empty() # --- Main Streamlit App --- st.set_page_config(layout="wide") st.title("🎙️ AdSonic Voice Over Ad Generator 🎬") st.markdown(""" This tool uses Google's Generative AI (Gemini) and Text-to-Speech APIs to create voice-over scripts and audio for your video ads in multiple languages. **Instructions:** 1. **Authenticate:** Upload your Google Cloud Service Account Key JSON file and enter your Project ID and Location. 2. **Configure:** Use the sidebar to set your ad parameters (YouTube URL, languages, voice, etc.). 3. **Generate:** Click the "Generate Voice Over Ads" button. 4. **Download:** Wait for the process to complete. Download links for the generated videos will appear. """) # --- Authentication --- st.header("1. Authentication") st.warning("Ensure the Service Account has 'Vertex AI User' and 'Cloud Text-to-Speech API User' roles.") uploaded_key = st.file_uploader("Upload Service Account Key (JSON)", type="json") project_id = st.text_input("Google Cloud Project ID", help="Your GCP project ID.") location = st.text_input("Google Cloud Location", value="us-central1", help="e.g., us-central1, asia-southeast1") credentials = None vertex_initialized = False if uploaded_key and project_id and location: try: # Read the uploaded file's content credentials_info = json.load(uploaded_key) # Load credentials using the cached function credentials = load_credentials(credentials_info) if credentials: st.success("Service Account Key loaded successfully.") # Initialize Vertex AI (only if credentials are valid) vertex_initialized = initialize_vertexai(project_id, location, credentials) else: st.error("Failed to load credentials from the uploaded file.") except json.JSONDecodeError: st.error("Invalid JSON file. Please upload a valid Service Account Key.") except Exception as e: st.error(f"An error occurred during authentication setup: {e}") else: st.info("Please upload your Service Account Key and provide Project ID & Location to proceed.") # --- Configuration (Sidebar) --- st.sidebar.header("2. Ad Configuration") youtube_url = st.sidebar.text_input("YouTube Video URL", help="Link to the source video.") video_name_override = st.sidebar.text_input("Base Output Filename (Optional)", help="Overrides default name based on YouTube ID.") app_info = st.sidebar.text_area("Product/App Information", help="Describe your app/game. Used by AI for script generation.") custom_transcript_prompt = st.sidebar.text_area("Custom Prompt Instructions (Optional)", help="Add specific instructions for the AI script writer.") available_languages = list(language_mapping.keys()) locales = st.sidebar.multiselect("Target Languages", options=available_languages, default=["English"], help="Select languages for the voice-over.") app_type_options = list(type_of_app_prompt_config.keys()) type_of_app = st.sidebar.selectbox("Type of App/Product", options=app_type_options, index=0) voice_tone_options = list(voice_tone_prompt_config.keys()) voice_tone = st.sidebar.selectbox("Voice Tone", options=voice_tone_options, index=0) voice_gender = st.sidebar.selectbox("Voice Gender", options=["Female", "Male"], index=0) bg_audio_volume = st.sidebar.slider("Background Audio Volume", min_value=0.0, max_value=1.0, value=0.1, step=0.01, help="Volume of the original video's audio track (0 = silent, 1 = original volume).") skip_intro = st.sidebar.checkbox("Skip Intro in Script?", value=True, help="Attempt to make the AI skip introductory phrases.") # --- Generation Trigger --- st.header("3. Generate & Download") if st.button("🚀 Generate Voice Over Ads"): # --- Input Validation --- if not uploaded_key or not credentials or not vertex_initialized: st.error("Authentication incomplete. Please upload key, set Project ID/Location.") elif not youtube_url: st.error("Please provide a YouTube Video URL.") elif not locales: st.error("Please select at least one target language.") else: st.info("Starting generation process...") # --- Setup Output Directory --- now = datetime.now() time_str = now.strftime("%Y%m%d_%H%M%S") # Derive video name from URL or use override video_id = None try: parsed_url = urlparse(youtube_url) if "youtube.com" in parsed_url.netloc: video_id = parse_qs(parsed_url.query).get('v', [None])[0] elif "youtu.be" in parsed_url.netloc: video_id = parsed_url.path.lstrip('/') except Exception: pass # Ignore parsing errors base_video_name = video_name_override if video_name_override else (video_id if video_id else "video") base_video_name = "".join(c if c.isalnum() or c in ('-', '_') else '_' for c in base_video_name) # Sanitize output_base_folder = "output" run_folder_name = f"{time_str}_{base_video_name}" run_folder_path = os.path.join(output_base_folder, run_folder_name) audio_folder_path = os.path.join(run_folder_path, "audio") # Separate folder for intermediate audio video_output_folder_path = os.path.join(run_folder_path, "video") # Final videos here os.makedirs(audio_folder_path, exist_ok=True) os.makedirs(video_output_folder_path, exist_ok=True) st.info(f"Results will be saved in: {run_folder_path}") # --- 1. Download Video --- download_placeholder = st.empty() download_placeholder.info("Downloading video...") local_video_filename = os.path.join(run_folder_path, f"{base_video_name}_original.mp4") download_success = download_video_from_url(youtube_url, local_video_filename) if not download_success or not os.path.exists(local_video_filename): download_placeholder.error("Failed to download video. Please check URL and try again.") st.stop() # Stop execution if download fails download_placeholder.success(f"Original video saved: {os.path.basename(local_video_filename)}") try: video_clip_temp = VideoFileClip(local_video_filename) ad_duration_sec = video_clip_temp.duration video_clip_temp.close() # Close immediately after getting duration st.write(f"Detected video duration: {ad_duration_sec:.2f} seconds") except Exception as e: st.error(f"Could not read video duration from downloaded file: {e}") ad_duration_sec = 60.0 # Fallback duration st.warning(f"Using fallback duration: {ad_duration_sec} seconds.") # --- 2. Generate Transcripts --- transcripts_placeholder = st.empty() transcripts_placeholder.info("Generating transcripts...") transcripts = generate_text( app_info=app_info, locales=locales, video_path=local_video_filename, ad_duration_sec=ad_duration_sec, voice_tone=voice_tone, type_of_app=type_of_app, custom_transcript_prompt=custom_transcript_prompt, skip_intro=skip_intro, credentials=credentials # Pass credentials ) if not transcripts: transcripts_placeholder.error("Failed to generate transcripts. Check logs and API settings.") # Log error details with open(f'text_error_logs.txt', 'a') as f: f.write(f"{datetime.now()} | Failed to generate transcripts for {base_video_name}\n") st.stop() transcripts_placeholder.success("Transcripts generated.") st.subheader("Generated Transcripts:") st.json(transcripts) # Save transcripts to file transcript_filepath = os.path.join(run_folder_path, 'transcripts.json') try: with open(transcript_filepath, 'w', encoding='utf-8') as fp: json.dump(transcripts, fp, ensure_ascii=False, indent=4) st.write(f"Transcripts saved to: {transcript_filepath}") except Exception as e: st.warning(f"Could not save transcripts to file: {e}") # --- 3. Generate Audio & Final Videos --- st.subheader("Generating Audio and Final Videos:") generated_video_files = [] overall_progress = st.progress(0.0) num_locales = len(transcripts) for i, locale in enumerate(transcripts): text = transcripts[locale] locale_progress_text = f"Processing {locale} ({i+1}/{num_locales})..." st.markdown(f"--- \n**{locale_progress_text}**") overall_progress.progress((i / num_locales), text=locale_progress_text) # --- Generate Audio File --- audio_gen_placeholder = st.empty() audio_gen_placeholder.info(f"Generating audio for {locale}...") audio_file_path = generate_audio_files( folder_path=audio_folder_path, # Save intermediate audio here transcript=text, locale=locale, gender=voice_gender, credentials=credentials # Pass credentials ) if not audio_file_path or not os.path.exists(audio_file_path): audio_gen_placeholder.warning(f"Skipping video generation for {locale} due to audio failure.") continue # Skip to the next language audio_gen_placeholder.success(f"Audio generated for {locale}.") # --- Generate Final Video --- video_gen_placeholder = st.empty() video_gen_placeholder.info(f"Generating final video for {locale}...") final_video_name = f"{base_video_name}-{locale}.mp4" final_video_path = os.path.join(video_output_folder_path, final_video_name) video_success = generate_final_video( video_path=local_video_filename, audio_path=audio_file_path, output_path=final_video_path, bg_audio_volume=bg_audio_volume ) if video_success and os.path.exists(final_video_path): video_gen_placeholder.success(f"Final video generated for {locale}.") generated_video_files.append(final_video_path) else: video_gen_placeholder.error(f"Failed to generate final video for {locale}.") overall_progress.progress(1.0, text="Processing complete!") time.sleep(1) overall_progress.empty() # --- 4. Display Download Links --- st.header("4. Download Results") if generated_video_files: st.success("Video generation finished!") for video_file in generated_video_files: try: with open(video_file, "rb") as f: st.download_button( label=f"Download {os.path.basename(video_file)}", data=f, file_name=os.path.basename(video_file), mime="video/mp4" ) except FileNotFoundError: st.warning(f"Could not find generated file for download: {os.path.basename(video_file)}") except Exception as e: st.error(f"Error creating download button for {os.path.basename(video_file)}: {e}") # Offer transcript download again if os.path.exists(transcript_filepath): try: with open(transcript_filepath, "rb") as f: st.download_button( label="Download Transcripts (JSON)", data=f, file_name="transcripts.json", mime="application/json" ) except Exception as e: st.warning(f"Could not create transcript download button: {e}") else: st.warning("No videos were successfully generated.") # Optional: Clean up original downloaded video? # try: # os.remove(local_video_filename) # st.write(f"Removed temporary original video: {os.path.basename(local_video_filename)}") # except Exception as e: # st.warning(f"Could not remove temporary file {local_video_filename}: {e}")