Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -92,29 +92,74 @@ def text_to_audio(text, api_key, voice):
|
|
| 92 |
return audio_filename
|
| 93 |
|
| 94 |
def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
audio_clips = [audio_clip] # Start with the main audio clip
|
| 99 |
|
| 100 |
-
# If there's an overlay audio file, add it to the composite
|
| 101 |
-
if overlay_audio_path:
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
composite_audio_clip = CompositeAudioClip(audio_clips)
|
| 106 |
|
| 107 |
if composite_audio_clip.duration > video_clip.duration:
|
| 108 |
# Extend video with the last frame if audio is longer
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
return output_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
def main():
|
| 120 |
st.title("Pixio Video to Voiceover 🎥🔮")
|
|
|
|
| 92 |
return audio_filename
|
| 93 |
|
| 94 |
def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
|
| 95 |
+
try:
|
| 96 |
+
video_clip = VideoFileClip(video_filename)
|
| 97 |
+
audio_clip = AudioFileClip(audio_filename)
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"Error loading video or audio clip: {e}")
|
| 100 |
+
return None
|
| 101 |
|
| 102 |
audio_clips = [audio_clip] # Start with the main audio clip
|
| 103 |
|
| 104 |
+
# If there's an overlay audio file and it exists, add it to the composite
|
| 105 |
+
if overlay_audio_path and os.path.exists(overlay_audio_path):
|
| 106 |
+
try:
|
| 107 |
+
overlay_clip = AudioFileClip(overlay_audio_path).volumex(0.2)
|
| 108 |
+
audio_clips.append(overlay_clip.set_duration(audio_clip.duration))
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"Error loading overlay audio clip: {e}")
|
| 111 |
+
# Optionally handle the error or continue without the overlay
|
| 112 |
+
elif overlay_audio_path:
|
| 113 |
+
print("Overlay audio file does not exist or path is incorrect.")
|
| 114 |
|
| 115 |
composite_audio_clip = CompositeAudioClip(audio_clips)
|
| 116 |
|
| 117 |
if composite_audio_clip.duration > video_clip.duration:
|
| 118 |
# Extend video with the last frame if audio is longer
|
| 119 |
+
try:
|
| 120 |
+
last_frame = video_clip.to_ImageClip(t=video_clip.duration-1).set_duration(composite_audio_clip.duration - video_clip.duration)
|
| 121 |
+
video_clip = concatenate_videoclips([video_clip, last_frame])
|
| 122 |
+
except Exception as e:
|
| 123 |
+
print(f"Error extending video clip: {e}")
|
| 124 |
+
return None
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
final_clip = video_clip.set_audio(composite_audio_clip)
|
| 128 |
+
final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print(f"Error creating final video file: {e}")
|
| 131 |
+
return None
|
| 132 |
+
finally:
|
| 133 |
+
video_clip.close()
|
| 134 |
+
audio_clip.close()
|
| 135 |
+
if 'overlay_clip' in locals():
|
| 136 |
+
overlay_clip.close()
|
| 137 |
|
| 138 |
return output_filename
|
| 139 |
+
# def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
|
| 140 |
+
# video_clip = VideoFileClip(video_filename)
|
| 141 |
+
# audio_clip = AudioFileClip(audio_filename)
|
| 142 |
+
|
| 143 |
+
# audio_clips = [audio_clip] # Start with the main audio clip
|
| 144 |
+
|
| 145 |
+
# # If there's an overlay audio file, add it to the composite
|
| 146 |
+
# if overlay_audio_path:
|
| 147 |
+
# overlay_clip = AudioFileClip(overlay_audio_path).volumex(0.2)
|
| 148 |
+
# audio_clips.append(overlay_clip.set_duration(audio_clip.duration))
|
| 149 |
+
|
| 150 |
+
# composite_audio_clip = CompositeAudioClip(audio_clips)
|
| 151 |
+
|
| 152 |
+
# if composite_audio_clip.duration > video_clip.duration:
|
| 153 |
+
# # Extend video with the last frame if audio is longer
|
| 154 |
+
# last_frame = video_clip.to_ImageClip(t=video_clip.duration-1).set_duration(composite_audio_clip.duration - video_clip.duration)
|
| 155 |
+
# video_clip = concatenate_videoclips([video_clip, last_frame])
|
| 156 |
+
|
| 157 |
+
# final_clip = video_clip.set_audio(composite_audio_clip)
|
| 158 |
+
# final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
|
| 159 |
+
# video_clip.close()
|
| 160 |
+
# audio_clip.close()
|
| 161 |
+
|
| 162 |
+
# return output_filename
|
| 163 |
|
| 164 |
def main():
|
| 165 |
st.title("Pixio Video to Voiceover 🎥🔮")
|