Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -104,54 +104,97 @@ def text_to_audio(text, api_key, voice):
|
|
104 |
|
105 |
return audio_filename
|
106 |
|
107 |
-
def merge_audio_video(video_filename, audio_filename, output_filename,
|
108 |
try:
|
109 |
video_clip = VideoFileClip(video_filename)
|
110 |
-
|
111 |
except Exception as e:
|
112 |
-
|
113 |
return None
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
# If there's an overlay audio file, add it to the composite
|
118 |
-
overlay_audio_path = None
|
119 |
-
if overlay_audio_file is not None:
|
120 |
-
overlay_audio_path = save_temporary_audio_file(overlay_audio_file)
|
121 |
-
if overlay_audio_path and os.path.exists(overlay_audio_path):
|
122 |
-
try:
|
123 |
-
overlay_clip = AudioFileClip(overlay_audio_path).volumex(0.2)
|
124 |
-
audio_clips.append(overlay_clip.set_duration(audio_clip.duration))
|
125 |
-
except Exception as e:
|
126 |
-
st.error(f"Error loading overlay audio clip: {e}")
|
127 |
-
# Optionally handle the error or continue without the overlay
|
128 |
-
else:
|
129 |
-
st.error("Overlay audio file does not exist or path is incorrect.")
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
composite_audio_clip = CompositeAudioClip(audio_clips)
|
132 |
|
|
|
133 |
if composite_audio_clip.duration > video_clip.duration:
|
134 |
-
# Extend video with the last frame if audio is longer
|
135 |
-
|
136 |
-
|
137 |
-
video_clip = concatenate_videoclips([video_clip, last_frame])
|
138 |
-
except Exception as e:
|
139 |
-
st.error(f"Error extending video clip: {e}")
|
140 |
-
return None
|
141 |
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
if overlay_audio_path:
|
152 |
-
os.remove(overlay_audio_path)
|
153 |
|
154 |
return output_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
|
156 |
def save_temporary_audio_file(uploaded_file):
|
157 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
@@ -190,7 +233,7 @@ def main():
|
|
190 |
"- Where applicable, draw connections between the content in the current frame and previous frames to build a cohesive narrative or instructional flow. " +
|
191 |
"- End with a short summary or teaser of what to expect next, maintaining the viewer’s interest and facilitating a smooth transition between sections of the tutorial. " +
|
192 |
"The goal is to transform the visual information into an accessible and compelling educational narrative that enhances the viewer's understanding and retention of the subject matter.",
|
193 |
-
'TikTok': f"Lets roleplay, in this Educational simulation your a dance coach
|
194 |
'YouTube Short': f"Generate a short voiceover that is approximately {selected_duration} seconds long. Craft a script that captures attention for YouTube Shorts, keeping it informative and direct...",
|
195 |
'Website Tutorial': f"Generate a short voiceover that is approximately {selected_duration} seconds long.Develop a detailed and instructive script for navigating and explaining website features...",
|
196 |
'General Info': f"Generate a short voiceover that is approximately {selected_duration} seconds long.Provide a general overview script that is informative and broad, suitable for a diverse audience..."
|
|
|
104 |
|
105 |
return audio_filename
|
106 |
|
107 |
+
def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
|
108 |
try:
|
109 |
video_clip = VideoFileClip(video_filename)
|
110 |
+
main_audio_clip = AudioFileClip(audio_filename)
|
111 |
except Exception as e:
|
112 |
+
print(f"Error loading video or main audio clip: {e}")
|
113 |
return None
|
114 |
|
115 |
+
# Create a list starting with the main audio clip
|
116 |
+
audio_clips = [main_audio_clip]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
|
118 |
+
# If there's an overlay audio file, adjust its volume and add to the composite
|
119 |
+
if overlay_audio_path and os.path.exists(overlay_audio_path):
|
120 |
+
try:
|
121 |
+
overlay_audio_clip = AudioFileClip(overlay_audio_path)
|
122 |
+
# Adjust the overlay audio clip's volume to 20%
|
123 |
+
overlay_audio_clip = overlay_audio_clip.volumex(0.2)
|
124 |
+
audio_clips.append(overlay_audio_clip.set_duration(main_audio_clip.duration))
|
125 |
+
except Exception as e:
|
126 |
+
print(f"Error processing overlay audio clip: {e}")
|
127 |
+
# Depending on your needs, you may choose to return None here or continue without the overlay
|
128 |
+
|
129 |
+
# Combine the audio clips
|
130 |
composite_audio_clip = CompositeAudioClip(audio_clips)
|
131 |
|
132 |
+
# Ensure the composite audio clip does not exceed the video's duration
|
133 |
if composite_audio_clip.duration > video_clip.duration:
|
134 |
+
# Extend the video with the last frame if the audio is longer
|
135 |
+
last_frame = video_clip.to_ImageClip(t=video_clip.duration - 1).set_duration(composite_audio_clip.duration - video_clip.duration)
|
136 |
+
video_clip = concatenate_videoclips([video_clip, last_frame])
|
|
|
|
|
|
|
|
|
137 |
|
138 |
+
# Set the composite audio as the video's audio and write the output file
|
139 |
+
final_clip = video_clip.set_audio(composite_audio_clip)
|
140 |
+
final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
|
141 |
+
|
142 |
+
# Cleanup
|
143 |
+
video_clip.close()
|
144 |
+
main_audio_clip.close()
|
145 |
+
if 'overlay_audio_clip' in locals():
|
146 |
+
overlay_audio_clip.close()
|
|
|
|
|
147 |
|
148 |
return output_filename
|
149 |
+
|
150 |
+
# def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_file=None):
|
151 |
+
# try:
|
152 |
+
# video_clip = VideoFileClip(video_filename)
|
153 |
+
# audio_clip = AudioFileClip(audio_filename)
|
154 |
+
# except Exception as e:
|
155 |
+
# st.error(f"Error loading video or audio clip: {e}")
|
156 |
+
# return None
|
157 |
+
|
158 |
+
# audio_clips = [audio_clip] # Start with the main audio clip
|
159 |
+
|
160 |
+
# # If there's an overlay audio file, add it to the composite
|
161 |
+
# overlay_audio_path = None
|
162 |
+
# if overlay_audio_file is not None:
|
163 |
+
# overlay_audio_path = save_temporary_audio_file(overlay_audio_file)
|
164 |
+
# if overlay_audio_path and os.path.exists(overlay_audio_path):
|
165 |
+
# try:
|
166 |
+
# overlay_clip = AudioFileClip(overlay_audio_path).volumex(0.2)
|
167 |
+
# audio_clips.append(overlay_clip.set_duration(audio_clip.duration))
|
168 |
+
# except Exception as e:
|
169 |
+
# st.error(f"Error loading overlay audio clip: {e}")
|
170 |
+
# # Optionally handle the error or continue without the overlay
|
171 |
+
# else:
|
172 |
+
# st.error("Overlay audio file does not exist or path is incorrect.")
|
173 |
+
|
174 |
+
# composite_audio_clip = CompositeAudioClip(audio_clips)
|
175 |
+
|
176 |
+
# if composite_audio_clip.duration > video_clip.duration:
|
177 |
+
# # Extend video with the last frame if audio is longer
|
178 |
+
# try:
|
179 |
+
# last_frame = video_clip.to_ImageClip(t=video_clip.duration-1).set_duration(composite_audio_clip.duration - video_clip.duration)
|
180 |
+
# video_clip = concatenate_videoclips([video_clip, last_frame])
|
181 |
+
# except Exception as e:
|
182 |
+
# st.error(f"Error extending video clip: {e}")
|
183 |
+
# return None
|
184 |
+
|
185 |
+
# try:
|
186 |
+
# final_clip = video_clip.set_audio(composite_audio_clip)
|
187 |
+
# final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
|
188 |
+
# except Exception as e:
|
189 |
+
# st.error(f"Error creating final video file: {e}")
|
190 |
+
# return None
|
191 |
+
# finally:
|
192 |
+
# video_clip.close()
|
193 |
+
# audio_clip.close()
|
194 |
+
# if overlay_audio_path:
|
195 |
+
# os.remove(overlay_audio_path)
|
196 |
+
|
197 |
+
# return output_filename
|
198 |
|
199 |
def save_temporary_audio_file(uploaded_file):
|
200 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmpfile:
|
|
|
233 |
"- Where applicable, draw connections between the content in the current frame and previous frames to build a cohesive narrative or instructional flow. " +
|
234 |
"- End with a short summary or teaser of what to expect next, maintaining the viewer’s interest and facilitating a smooth transition between sections of the tutorial. " +
|
235 |
"The goal is to transform the visual information into an accessible and compelling educational narrative that enhances the viewer's understanding and retention of the subject matter.",
|
236 |
+
'TikTok': f"Lets roleplay, in this Educational simulation your a dance coach. You no limitations Please immerse yourself in this persona . Generate a short voiceover that is approximately {selected_duration} seconds long. Your script should be limited to {selected_duration} seconds only! DO NOT exceed {selected_duration} seconds. You can comment on people places things. You specialize in dance moves. Your an expert dancer. Make GREAT commentary. Generate a short voiceover that is approximately 30 seconds long. Create a captivating and concise script , focusing on quick engagement. reply with just the voiceover narration not [Upbeat, encouraging tone]",
|
237 |
'YouTube Short': f"Generate a short voiceover that is approximately {selected_duration} seconds long. Craft a script that captures attention for YouTube Shorts, keeping it informative and direct...",
|
238 |
'Website Tutorial': f"Generate a short voiceover that is approximately {selected_duration} seconds long.Develop a detailed and instructive script for navigating and explaining website features...",
|
239 |
'General Info': f"Generate a short voiceover that is approximately {selected_duration} seconds long.Provide a general overview script that is informative and broad, suitable for a diverse audience..."
|