1littlecoder commited on
Commit
e800e83
1 Parent(s): 397ea54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -35
app.py CHANGED
@@ -6,41 +6,52 @@ import tempfile
6
  import os
7
 
8
  def create_waveform_video(image, audio_path):
9
- # Save uploaded image temporarily
10
- image_path = tempfile.mktemp(suffix=".png")
11
- video_path = tempfile.mktemp(suffix=".mp4")
12
- waveform_path = tempfile.mktemp(suffix=".png")
13
-
14
- image.save(image_path)
15
-
16
- # Load audio
17
- audio_clip = AudioFileClip(audio_path)
18
-
19
- # Generate waveform as an image
20
- waveform = audio_clip.to_soundarray(fps=22050).mean(axis=1) # Convert stereo to mono
21
- waveform = np.interp(waveform, (waveform.min(), waveform.max()), (-1, 1)) # Normalize
22
-
23
- # Plot waveform using matplotlib
24
- plt.figure(figsize=(10, 2))
25
- plt.plot(waveform, color="blue")
26
- plt.axis("off")
27
- plt.savefig(waveform_path, bbox_inches="tight", pad_inches=0)
28
- plt.close()
29
-
30
- # Load image and create a video clip of the same duration as the audio
31
- img_clip = ImageClip(image_path).set_duration(audio_clip.duration).resize(height=720)
32
- waveform_clip = ImageClip(waveform_path).set_duration(audio_clip.duration).resize(width=img_clip.w)
33
- waveform_clip = waveform_clip.set_position(("center", img_clip.h - waveform_clip.h - 20))
34
-
35
- # Combine image, waveform overlay, and audio into one video
36
- final_clip = CompositeVideoClip([img_clip, waveform_clip.set_opacity(0.7)]).set_audio(audio_clip)
37
- final_clip.write_videofile(video_path, codec="libx264", fps=24)
38
-
39
- # Cleanup temporary files
40
- os.remove(image_path)
41
- os.remove(waveform_path)
42
-
43
- return video_path
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  iface = gr.Interface(
46
  fn=create_waveform_video,
 
6
  import os
7
 
8
  def create_waveform_video(image, audio_path):
9
+ # Check if audio file was uploaded
10
+ if audio_path is None:
11
+ return "Error: No audio file provided."
12
+
13
+ try:
14
+ # Save uploaded image temporarily
15
+ image_path = tempfile.mktemp(suffix=".png")
16
+ video_path = tempfile.mktemp(suffix=".mp4")
17
+ waveform_path = tempfile.mktemp(suffix=".png")
18
+
19
+ image.save(image_path)
20
+
21
+ # Load audio
22
+ audio_clip = AudioFileClip(audio_path)
23
+
24
+ # Generate waveform as an image
25
+ waveform = audio_clip.to_soundarray(fps=22050) # Convert audio to waveform array
26
+ if waveform.ndim > 1:
27
+ waveform = waveform.mean(axis=1) # Convert stereo to mono
28
+
29
+ waveform = np.interp(waveform, (waveform.min(), waveform.max()), (-1, 1)) # Normalize
30
+
31
+ # Plot waveform using matplotlib
32
+ plt.figure(figsize=(10, 2))
33
+ plt.plot(waveform, color="blue")
34
+ plt.axis("off")
35
+ plt.savefig(waveform_path, bbox_inches="tight", pad_inches=0)
36
+ plt.close()
37
+
38
+ # Load image and create a video clip of the same duration as the audio
39
+ img_clip = ImageClip(image_path).set_duration(audio_clip.duration).resize(height=720)
40
+ waveform_clip = ImageClip(waveform_path).set_duration(audio_clip.duration).resize(width=img_clip.w)
41
+ waveform_clip = waveform_clip.set_position(("center", img_clip.h - waveform_clip.h - 20))
42
+
43
+ # Combine image, waveform overlay, and audio into one video
44
+ final_clip = CompositeVideoClip([img_clip, waveform_clip.set_opacity(0.7)]).set_audio(audio_clip)
45
+ final_clip.write_videofile(video_path, codec="libx264", fps=24)
46
+
47
+ # Cleanup temporary files
48
+ os.remove(image_path)
49
+ os.remove(waveform_path)
50
+
51
+ return video_path
52
+
53
+ except Exception as e:
54
+ return f"An error occurred: {str(e)}"
55
 
56
  iface = gr.Interface(
57
  fn=create_waveform_video,