File size: 3,492 Bytes
c167a15
 
 
 
56f8b20
34c657e
c167a15
88d41b4
594f203
 
c167a15
 
 
 
 
 
88d41b4
c167a15
 
88d41b4
c167a15
 
 
 
 
 
 
 
34c657e
 
88d41b4
 
 
 
 
 
 
 
c167a15
88d41b4
 
56f8b20
88d41b4
56f8b20
 
88d41b4
56f8b20
88d41b4
 
 
 
 
 
 
c167a15
88d41b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c167a15
 
88d41b4
 
 
 
 
 
 
c167a15
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import gradio as gr
import requests
import json
from gtts import gTTS
import ffmpeg
import runway

# ๐Ÿ”น API Keys
GEMINI_API_KEY = "AIzaSyAfjSQ7WnSkMNOJ9RRIU_-jYy4ik4yhWUA"
RUNWAY_API_KEY = "e17d25a4363b223feb9e5a177b77384b7445fddfc5b467179f8dbd00d76e573e99e291cc665aff0243dae1b92746b23cc05b5e85669b1dcbe358272e4676a10d"

# ๐Ÿ”น Function to Generate a Video Script
def generate_script(topic):
    url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={GEMINI_API_KEY}"
    headers = {"Content-Type": "application/json"}
    data = {"contents": [{"parts": [{"text": f"Create a YouTube/TikTok video script about: {topic}"}]}]}
    
    response = requests.post(url, headers=headers, data=json.dumps(data))
    if response.status_code == 200:
        return response.json()["candidates"][0]["content"]["parts"][0]["text"]
    return "Error generating script."

# ๐Ÿ”น Function to Convert Script to Speech
def convert_text_to_speech(script_text):
    tts = gTTS(text=script_text, lang="en")
    tts.save("audio.mp3")
    return "audio.mp3"

# ๐Ÿ”น Function to Generate Video Using Runway ML
def generate_video_with_runway(script_text):
    try:
        runway.init(api_key=RUNWAY_API_KEY)
        model = runway.connect('text-to-video')
        video_url = model.query('generate_video', inputs={"text": script_text})
        return video_url
    except Exception as e:
        print(f"โŒ Error generating video: {e}")
        return None

# ๐Ÿ”น Add Text Overlay to Video
def add_text_overlay(video_file, text, output_file="video_with_text.mp4"):
    try:
        ffmpeg.input(video_file).output(output_file, vf=f"drawtext=text='{text}':fontcolor=white:fontsize=24:x=10:y=10").run()
        return output_file
    except Exception as e:
        return f"Error adding text overlay: {e}"

# ๐Ÿ”น Add Background Music to Video
def add_background_music(video_file, music_file, output_file="video_with_music.mp4"):
    try:
        ffmpeg.input(video_file).input(music_file).output(output_file, vcodec="libx264", acodec="aac", strict="experimental").run()
        return output_file
    except Exception as e:
        return f"Error adding background music: {e}"

# ๐Ÿ”น Function to Create Video with Progress Bar
def create_video_with_progress(topic, custom_audio=None, music_file=None):
    progress = gr.Progress()
    try:
        script = generate_script(topic)
        if script:
            progress(10, "Script generated")
            audio_file = convert_text_to_speech(script) if not custom_audio else custom_audio
            progress(30, "Audio converted")
            video_file = generate_video_with_runway(script)
            progress(50, "Video generated")
            video_with_overlay = add_text_overlay(video_file, "Your Video Title Here")
            progress(70, "Text overlay added")
            video_with_music = add_background_music(video_with_overlay, music_file if music_file else "default_music.mp3")
            progress(90, "Background music added")
            progress(100, "Video created")
            return video_with_music
    except Exception as e:
        print(f"โŒ Error during video creation: {e}")
    return "Error creating video."

# Gradio UI setup
app = gr.Interface(
    fn=create_video_with_progress, 
    inputs=["text", gr.inputs.File(label="Upload Audio (Optional)"), gr.inputs.File(label="Upload Music (Optional)")], 
    outputs="file", 
    title="AI Video Creator with Runway ML"
)

app.launch()