tsi-org commited on
Commit
c6ad66e
1 Parent(s): f27d082

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -12
app.py CHANGED
@@ -105,7 +105,7 @@ def text_to_audio(text, api_key, voice):
105
  audio_filename = tmpfile.name
106
 
107
  return audio_filename
108
-
109
  def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
110
  try:
111
  video_clip = VideoFileClip(video_filename)
@@ -114,27 +114,26 @@ def merge_audio_video(video_filename, audio_filename, output_filename, overlay_a
114
  print(f"Error loading video or main audio clip: {e}")
115
  return None
116
 
117
- # Start with the main audio clip
 
 
 
 
 
 
118
  audio_clips = [main_audio_clip]
119
 
 
120
  if overlay_audio_path and os.path.exists(overlay_audio_path):
121
  try:
122
- # Load the overlay audio clip
123
- overlay_audio_clip = AudioFileClip(overlay_audio_path)
124
- # Adjust the overlay audio clip's volume to 10%
125
- overlay_audio_clip = overlay_audio_clip.volumex(0.1)
126
- # Ensure the overlay audio clip matches the main audio clip's duration
127
  if overlay_audio_clip.duration > main_audio_clip.duration:
128
  overlay_audio_clip = overlay_audio_clip.subclip(0, main_audio_clip.duration)
129
  audio_clips.append(overlay_audio_clip)
130
  except Exception as e:
131
  print(f"Error processing overlay audio clip: {e}")
132
- # Optionally handle the error or continue without the overlay
133
 
134
- # Combine the audio clips into a composite
135
  composite_audio_clip = CompositeAudioClip(audio_clips)
136
-
137
- # Set the video's audio to the composite audio clip and write the output file
138
  final_clip = video_clip.set_audio(composite_audio_clip)
139
  final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
140
 
@@ -144,7 +143,46 @@ def merge_audio_video(video_filename, audio_filename, output_filename, overlay_a
144
  if 'overlay_audio_clip' in locals():
145
  overlay_audio_clip.close()
146
 
147
- return output_filename
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  def save_temporary_audio_file(uploaded_file):
150
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
 
105
  audio_filename = tmpfile.name
106
 
107
  return audio_filename
108
+
109
  def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
110
  try:
111
  video_clip = VideoFileClip(video_filename)
 
114
  print(f"Error loading video or main audio clip: {e}")
115
  return None
116
 
117
+ # Check if the audio is longer than the video
118
+ if main_audio_clip.duration > video_clip.duration:
119
+ # Calculate the number of times the video should be looped
120
+ loops_required = int(main_audio_clip.duration // video_clip.duration) + 1
121
+ video_clip = concatenate_videoclips([video_clip] * loops_required)
122
+ video_clip = video_clip.subclip(0, main_audio_clip.duration) # Trim the looped video to match the audio's duration
123
+
124
  audio_clips = [main_audio_clip]
125
 
126
+ # Process overlay audio if exists
127
  if overlay_audio_path and os.path.exists(overlay_audio_path):
128
  try:
129
+ overlay_audio_clip = AudioFileClip(overlay_audio_path).volumex(0.1)
 
 
 
 
130
  if overlay_audio_clip.duration > main_audio_clip.duration:
131
  overlay_audio_clip = overlay_audio_clip.subclip(0, main_audio_clip.duration)
132
  audio_clips.append(overlay_audio_clip)
133
  except Exception as e:
134
  print(f"Error processing overlay audio clip: {e}")
 
135
 
 
136
  composite_audio_clip = CompositeAudioClip(audio_clips)
 
 
137
  final_clip = video_clip.set_audio(composite_audio_clip)
138
  final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
139
 
 
143
  if 'overlay_audio_clip' in locals():
144
  overlay_audio_clip.close()
145
 
146
+ return output_filename
147
+ # def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
148
+ # try:
149
+ # video_clip = VideoFileClip(video_filename)
150
+ # main_audio_clip = AudioFileClip(audio_filename)
151
+ # except Exception as e:
152
+ # print(f"Error loading video or main audio clip: {e}")
153
+ # return None
154
+
155
+ # # Start with the main audio clip
156
+ # audio_clips = [main_audio_clip]
157
+
158
+ # if overlay_audio_path and os.path.exists(overlay_audio_path):
159
+ # try:
160
+ # # Load the overlay audio clip
161
+ # overlay_audio_clip = AudioFileClip(overlay_audio_path)
162
+ # # Adjust the overlay audio clip's volume to 10%
163
+ # overlay_audio_clip = overlay_audio_clip.volumex(0.1)
164
+ # # Ensure the overlay audio clip matches the main audio clip's duration
165
+ # if overlay_audio_clip.duration > main_audio_clip.duration:
166
+ # overlay_audio_clip = overlay_audio_clip.subclip(0, main_audio_clip.duration)
167
+ # audio_clips.append(overlay_audio_clip)
168
+ # except Exception as e:
169
+ # print(f"Error processing overlay audio clip: {e}")
170
+ # # Optionally handle the error or continue without the overlay
171
+
172
+ # # Combine the audio clips into a composite
173
+ # composite_audio_clip = CompositeAudioClip(audio_clips)
174
+
175
+ # # Set the video's audio to the composite audio clip and write the output file
176
+ # final_clip = video_clip.set_audio(composite_audio_clip)
177
+ # final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
178
+
179
+ # # Cleanup
180
+ # video_clip.close()
181
+ # main_audio_clip.close()
182
+ # if 'overlay_audio_clip' in locals():
183
+ # overlay_audio_clip.close()
184
+
185
+ # return output_filename
186
 
187
  def save_temporary_audio_file(uploaded_file):
188
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile: