Ahmadkhan12 commited on
Commit
176c646
·
verified ·
1 Parent(s): a8948bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -32
app.py CHANGED
@@ -2,11 +2,14 @@ import gradio as gr
2
  import moviepy.editor as mp
3
  import numpy as np
4
  from PIL import Image
 
 
5
 
6
- # Resize image with aspect ratio
7
  def resize_image_with_aspect_ratio(img, target_size=(1280, 720), padding_color=(0, 0, 0)):
8
  width, height = img.size
9
  target_width, target_height = target_size
 
10
  aspect_ratio = width / height
11
  target_aspect_ratio = target_width / target_height
12
 
@@ -27,34 +30,55 @@ def resize_image_with_aspect_ratio(img, target_size=(1280, 720), padding_color=(
27
 
28
  # Video generation function with transition
29
  def process_and_generate_video(audio_file, images):
30
- audio = mp.AudioFileClip(audio_file)
31
- audio_duration = audio.duration
32
- image_clips = []
33
- image_count = len(images)
34
- image_duration = audio_duration / image_count
35
-
36
- # Process each image and create a video clip
37
- for img_path in images:
38
- img = Image.open(img_path)
39
- img = resize_image_with_aspect_ratio(img, target_size=(1280, 720))
40
-
41
- img_clip = mp.ImageClip(np.array(img)).set_duration(image_duration).set_fps(24)
42
-
43
- # Add transition effect (crossfade)
44
- if len(image_clips) > 0:
45
- img_clip = img_clip.crossfadein(1) # 1-second crossfade transition
46
-
47
- image_clips.append(img_clip)
48
-
49
- # Combine the image clips into a single video
50
- video = mp.concatenate_videoclips(image_clips, method="compose")
51
- video = video.set_audio(audio)
52
-
53
- # Output path for the generated video
54
- output_path = '/content/generated_video.mp4'
55
- video.write_videofile(output_path, codec='libx264', audio_codec='aac')
56
-
57
- return output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
  # Gradio interface setup
60
  def gradio_interface():
@@ -62,14 +86,14 @@ def gradio_interface():
62
  with gr.Row():
63
  with gr.Column():
64
  mp3_input = gr.Audio(type="filepath", label="Upload MP3") # MP3 input
65
- image_input = gr.File(type="filepath", file_types=[".jpg", ".png"], label="Upload Images", file_count="multiple") # Images input
66
  generate_button = gr.Button("Generate Video") # Button to generate video
67
-
68
  output_video = gr.Video(label="Generated Video") # Video output display
69
 
70
  generate_button.click(fn=process_and_generate_video, inputs=[mp3_input, image_input], outputs=output_video)
71
 
72
  demo.launch() # Launch the Gradio interface
73
 
74
- # Run the Gradio app
75
  gradio_interface()
 
2
  import moviepy.editor as mp
3
  import numpy as np
4
  from PIL import Image
5
+ import tempfile
6
+ import shutil
7
 
8
+ # Resize image while maintaining aspect ratio
9
  def resize_image_with_aspect_ratio(img, target_size=(1280, 720), padding_color=(0, 0, 0)):
10
  width, height = img.size
11
  target_width, target_height = target_size
12
+
13
  aspect_ratio = width / height
14
  target_aspect_ratio = target_width / target_height
15
 
 
30
 
31
  # Video generation function with transition
32
  def process_and_generate_video(audio_file, images):
33
+ try:
34
+ # Create temporary folder to store image paths
35
+ with tempfile.TemporaryDirectory() as temp_dir:
36
+ # Save images to temporary directory
37
+ image_paths = []
38
+ for img in images:
39
+ temp_image_path = os.path.join(temp_dir, os.path.basename(img.name))
40
+ with open(temp_image_path, 'wb') as f:
41
+ f.write(img.read())
42
+ image_paths.append(temp_image_path)
43
+
44
+ # Process audio file
45
+ audio = mp.AudioFileClip(audio_file.name) # Use .name for filepath
46
+ audio_duration = audio.duration
47
+ image_clips = []
48
+ image_count = len(image_paths)
49
+ image_duration = audio_duration / image_count
50
+
51
+ print(f"Audio duration: {audio_duration} seconds, Image count: {image_count}")
52
+
53
+ # Process each image and create video clip
54
+ for img_path in image_paths:
55
+ img = Image.open(img_path)
56
+ img = resize_image_with_aspect_ratio(img, target_size=(1280, 720))
57
+
58
+ # Create image clip with a crossfade transition effect
59
+ img_clip = mp.ImageClip(np.array(img)).set_duration(image_duration).set_fps(24)
60
+
61
+ # Add transition effect - Crossfade In
62
+ if len(image_clips) > 0: # Apply transition only after the first image
63
+ img_clip = img_clip.crossfadein(1) # 1-second fade-in transition
64
+
65
+ image_clips.append(img_clip)
66
+
67
+ # Concatenate image clips with transitions
68
+ video = mp.concatenate_videoclips(image_clips, method="compose")
69
+ video = video.set_audio(audio)
70
+
71
+ # Set output file path
72
+ output_path = '/tmp/generated_video.mp4' # Temporary path for output
73
+
74
+ # Write video to file
75
+ video.write_videofile(output_path, codec='libx264', audio_codec='aac')
76
+
77
+ return output_path # Return the file path for Gradio output
78
+
79
+ except Exception as e:
80
+ print(f"Error during video generation: {str(e)}")
81
+ return f"Error generating video: {str(e)}"
82
 
83
  # Gradio interface setup
84
  def gradio_interface():
 
86
  with gr.Row():
87
  with gr.Column():
88
  mp3_input = gr.Audio(type="filepath", label="Upload MP3") # MP3 input
89
+ image_input = gr.File(type="file", file_types=[".jpg", ".png"], label="Upload Images", file_count="multiple") # Images input
90
  generate_button = gr.Button("Generate Video") # Button to generate video
91
+
92
  output_video = gr.Video(label="Generated Video") # Video output display
93
 
94
  generate_button.click(fn=process_and_generate_video, inputs=[mp3_input, image_input], outputs=output_video)
95
 
96
  demo.launch() # Launch the Gradio interface
97
 
98
+ # Run the interface
99
  gradio_interface()