Monday, May 26, 2025

Video generate all time fee

import os import time import requests from PIL import Image, ImageDraw, ImageFont import numpy as np import pygame from gtts import gTTS from moviepy.editor import ImageSequenceClip, AudioFileClip, concatenate_videoclips import openai # For text generation from stable_diffusion import generate_image # Hypothetical image generation module # Initialize pygame for audio pygame.mixer.init() class AIVideoGenerator: def __init__(self): self.scenes = [] self.audio_files = [] self.temp_files = [] def generate_story(self, prompt): """Generate story using GPT-3""" openai.api_key = os.getenv("OPENAI_API_KEY") response = openai.Completion.create( engine="text-davinci-003", prompt=f"Write a short emotional story in 150 words about: {prompt}", max_tokens=300 ) return response.choices[0].text.strip() def text_to_speech(self, text, filename): """Convert text to speech using gTTS""" tts = gTTS(text=text, lang='hi' if any(ord(c) > 127 for c in text) else 'en') tts.save(filename) self.temp_files.append(filename) return filename def generate_image_scene(self, prompt, filename): """Generate image using Stable Diffusion""" # This is a placeholder - you would need actual API access img_url = generate_image(prompt) img_data = requests.get(img_url).content with open(filename, 'wb') as handler: handler.write(img_data) self.temp_files.append(filename) return filename def create_scene(self, text, image_prompt, duration=5): """Create a video scene with image and audio""" # Generate assets audio_file = f"temp_audio_{len(self.scenes)}.mp3" image_file = f"temp_image_{len(self.scenes)}.png" self.text_to_speech(text, audio_file) self.generate_image_scene(image_prompt, image_file) # Create scene audio_clip = AudioFileClip(audio_file) actual_duration = min(duration, audio_clip.duration) img = Image.open(image_file) frames = [np.array(img)] * int(actual_duration * 24) # 24fps video_clip = ImageSequenceClip(frames, fps=24) video_clip = video_clip.set_audio(audio_clip) self.scenes.append(video_clip) def generate_video(self, output_file="output.mp4"): """Combine all scenes into final video""" if not self.scenes: raise ValueError("No scenes to compile") final_clip = concatenate_videoclips(self.scenes) final_clip.write_videofile(output_file, fps=24) # Cleanup temporary files for file in self.temp_files: try: os.remove(file) except: pass return output_file def main(): print("AI Video Generation Tool") print("This will create a 3-minute video with AI-generated content") # User input prompt = input("Enter your story prompt (e.g., 'a sad story about a bird'): ") # Initialize generator generator = AIVideoGenerator() # Generate story print("\nGenerating story...") story = generator.generate_story(prompt) print("Story generated successfully!") # Split story into scenes (simple split by sentences) sentences = [s.strip() for s in story.split('.') if s.strip()] # Create scenes print("\nCreating video scenes...") start_time = time.time() for i, sentence in enumerate(sentences): if time.time() - start_time > 150: # Stop at 2.5 minutes (30 seconds buffer) break print(f"Creating scene {i+1}/{len(sentences)}") generator.create_scene( text=sentence, image_prompt=f"An image showing: {sentence}", duration=len(sentence.split())/2 # Duration based on word count ) # Generate final video print("\nCompiling final video...") output_file = generator.generate_video() print(f"\nVideo generated successfully: {output_file}") # Play the video (optional) print("Playing the generated video...") os.system(f"start {output_file}") # Windows # For Linux/Mac: os.system(f"xdg-open {output_file}") if __name__ == "__main__": main()

No comments:

Post a Comment