Spaces:
Running
Running
1. handled sorted scenes issue with natsorted
Browse filesFiles changed (2)
- requirements.txt+1
- utils.py+33-50
82 unmodified lines83848582 unmodified linesurllib3==2.3.0watchdog==6.0.0websockets==14.182 unmodified lines8384858682 unmodified linesurllib3==2.3.0watchdog==6.0.0websockets==14.1natsort==8.4.0
5 unmodified lines67891011129 unmodified lines1411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212225 unmodified linesfrom structured_output_extractor import StructuredOutputExtractorfrom response_schemas import ScenesResponseSchemafrom typing import List, Dictdef get_scenes(text_script: str):129 unmodified linesprint(f"Error during image generation: {e}")return {"error": str(e)}def generate_images(image_prompts, folder_name='test_folder'):folder_path = tmp_folder(folder_name)for index, prompt in enumerate(image_prompts):print(index, prompt)image_path = generate_image(prompt=prompt, path=f"{folder_path}/{index}.png")yield prompt, image_pathdef tmp_folder(folder_name: str) -> str:# Use the current working directory or any other accessible path for temp foldersbase_tmp_path = os.path.join(os.getcwd(), "tmp_dir") # Change this to any path you prefer# Ensure that the base temp folder existsif not os.path.exists(base_tmp_path):os.makedirs(base_tmp_path)print(f"Base temporary folder '{base_tmp_path}' created.")# Define the path for the specific temporary folderfolder_path = os.path.join(base_tmp_path, folder_name)# Create the specific temporary folder if it doesn't existos.makedirs(folder_path, exist_ok=True)print(f"Temporary folder '{folder_name}' is ready at {folder_path}.")return folder_pathdef generate_video(video_folder: str, output_filename: str = "final_video.mp4"):audio_folder = os.path.join(video_folder, "audio")images_folder = os.path.join(video_folder, "images")final_clips = []# Get all scene foldersscene_folders = sorted(os.listdir(images_folder))for scene in scene_folders:scene_path = os.path.join(images_folder, scene)audio_path = os.path.join(audio_folder, f"{scene}.mp3")if not os.path.exists(audio_path):print(f"Warning: Audio file {audio_path} not found. Skipping scene {scene}.")continue# Get all images for the sceneimage_files = sorted([os.path.join(scene_path, img) for img in os.listdir(scene_path) if img.endswith(('.png', '.jpg', '.jpeg'))])if not image_files:print(f"Warning: No images found in {scene_path}. Skipping scene {scene}.")continue# Load audio fileaudio_clip = mp.AudioFileClip(audio_path)# Calculate duration per imageduration_per_image = audio_clip.duration / len(image_files)# Create image clipsimage_clips = [mp.ImageClip(img).set_duration(duration_per_image) for img in image_files]# Concatenate image clipsscene_video = mp.concatenate_videoclips(image_clips, method="compose").set_audio(audio_clip)final_clips.append(scene_video)if not final_clips:print("Error: No valid scenes processed.")return None# Concatenate all scenesfinal_video = mp.concatenate_videoclips(final_clips, method="compose")output_path = os.path.join(video_folder, output_filename)final_video.write_videofile(output_path, fps=24, codec='libx264')return output_pathdef calculate_read_time(text: str, words_per_minute: int = 155) -> str:5 unmodified lines678910111213129 unmodified lines1431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042055 unmodified linesfrom structured_output_extractor import StructuredOutputExtractorfrom response_schemas import ScenesResponseSchemafrom typing import List, Dictfrom natsort import natsorted # Natural sorting for correct numerical orderdef get_scenes(text_script: str):129 unmodified linesprint(f"Error during image generation: {e}")return {"error": str(e)}def generate_video(video_folder: str, output_filename: str = "final_video.mp4"):audio_folder = os.path.join(video_folder, "audio")images_folder = os.path.join(video_folder, "images")final_clips = []# Get all scene folders with absolute paths and sorted orderscene_folders = [os.path.join(images_folder, scene)for scene in natsorted(os.listdir(images_folder))if os.path.isdir(os.path.join(images_folder, scene))]for scene_path in scene_folders:scene_name = os.path.basename(scene_path)audio_path = os.path.join(audio_folder, f"{scene_name}.mp3")# Ensure audio file existsif not os.path.exists(audio_path):print(f"Warning: Audio file {audio_path} not found. Skipping scene {scene_name}.")continue# Get all image files for the scene (sorted)image_files = natsorted([os.path.join(scene_path, img)for img in os.listdir(scene_path)if img.lower().endswith(('.png', '.jpg', '.jpeg'))])if not image_files:print(f"Warning: No images found in {scene_path}. Skipping scene {scene_name}.")continue# Load audio fileaudio_clip = mp.AudioFileClip(audio_path)# Calculate duration per imageduration_per_image = audio_clip.duration / len(image_files)# Create image clipsimage_clips = [mp.ImageClip(img).set_duration(duration_per_image) for img in image_files]# Concatenate image clipsscene_video = mp.concatenate_videoclips(image_clips, method="compose").set_audio(audio_clip)final_clips.append(scene_video)if not final_clips:print("Error: No valid scenes processed.")return None# Concatenate all scenesfinal_video = mp.concatenate_videoclips(final_clips, method="compose")output_path = os.path.join(video_folder, output_filename)final_video.write_videofile(output_path, fps=24, codec='libx264')return output_pathdef calculate_read_time(text: str, words_per_minute: int = 155) -> str:
requirements.txt+1
utils.py+33-50