Ahmadkhan12 commited on
Commit
e5501e0
·
verified ·
1 Parent(s): ff4e5c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -62
app.py CHANGED
@@ -34,67 +34,54 @@ def process_and_generate_video(audio_file, images):
34
 
35
  try:
36
  # Log the files received
37
- debug_log.append(f"Audio file received: {audio_file.name}")
38
- debug_log.append(f"Images received: {[img.name for img in images]}")
39
-
40
- # Create temporary directory for images
41
- with tempfile.TemporaryDirectory() as temp_dir:
42
- # Save images to temporary directory
43
- image_paths = []
44
- for img in images:
45
- temp_image_path = os.path.join(temp_dir, os.path.basename(img.name))
46
- with open(temp_image_path, 'wb') as f:
47
- f.write(img.read())
48
- image_paths.append(temp_image_path)
49
-
50
- # Log image paths for debugging
51
- debug_log.append(f"Saved images in temporary directory: {image_paths}")
52
-
53
- # Process audio file
54
- audio = mp.AudioFileClip(audio_file.name) # Use .name for filepath
55
- audio_duration = audio.duration
56
- image_clips = []
57
- image_count = len(image_paths)
58
- image_duration = audio_duration / image_count
59
-
60
- debug_log.append(f"Audio duration: {audio_duration} seconds, Image count: {image_count}")
61
-
62
- # Process each image and create video clip
63
- for img_path in image_paths:
64
- debug_log.append(f"Processing image: {img_path}") # Debug print
65
- img = Image.open(img_path)
66
- img = resize_image_with_aspect_ratio(img, target_size=(1280, 720))
67
-
68
- # Create image clip with a crossfade transition effect
69
- img_clip = mp.ImageClip(np.array(img)).set_duration(image_duration).set_fps(24)
70
-
71
- # Add transition effect - Crossfade In
72
- if len(image_clips) > 0: # Apply transition only after the first image
73
- img_clip = img_clip.crossfadein(1) # 1-second fade-in transition
74
-
75
- image_clips.append(img_clip)
76
-
77
- debug_log.append(f"Created {len(image_clips)} image clips.")
78
-
79
- # Concatenate image clips with transitions
80
- video = mp.concatenate_videoclips(image_clips, method="compose")
81
- video = video.set_audio(audio)
82
-
83
- # Set output file path in a temporary location
84
- output_path = '/tmp/generated_video.mp4' # Temporary path for output
85
-
86
- debug_log.append(f"Writing video to {output_path}...")
87
-
88
- # Write video to file
89
- video.write_videofile(output_path, codec='libx264', audio_codec='aac')
90
-
91
- # Check if the video file exists
92
- if os.path.exists(output_path):
93
- debug_log.append(f"Video generated successfully at {output_path}")
94
- return output_path, "\n".join(debug_log) # Return the video path and debug log
95
- else:
96
- debug_log.append(f"Error: Video not generated at {output_path}")
97
- return "Error: Video not generated.", "\n".join(debug_log)
98
 
99
  except Exception as e:
100
  debug_log.append(f"Error during video generation: {str(e)}")
@@ -108,7 +95,7 @@ def gradio_interface():
108
  mp3_input = gr.Audio(type="filepath", label="Upload MP3") # MP3 input
109
  image_input = gr.File(type="filepath", file_types=[".jpg", ".png"], label="Upload Images", file_count="multiple") # Images input
110
  generate_button = gr.Button("Generate Video") # Button to generate video
111
-
112
  output_video = gr.Video(label="Generated Video") # Video output display
113
  output_logs = gr.Textbox(label="Debug Logs", interactive=False) # Display debug logs
114
 
 
34
 
35
  try:
36
  # Log the files received
37
+ debug_log.append(f"Audio file received: {audio_file}")
38
+ debug_log.append(f"Images received: {[img for img in images]}")
39
+
40
+ # Process audio file (use audio_file directly as it's a path)
41
+ audio = mp.AudioFileClip(audio_file) # Use file path directly
42
+ audio_duration = audio.duration
43
+ image_clips = []
44
+ image_count = len(images)
45
+ image_duration = audio_duration / image_count
46
+
47
+ debug_log.append(f"Audio duration: {audio_duration} seconds, Image count: {image_count}")
48
+
49
+ # Process each image and create video clip
50
+ for img in images:
51
+ debug_log.append(f"Processing image: {img}") # Debug print
52
+ img = Image.open(img) # Open image from file path
53
+ img = resize_image_with_aspect_ratio(img, target_size=(1280, 720))
54
+
55
+ # Create image clip with a crossfade transition effect
56
+ img_clip = mp.ImageClip(np.array(img)).set_duration(image_duration).set_fps(24)
57
+
58
+ # Add transition effect - Crossfade In
59
+ if len(image_clips) > 0: # Apply transition only after the first image
60
+ img_clip = img_clip.crossfadein(1) # 1-second fade-in transition
61
+
62
+ image_clips.append(img_clip)
63
+
64
+ debug_log.append(f"Created {len(image_clips)} image clips.")
65
+
66
+ # Concatenate image clips with transitions
67
+ video = mp.concatenate_videoclips(image_clips, method="compose")
68
+ video = video.set_audio(audio)
69
+
70
+ # Set output file path in a temporary location
71
+ output_path = '/tmp/generated_video.mp4' # Temporary path for output
72
+
73
+ debug_log.append(f"Writing video to {output_path}...")
74
+
75
+ # Write video to file
76
+ video.write_videofile(output_path, codec='libx264', audio_codec='aac')
77
+
78
+ # Check if the video file exists
79
+ if os.path.exists(output_path):
80
+ debug_log.append(f"Video generated successfully at {output_path}")
81
+ return output_path, "\n".join(debug_log) # Return the video path and debug log
82
+ else:
83
+ debug_log.append(f"Error: Video not generated at {output_path}")
84
+ return "Error: Video not generated.", "\n".join(debug_log)
 
 
 
 
 
 
 
 
 
 
 
 
 
85
 
86
  except Exception as e:
87
  debug_log.append(f"Error during video generation: {str(e)}")
 
95
  mp3_input = gr.Audio(type="filepath", label="Upload MP3") # MP3 input
96
  image_input = gr.File(type="filepath", file_types=[".jpg", ".png"], label="Upload Images", file_count="multiple") # Images input
97
  generate_button = gr.Button("Generate Video") # Button to generate video
98
+
99
  output_video = gr.Video(label="Generated Video") # Video output display
100
  output_logs = gr.Textbox(label="Debug Logs", interactive=False) # Display debug logs
101