File size: 6,846 Bytes
cd5bf5b
 
 
 
 
 
bb7daca
cd5bf5b
 
 
 
 
 
 
 
 
 
 
 
bb7daca
cd5bf5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8806620
cd5bf5b
 
 
fcff35c
 
8806620
cd5bf5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d09fb7
cd5bf5b
 
 
 
 
 
 
8806620
cd5bf5b
 
 
 
 
 
 
8806620
 
cd5bf5b
 
 
 
 
 
449e529
cd5bf5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e4adf6
640651e
6d09fb7
 
 
8806620
 
6d09fb7
cd5bf5b
6d09fb7
cd5bf5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aff3038
449e529
cd5bf5b
 
 
 
 
 
449e529
cd5bf5b
 
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

import re
import requests
from gtts import gTTS
import moviepy.editor as mpe
from moviepy.editor import concatenate_videoclips
import ffmpeg

# API key and basic setup for Pixabay
API_KEY = '43444686-8ab8011dcadf5ec78f23cbff8'


PIXABAY_URL = f"https://pixabay.com/api/?key={API_KEY}"
import pixabay.core
import google.generativeai as genai

def gemini_ai_config():
  #@title Configure Gemini API key


#Access your Gemini API key
  try:
    GOOGLE_API_KEY="AIzaSyD3XZWTlW3S5a2qDoWAFSH3lsOpzVYVpSs"
    genai.configure(api_key=GOOGLE_API_KEY)
    model = genai.GenerativeModel('gemini-pro')
    return model
  except Exception as e:
    # unknown error
    print(f"There was an unknown error. Ensure you have a secret  stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey")
    raise e


def generate_voice_from_text(text, query,lang='en'):
    """Generates an MP3 file from the given text using gTTS."""
    tts = gTTS(text=text, lang=lang)
    audio_path = f'temp_{query}_audio.mp3'
    tts.save(audio_path)
    save_and_return_file(audio_path)
    return audio_path


def save_and_return_file(filename):
    print("save",filename)


def read_script(content):
    """Reads a script file and returns a list of text and queries."""
    '''with open(filename, "r") as file:
        content = file.read().strip()
    # This pattern assumes there could be multiple lines between ####TEXT: and ####QUERY:'''
    pattern = r"###TEXT:\s*(.*?)\s*###QUERY:\s*(.*?)(?=###TEXT:|$)"

    segments = re.findall(pattern, content, re.DOTALL)
    if not segments:
        print("No valid segments found in the script. Please check the script format.")
    return segments

def fetch_video_from_pixabay(query, i):
    try:

        px = pixabay.core(API_KEY)
        space = px.queryVideo(query)
        print("{} hits".format(len(space)))
        filename = f"{query}_{i}.mp4"
        space[0].download(filename, "large")
        return filename
    except Exception as e:
        print(f"Error fetching video for query '{query}': {e}")
        return None

def process_segment(text, query, index):
    video_url = fetch_video_from_pixabay(query, index)
    print("index",index,"query",query)

    if video_url:
        video_clip = mpe.VideoFileClip(video_url)
        audio_path = generate_voice_from_text(text, query)

        audio_clip = mpe.AudioFileClip(audio_path)
         # Trim the video to the desired duration 
        if video_clip.duration > audio_clip.duration:
            video_clip = video_clip.subclip(0, audio_clip.duration)
        else: 
            if video_clip.duration <  audio_clip.duration:   
                loop_count = int( audio_clip.duration// video_clip.duration) 
                video_clip = mpe.concatenate_videoclips([video_clip] * loop_count)
                
                
        # Combine the audio and video
        final_clip = video_clip.set_audio(audio_clip)

        final_clip = video_clip.set_audio(audio_clip)
        video_file_path = f"{query}_{index}.mp4"  # Removed the redundant assignment here

        final_clip.write_videofile(video_file_path, codec="libx264", fps=24)
        save_and_return_file(video_file_path)

        return video_file_path
    else:
        print(f"Skipping segment due to no available video for query: {query}")
        return None

# Main process
def process_video_paragraph(text):
    
    model=gemini_ai_config()
    response = model.generate_content(text)
    print(f'**You**: what is add\n\n**Gemini**:\n{response.text}')
    video_segments = read_script(response.text)
    video_clips=[]
    print(f"Number of segments: {len(video_segments)}")
    for index, segment in enumerate(video_segments):
        text, query = segment[0],segment[1]
        print(f"Processing segment {index + 1}/{len(video_segments)} - Text: {text} Query: {query}")
        video_clip_path=process_segment(text, query,index)
        if video_clip_path:
            # Load the video clip and add to the list
            video_clip = mpe.VideoFileClip(video_clip_path)
            video_clips.append(video_clip)
    else:
        print("No video segments to concatenate.")
    if video_clips:
        final_video = concatenate_videoclips(video_clips,method="compose")
        final_output_path = "final_concatenated_video.mp4"
        final_video.write_videofile(final_output_path, codec="libx264", fps=24)
        print(f"Final concatenated video created: {final_output_path}")
        save_and_return_file(final_output_path)

        return final_output_path

    
import gradio as gr


text="""
    You are attention deficit coach , for a college student, write about best way to productive if you have attention deficit disorder
    We will provide you with an idea of the video along with some reference content. Using the idea create a 1-2 min video for it.
    Create a text for 1-2 min minute video (around 200-400 words),
    video title (make it clickbait super interesting, include numbers into title and maybe open question), video description (optimize for more tags for youtube and google search).
    For actual video content I will be using pixelbay content to compose a series of videos
    into my video. So for the text you provide you have to provide a search query for every
    part so i can search it in pixelbay and pick videos from there.
    Make video, engaging, include different stories, history, make it fun at some points.
    Make it as interesting as possible.
    Avoid any
    kind of lists 1), 2) .. in the text part in the video, also avoid everything
    which is usually not said by humans in the video including various symbols and etc. use just
    words and numbers because the video text will be used for voice over.
    Please provide the output to meet the following format:


        ###TEXT: <text of what you will say in the video, this should complement to the query video which will be shown in the video>
        ###QUERY: <text of what video i should search on storyblocks to match the text above, the query should be short, 1-2 words max, should be as general as possible but on the given topic>

    Always include pair of ###QUERY and ###TEXT for next parts, a video should have a new query around every 10-30 seconds. Initial 1st minute of video should be more
    dynamic and contain more queries 10s each.
    End video asking to like and subscribe to channel, like and share."""

# Define the Gradio interface
interface = gr.Interface(
    fn=process_video_paragraph,
    inputs=gr.Textbox(label="Input Paragraph", placeholder=text),
    outputs=gr.Video(label="video"),
    title="Paragraph to Video Generator",
    description="Enter a paragraph of text and receive a filepath to the generated video."
)

# Run the Gradio app
if __name__ == "__main__":
    interface.launch(debug=True,share=True)