1littlecoder commited on
Commit
7e78764
·
verified ·
1 Parent(s): 1f894a3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from moviepy.editor import AudioFileClip, ImageClip, CompositeVideoClip
4
+ import tempfile
5
+ import os
6
+
7
+ def create_waveform_video(image, audio):
8
+ # Save uploaded files temporarily
9
+ image_path = tempfile.mktemp(suffix=".png")
10
+ audio_path = tempfile.mktemp(suffix=".mp3")
11
+ video_path = tempfile.mktemp(suffix=".mp4")
12
+
13
+ image.save(image_path)
14
+ audio.save(audio_path)
15
+
16
+ # Load audio
17
+ audio_clip = AudioFileClip(audio_path)
18
+
19
+ # Load image and create a video clip of the same duration as the audio
20
+ img_clip = ImageClip(image_path).set_duration(audio_clip.duration)
21
+ img_clip = img_clip.resize(height=720) # Resize image to a suitable video height
22
+
23
+ # Generate waveform effect as a video
24
+ waveform = audio_clip.to_soundarray(fps=22050) # Convert audio to waveform array
25
+ waveform = waveform.mean(axis=1) # Convert stereo to mono by averaging channels
26
+
27
+ # Normalize waveform for overlay
28
+ waveform = np.interp(waveform, (waveform.min(), waveform.max()), (-1, 1))
29
+
30
+ # Overlay waveform on image (optional customization: size and position of waveform)
31
+ waveform_clip = ImageClip(image_path).set_duration(audio_clip.duration)
32
+ waveform_clip = waveform_clip.fx(CompositeVideoClip, [[waveform, 'center', 0.8]])
33
+
34
+ # Combine image and audio into one video
35
+ final_clip = CompositeVideoClip([img_clip, waveform_clip.set_audio(audio_clip)])
36
+ final_clip.write_videofile(video_path, codec="libx264", fps=24)
37
+
38
+ # Cleanup temporary files
39
+ os.remove(image_path)
40
+ os.remove(audio_path)
41
+
42
+ return video_path
43
+
44
+ iface = gr.Interface(
45
+ fn=create_waveform_video,
46
+ inputs=[
47
+ gr.Image(type="pil", label="Upload Image"),
48
+ gr.Audio(type="file", label="Upload Audio")
49
+ ],
50
+ outputs=gr.Video(label="Generated Video"),
51
+ title="Image + Audio to Video with Waveform Overlay"
52
+ )
53
+
54
+ iface.launch()