freeai-app commited on
Commit
04ce0b1
·
verified ·
1 Parent(s): fedb41f

Create app_video_audio.py

Browse files
Files changed (1) hide show
  1. app_video_audio.py +46 -0
app_video_audio.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gtts import gTTS
3
+ from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips
4
+ import os
5
+ import tempfile
6
+
7
+ def text_to_video(text, image_path):
8
+ try:
9
+ # temp folder
10
+ tmpdir = tempfile.mkdtemp()
11
+
12
+ # 1. Generate TTS
13
+ tts_path = os.path.join(tmpdir, "speech.mp3")
14
+ tts = gTTS(text)
15
+ tts.save(tts_path)
16
+
17
+ # 2. Prepare Audio
18
+ audio = AudioFileClip(tts_path)
19
+
20
+ # 3. Prepare Image
21
+ image_clip = ImageClip(image_path).set_duration(audio.duration)
22
+
23
+ # 4. Combine
24
+ final = image_clip.set_audio(audio)
25
+
26
+ # 5. Export
27
+ output_path = os.path.join(tmpdir, "final.mp4")
28
+ final.write_videofile(output_path, fps=24)
29
+
30
+ return output_path
31
+ except Exception as e:
32
+ return f"Error: {str(e)}"
33
+
34
+ # Gradio Interface
35
+ demo = gr.Interface(
36
+ fn=text_to_video,
37
+ inputs=[
38
+ gr.Textbox(label="Enter your text"),
39
+ gr.Image(label="Upload an image", type="filepath")
40
+ ],
41
+ outputs=gr.Video(label="Generated Video"),
42
+ title="Text + Image to Video with Audio"
43
+ )
44
+
45
+ if __name__ == "__main__":
46
+ demo.launch()