Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -91,26 +91,29 @@ def text_to_audio(text, api_key, voice):
|
|
| 91 |
|
| 92 |
return audio_filename
|
| 93 |
|
| 94 |
-
def merge_audio_video(video_filename, audio_filename, output_filename,
|
| 95 |
try:
|
| 96 |
video_clip = VideoFileClip(video_filename)
|
| 97 |
audio_clip = AudioFileClip(audio_filename)
|
| 98 |
except Exception as e:
|
| 99 |
-
|
| 100 |
return None
|
| 101 |
|
| 102 |
audio_clips = [audio_clip] # Start with the main audio clip
|
| 103 |
|
| 104 |
-
# If there's an overlay audio file
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
composite_audio_clip = CompositeAudioClip(audio_clips)
|
| 116 |
|
|
@@ -120,50 +123,29 @@ def merge_audio_video(video_filename, audio_filename, output_filename, overlay_a
|
|
| 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 |
-
|
| 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 |
-
|
| 131 |
return None
|
| 132 |
finally:
|
| 133 |
video_clip.close()
|
| 134 |
audio_clip.close()
|
| 135 |
-
if
|
| 136 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
def main():
|
| 165 |
-
st.title("Pixio Video to Voiceover 🎥🔮")
|
| 166 |
-
|
| 167 |
if not check_password():
|
| 168 |
return
|
| 169 |
|
|
@@ -217,10 +199,10 @@ def main():
|
|
| 217 |
prompt = st.text_area("Edit the voiceover script prompt as needed:", value=initial_prompt.format(selected_duration=selected_duration), height=300)
|
| 218 |
|
| 219 |
|
| 220 |
-
|
| 221 |
with st.spinner("Processing..."):
|
| 222 |
base64Frame, video_filename, video_duration = video_to_frames(uploaded_file, 1)
|
| 223 |
-
prompt = script_templates[selected_script_type]
|
| 224 |
text = frames_to_story(base64Frame, prompt, openai_key)
|
| 225 |
audio_filename = text_to_audio(text, openai_key, voice_options[voice])
|
| 226 |
output_video_filename = os.path.splitext(video_filename)[0] + "_output.mp4"
|
|
@@ -234,7 +216,6 @@ def main():
|
|
| 234 |
if __name__ == "__main__":
|
| 235 |
main()
|
| 236 |
|
| 237 |
-
|
| 238 |
# from dotenv import load_dotenv
|
| 239 |
# import streamlit as st
|
| 240 |
# from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip
|
|
|
|
| 91 |
|
| 92 |
return audio_filename
|
| 93 |
|
| 94 |
+
def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_file=None):
|
| 95 |
try:
|
| 96 |
video_clip = VideoFileClip(video_filename)
|
| 97 |
audio_clip = AudioFileClip(audio_filename)
|
| 98 |
except Exception as e:
|
| 99 |
+
st.error(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, add it to the composite
|
| 105 |
+
overlay_audio_path = None
|
| 106 |
+
if overlay_audio_file is not None:
|
| 107 |
+
overlay_audio_path = save_temporary_audio_file(overlay_audio_file)
|
| 108 |
+
if overlay_audio_path and os.path.exists(overlay_audio_path):
|
| 109 |
+
try:
|
| 110 |
+
overlay_clip = AudioFileClip(overlay_audio_path).volumex(0.2)
|
| 111 |
+
audio_clips.append(overlay_clip.set_duration(audio_clip.duration))
|
| 112 |
+
except Exception as e:
|
| 113 |
+
st.error(f"Error loading overlay audio clip: {e}")
|
| 114 |
+
# Optionally handle the error or continue without the overlay
|
| 115 |
+
else:
|
| 116 |
+
st.error("Overlay audio file does not exist or path is incorrect.")
|
| 117 |
|
| 118 |
composite_audio_clip = CompositeAudioClip(audio_clips)
|
| 119 |
|
|
|
|
| 123 |
last_frame = video_clip.to_ImageClip(t=video_clip.duration-1).set_duration(composite_audio_clip.duration - video_clip.duration)
|
| 124 |
video_clip = concatenate_videoclips([video_clip, last_frame])
|
| 125 |
except Exception as e:
|
| 126 |
+
st.error(f"Error extending video clip: {e}")
|
| 127 |
return None
|
| 128 |
|
| 129 |
try:
|
| 130 |
final_clip = video_clip.set_audio(composite_audio_clip)
|
| 131 |
final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
|
| 132 |
except Exception as e:
|
| 133 |
+
st.error(f"Error creating final video file: {e}")
|
| 134 |
return None
|
| 135 |
finally:
|
| 136 |
video_clip.close()
|
| 137 |
audio_clip.close()
|
| 138 |
+
if overlay_audio_path:
|
| 139 |
+
os.remove(overlay_audio_path)
|
| 140 |
|
| 141 |
return output_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
+
def save_temporary_audio_file(uploaded_file):
|
| 144 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
| 145 |
+
tmpfile.write(uploaded_file.getvalue())
|
| 146 |
+
return tmpfile.name
|
| 147 |
|
| 148 |
def main():
|
|
|
|
|
|
|
| 149 |
if not check_password():
|
| 150 |
return
|
| 151 |
|
|
|
|
| 199 |
prompt = st.text_area("Edit the voiceover script prompt as needed:", value=initial_prompt.format(selected_duration=selected_duration), height=300)
|
| 200 |
|
| 201 |
|
| 202 |
+
if uploaded_file is not None and st.button("START PROCESSING"):
|
| 203 |
with st.spinner("Processing..."):
|
| 204 |
base64Frame, video_filename, video_duration = video_to_frames(uploaded_file, 1)
|
| 205 |
+
prompt = script_templates[selected_script_type]
|
| 206 |
text = frames_to_story(base64Frame, prompt, openai_key)
|
| 207 |
audio_filename = text_to_audio(text, openai_key, voice_options[voice])
|
| 208 |
output_video_filename = os.path.splitext(video_filename)[0] + "_output.mp4"
|
|
|
|
| 216 |
if __name__ == "__main__":
|
| 217 |
main()
|
| 218 |
|
|
|
|
| 219 |
# from dotenv import load_dotenv
|
| 220 |
# import streamlit as st
|
| 221 |
# from moviepy.editor import VideoFileClip, concatenate_videoclips, AudioFileClip
|