product2204 commited on
Commit
e9d0b3c
·
verified ·
1 Parent(s): 0ebe162

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -43
app.py CHANGED
@@ -1,67 +1,66 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
- from pydub import AudioSegment
5
- from moviepy.editor import VideoFileClip, AudioFileClip
6
  import tempfile
7
- import os
 
8
 
9
- def image_to_video(image, audio):
10
  """
11
- Converts an image and an audio file into a video.
 
12
 
13
  Parameters:
14
- - image: Uploaded image file.
15
- - audio: Uploaded audio file.
 
 
 
 
16
  """
17
- # Create temporary paths for the files
18
- image_path = tempfile.mktemp(suffix=".png")
19
- audio_path = tempfile.mktemp(suffix=".mp3")
20
- video_path = tempfile.mktemp(suffix=".mp4")
21
 
22
- # Save uploaded files to temporary paths
23
- image.save(image_path)
24
- audio.save(audio_path)
25
 
26
- # Load the image
27
- img = cv2.imread(image_path)
28
-
29
- # Get image dimensions
30
- height, width, layers = img.shape
31
-
32
- # Define the codec and create VideoWriter object
33
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
34
- video = cv2.VideoWriter(video_path, fourcc, 30, (width, height))
35
-
36
- audio_segment = AudioSegment.from_file(audio_path)
37
- duration_sec = len(audio_segment) / 1000.0 # Convert duration from ms to seconds
38
-
39
- # Calculate the number of frames needed
40
- num_frames = int(duration_sec * 30) # Assuming 30 fps
41
-
42
  # Write the image to the video file for the required number of frames
 
43
  for _ in range(num_frames):
44
- video.write(img)
45
-
46
- # Release the video writer
47
  video.release()
48
-
49
- # Add audio to the video
 
 
 
 
50
  video_clip = VideoFileClip(video_path)
51
  audio_clip = AudioFileClip(audio_path)
52
  final_clip = video_clip.set_audio(audio_clip)
53
- final_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
54
-
55
  return video_path
56
 
57
- # Define the Gradio interface
58
  iface = gr.Interface(
59
  fn=image_to_video,
60
- inputs=[gr.Image(label="Upload Image"), gr.Audio(label="Upload Audio")],
61
- outputs=gr.Video(label="Output Video"),
62
- title="Image to Video Converter",
63
- description="Converts an image and an audio file into a video."
 
 
 
64
  )
65
 
66
- # Launch the app
67
- iface.launch()
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
 
4
  import tempfile
5
+ import soundfile as sf
6
+ from moviepy.editor import VideoFileClip, concatenate_videoclips,AudioFileClip
7
 
8
+ def image_to_video(image, audio, fps=30):
9
  """
10
+ Converts an image (NumPy array) and audio (tuple of sample rate and audio data)
11
+ into a video file.
12
 
13
  Parameters:
14
+ - image: Input image as a NumPy array.
15
+ - audio: Input audio as a tuple of (sample rate, audio data as a NumPy array).
16
+ - fps: Frames per second of the output video.
17
+
18
+ Returns:
19
+ - Path to the generated video file.
20
  """
21
+ # Create a temporary video file
22
+ video_path = tempfile.mktemp('.mp4')
 
 
23
 
24
+ # Save the image to a temporary file
25
+ image_path = tempfile.mktemp('.png')
26
+ cv2.imwrite(image_path, image)
27
 
28
+ # Create VideoWriter to write frames
29
+ height, width, _ = image.shape
 
 
 
 
 
30
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
31
+ video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))
32
+
33
+ # Determine the duration from the audio sample count and sample rate
34
+ sample_rate, audio_data = audio
35
+ duration_sec = len(audio_data) / sample_rate
36
+
 
 
37
  # Write the image to the video file for the required number of frames
38
+ num_frames = int(duration_sec * fps)
39
  for _ in range(num_frames):
40
+ video.write(cv2.imread(image_path))
 
 
41
  video.release()
42
+
43
+ # Save the audio to a temporary file
44
+ audio_path = tempfile.mktemp('.wav')
45
+ sf.write(audio_path, audio_data.T, sample_rate) # Transpose if necessary for multi-channel audio
46
+
47
+ # Combine the video and audio
48
  video_clip = VideoFileClip(video_path)
49
  audio_clip = AudioFileClip(audio_path)
50
  final_clip = video_clip.set_audio(audio_clip)
51
+ final_clip.write_videofile(video_path, codec="libx264", audio_codec="aac")
 
52
  return video_path
53
 
54
+ # Gradio interface
55
  iface = gr.Interface(
56
  fn=image_to_video,
57
+ inputs=[
58
+ gr.Image(type="numpy"),
59
+ gr.Audio(type="numpy", label="Audio (Sample Rate, Audio Data)")
60
+ ],
61
+ outputs=gr.Video("video"),
62
+ title="Image and Audio to Video Converter",
63
+ description="Upload an image and audio to convert them into a video."
64
  )
65
 
66
+ iface.launch(debug=True,share=True)