rockerritesh commited on
Commit
d69b229
·
verified ·
1 Parent(s): a697d3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -4,7 +4,7 @@ import tempfile
4
  import numpy as np
5
  import cv2
6
  from PIL import Image
7
- from moviepy import AudioFileClip, VideoFileClip, ImageClip
8
  import random
9
  import time
10
 
@@ -73,11 +73,27 @@ def add_sparkle_effect(frame, density=0.001):
73
  cv2.circle(frame, (int(x), int(y)), size, color, -1)
74
  return frame
75
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  def create_video(image_path, audio_path, effect_type, output_path, fps=24):
77
  """Create a video from an image and audio with the selected effect"""
78
- # Load audio to get duration
79
- audio_clip = AudioFileClip(audio_path)
80
- duration = audio_clip.duration
 
 
 
 
81
 
82
  # Load image
83
  img = cv2.imread(image_path)
@@ -128,21 +144,24 @@ def create_video(image_path, audio_path, effect_type, output_path, fps=24):
128
  # Release the video writer
129
  out.release()
130
 
131
- # Alternative approach using direct moviepy functionalities
132
- # First create a mute video clip
133
- video_clip = VideoFileClip(temp_video_path)
134
-
135
- # Then set its audio to our audio clip
136
- final_clip = video_clip.set_audio(audio_clip)
 
 
 
 
 
 
137
 
138
- # Write the result to our output file
139
- final_clip.write_videofile(output_path, codec='libx264')
140
 
141
- # Clean up temporary file and close clips
142
  os.remove(temp_video_path)
143
- video_clip.close()
144
- audio_clip.close()
145
- final_clip.close()
146
 
147
  progress_bar.progress(1.0)
148
  return output_path
 
4
  import numpy as np
5
  import cv2
6
  from PIL import Image
7
+ import subprocess
8
  import random
9
  import time
10
 
 
73
  cv2.circle(frame, (int(x), int(y)), size, color, -1)
74
  return frame
75
 
76
+ def get_audio_duration(audio_path):
77
+ """Get the duration of an audio file using ffprobe"""
78
+ cmd = [
79
+ 'ffprobe',
80
+ '-v', 'error',
81
+ '-show_entries', 'format=duration',
82
+ '-of', 'default=noprint_wrappers=1:nokey=1',
83
+ audio_path
84
+ ]
85
+ result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
86
+ return float(result.stdout.strip())
87
+
88
  def create_video(image_path, audio_path, effect_type, output_path, fps=24):
89
  """Create a video from an image and audio with the selected effect"""
90
+ # Get audio duration
91
+ try:
92
+ duration = get_audio_duration(audio_path)
93
+ except Exception as e:
94
+ st.error(f"Error getting audio duration: {e}")
95
+ # Fallback to a default duration if ffprobe fails
96
+ duration = 30.0
97
 
98
  # Load image
99
  img = cv2.imread(image_path)
 
144
  # Release the video writer
145
  out.release()
146
 
147
+ # Combine the silent video with audio using ffmpeg directly
148
+ cmd = [
149
+ 'ffmpeg',
150
+ '-i', temp_video_path, # Input video file
151
+ '-i', audio_path, # Input audio file
152
+ '-map', '0:v', # Use video from first input
153
+ '-map', '1:a', # Use audio from second input
154
+ '-c:v', 'copy', # Copy the video codec
155
+ '-c:a', 'aac', # Use AAC for audio
156
+ '-shortest', # End when the shortest input ends
157
+ output_path # Output file
158
+ ]
159
 
160
+ # Run ffmpeg command
161
+ subprocess.run(cmd, check=True)
162
 
163
+ # Clean up temporary file
164
  os.remove(temp_video_path)
 
 
 
165
 
166
  progress_bar.progress(1.0)
167
  return output_path