Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -144,97 +144,6 @@ def merge_audio_video(video_filename, audio_filename, output_filename, overlay_a
|
|
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 |
-
# # Create a list starting with the main audio clip
|
156 |
-
# audio_clips = [main_audio_clip]
|
157 |
-
|
158 |
-
# # If there's an overlay audio file, adjust its volume and add to the composite
|
159 |
-
# if overlay_audio_path and os.path.exists(overlay_audio_path):
|
160 |
-
# try:
|
161 |
-
# overlay_audio_clip = AudioFileClip(overlay_audio_path)
|
162 |
-
# # Adjust the overlay audio clip's volume to 20%
|
163 |
-
# overlay_audio_clip = overlay_audio_clip.volumex(0.2)
|
164 |
-
# audio_clips.append(overlay_audio_clip.set_duration(main_audio_clip.duration))
|
165 |
-
# except Exception as e:
|
166 |
-
# print(f"Error processing overlay audio clip: {e}")
|
167 |
-
# # Depending on your needs, you may choose to return None here or continue without the overlay
|
168 |
-
|
169 |
-
# # Combine the audio clips
|
170 |
-
# composite_audio_clip = CompositeAudioClip(audio_clips)
|
171 |
-
|
172 |
-
# # Ensure the composite audio clip does not exceed the video's duration
|
173 |
-
# if composite_audio_clip.duration > video_clip.duration:
|
174 |
-
# # Extend the video with the last frame if the audio is longer
|
175 |
-
# last_frame = video_clip.to_ImageClip(t=video_clip.duration - 1).set_duration(composite_audio_clip.duration - video_clip.duration)
|
176 |
-
# video_clip = concatenate_videoclips([video_clip, last_frame])
|
177 |
-
|
178 |
-
# # Set the composite audio as the video's audio and write the output file
|
179 |
-
# final_clip = video_clip.set_audio(composite_audio_clip)
|
180 |
-
# final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
|
181 |
-
|
182 |
-
# # Cleanup
|
183 |
-
# video_clip.close()
|
184 |
-
# main_audio_clip.close()
|
185 |
-
# if 'overlay_audio_clip' in locals():
|
186 |
-
# overlay_audio_clip.close()
|
187 |
-
|
188 |
-
# return output_filename
|
189 |
-
|
190 |
-
# def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_file=None):
|
191 |
-
# try:
|
192 |
-
# video_clip = VideoFileClip(video_filename)
|
193 |
-
# audio_clip = AudioFileClip(audio_filename)
|
194 |
-
# except Exception as e:
|
195 |
-
# st.error(f"Error loading video or audio clip: {e}")
|
196 |
-
# return None
|
197 |
-
|
198 |
-
# audio_clips = [audio_clip] # Start with the main audio clip
|
199 |
-
|
200 |
-
# # If there's an overlay audio file, add it to the composite
|
201 |
-
# overlay_audio_path = None
|
202 |
-
# if overlay_audio_file is not None:
|
203 |
-
# overlay_audio_path = save_temporary_audio_file(overlay_audio_file)
|
204 |
-
# if overlay_audio_path and os.path.exists(overlay_audio_path):
|
205 |
-
# try:
|
206 |
-
# overlay_clip = AudioFileClip(overlay_audio_path).volumex(0.2)
|
207 |
-
# audio_clips.append(overlay_clip.set_duration(audio_clip.duration))
|
208 |
-
# except Exception as e:
|
209 |
-
# st.error(f"Error loading overlay audio clip: {e}")
|
210 |
-
# # Optionally handle the error or continue without the overlay
|
211 |
-
# else:
|
212 |
-
# st.error("Overlay audio file does not exist or path is incorrect.")
|
213 |
-
|
214 |
-
# composite_audio_clip = CompositeAudioClip(audio_clips)
|
215 |
-
|
216 |
-
# if composite_audio_clip.duration > video_clip.duration:
|
217 |
-
# # Extend video with the last frame if audio is longer
|
218 |
-
# try:
|
219 |
-
# last_frame = video_clip.to_ImageClip(t=video_clip.duration-1).set_duration(composite_audio_clip.duration - video_clip.duration)
|
220 |
-
# video_clip = concatenate_videoclips([video_clip, last_frame])
|
221 |
-
# except Exception as e:
|
222 |
-
# st.error(f"Error extending video clip: {e}")
|
223 |
-
# return None
|
224 |
-
|
225 |
-
# try:
|
226 |
-
# final_clip = video_clip.set_audio(composite_audio_clip)
|
227 |
-
# final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
|
228 |
-
# except Exception as e:
|
229 |
-
# st.error(f"Error creating final video file: {e}")
|
230 |
-
# return None
|
231 |
-
# finally:
|
232 |
-
# video_clip.close()
|
233 |
-
# audio_clip.close()
|
234 |
-
# if overlay_audio_path:
|
235 |
-
# os.remove(overlay_audio_path)
|
236 |
-
|
237 |
-
# return output_filename
|
238 |
|
239 |
def save_temporary_audio_file(uploaded_file):
|
240 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
@@ -312,33 +221,27 @@ def main():
|
|
312 |
if uploaded_video_file is not None and st.button("START PROCESSING"):
|
313 |
with st.spinner("Processing..."):
|
314 |
overlay_audio_path = None
|
315 |
-
if
|
316 |
-
|
317 |
-
|
318 |
-
overlay_audio_path = save_temporary_audio_file(overlay_audio_file)
|
319 |
|
320 |
-
|
321 |
-
|
322 |
-
base64Frame, video_filename, video_duration = video_to_frames(uploaded_file, frame_sampling_rate=1)
|
323 |
|
324 |
-
|
325 |
-
prompt = prompt_template.format(selected_duration=selected_duration)
|
326 |
text = frames_to_story(base64Frame, prompt, openai_key)
|
327 |
|
328 |
audio_filename = text_to_audio(text, openai_key, voice_options[voice])
|
329 |
output_video_filename = os.path.splitext(video_filename)[0] + "_output.mp4"
|
330 |
final_video_filename = merge_audio_video(video_filename, audio_filename, output_video_filename, overlay_audio_path)
|
331 |
|
332 |
-
# Display the generated script to the user
|
333 |
st.subheader("Generated Script")
|
334 |
st.write(text)
|
335 |
|
336 |
if final_video_filename:
|
337 |
-
# Display the final video with voiceover
|
338 |
st.subheader("Final Video with Voiceover")
|
339 |
st.video(final_video_filename)
|
340 |
|
341 |
-
# Cleanup temporary files
|
342 |
os.remove(video_filename)
|
343 |
os.remove(audio_filename)
|
344 |
if overlay_audio_path:
|
|
|
144 |
overlay_audio_clip.close()
|
145 |
|
146 |
return output_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
def save_temporary_audio_file(uploaded_file):
|
149 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
|
|
221 |
if uploaded_video_file is not None and st.button("START PROCESSING"):
|
222 |
with st.spinner("Processing..."):
|
223 |
overlay_audio_path = None
|
224 |
+
if uploaded_audio_file is not None:
|
225 |
+
uploaded_audio_file.seek(0)
|
226 |
+
overlay_audio_path = save_temporary_audio_file(uploaded_audio_file)
|
|
|
227 |
|
228 |
+
uploaded_video_file.seek(0)
|
229 |
+
base64Frame, video_filename, video_duration = video_to_frames(uploaded_video_file, frame_sampling_rate=1)
|
|
|
230 |
|
231 |
+
prompt = prompt_template
|
|
|
232 |
text = frames_to_story(base64Frame, prompt, openai_key)
|
233 |
|
234 |
audio_filename = text_to_audio(text, openai_key, voice_options[voice])
|
235 |
output_video_filename = os.path.splitext(video_filename)[0] + "_output.mp4"
|
236 |
final_video_filename = merge_audio_video(video_filename, audio_filename, output_video_filename, overlay_audio_path)
|
237 |
|
|
|
238 |
st.subheader("Generated Script")
|
239 |
st.write(text)
|
240 |
|
241 |
if final_video_filename:
|
|
|
242 |
st.subheader("Final Video with Voiceover")
|
243 |
st.video(final_video_filename)
|
244 |
|
|
|
245 |
os.remove(video_filename)
|
246 |
os.remove(audio_filename)
|
247 |
if overlay_audio_path:
|