tsi-org commited on
Commit
fc8da00
·
verified ·
1 Parent(s): a6bf494

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -15
app.py CHANGED
@@ -112,30 +112,27 @@ def merge_audio_video(video_filename, audio_filename, output_filename, overlay_a
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
 
@@ -145,7 +142,49 @@ def merge_audio_video(video_filename, audio_filename, output_filename, overlay_a
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:
@@ -210,8 +249,18 @@ def main():
210
  st.error("OpenAI API key is not set in .env.local")
211
  return
212
 
213
- uploaded_file = st.file_uploader("Select a video file", type=["mp4", "avi"])
214
- overlay_audio_file = st.file_uploader("Upload overlay audio (optional)", type=["mp3", "wav"])
 
 
 
 
 
 
 
 
 
 
215
 
216
  voice_options = {'Echo (Male)': 'echo', 'Fable (Male)': 'fable', 'Onyx (Male)': 'onyx', 'Nova (Female)': 'nova', 'Shimmer (Female)': 'shimmer', 'Alloy (Female)': 'alloy'}
217
  voice = st.selectbox('Choose the voice you want', list(voice_options.keys()))
@@ -270,6 +319,10 @@ def main():
270
  output_video_filename = os.path.splitext(video_filename)[0] + "_output.mp4"
271
  final_video_filename = merge_audio_video(video_filename, audio_filename, output_video_filename, overlay_audio_path)
272
 
 
 
 
 
273
  if final_video_filename:
274
  st.video(final_video_filename)
275
 
 
112
  print(f"Error loading video or main audio clip: {e}")
113
  return None
114
 
115
+ # Start with the main audio clip
116
  audio_clips = [main_audio_clip]
117
 
 
118
  if overlay_audio_path and os.path.exists(overlay_audio_path):
119
  try:
120
+ # Load the overlay audio clip
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
+ # Ensure the overlay audio clip matches the main audio clip's duration
125
+ if overlay_audio_clip.duration > main_audio_clip.duration:
126
+ overlay_audio_clip = overlay_audio_clip.subclip(0, main_audio_clip.duration)
127
+ audio_clips.append(overlay_audio_clip)
128
  except Exception as e:
129
  print(f"Error processing overlay audio clip: {e}")
130
+ # Optionally handle the error or continue without the overlay
131
 
132
+ # Combine the audio clips into a composite
133
  composite_audio_clip = CompositeAudioClip(audio_clips)
134
 
135
+ # Set the video's audio to the composite audio clip and write the output file
 
 
 
 
 
 
136
  final_clip = video_clip.set_audio(composite_audio_clip)
137
  final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
138
 
 
142
  if 'overlay_audio_clip' in locals():
143
  overlay_audio_clip.close()
144
 
145
+ return output_filename
146
+ # def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_path=None):
147
+ # try:
148
+ # video_clip = VideoFileClip(video_filename)
149
+ # main_audio_clip = AudioFileClip(audio_filename)
150
+ # except Exception as e:
151
+ # print(f"Error loading video or main audio clip: {e}")
152
+ # return None
153
+
154
+ # # Create a list starting with the main audio clip
155
+ # audio_clips = [main_audio_clip]
156
+
157
+ # # If there's an overlay audio file, adjust its volume and add to the composite
158
+ # if overlay_audio_path and os.path.exists(overlay_audio_path):
159
+ # try:
160
+ # overlay_audio_clip = AudioFileClip(overlay_audio_path)
161
+ # # Adjust the overlay audio clip's volume to 20%
162
+ # overlay_audio_clip = overlay_audio_clip.volumex(0.2)
163
+ # audio_clips.append(overlay_audio_clip.set_duration(main_audio_clip.duration))
164
+ # except Exception as e:
165
+ # print(f"Error processing overlay audio clip: {e}")
166
+ # # Depending on your needs, you may choose to return None here or continue without the overlay
167
+
168
+ # # Combine the audio clips
169
+ # composite_audio_clip = CompositeAudioClip(audio_clips)
170
+
171
+ # # Ensure the composite audio clip does not exceed the video's duration
172
+ # if composite_audio_clip.duration > video_clip.duration:
173
+ # # Extend the video with the last frame if the audio is longer
174
+ # last_frame = video_clip.to_ImageClip(t=video_clip.duration - 1).set_duration(composite_audio_clip.duration - video_clip.duration)
175
+ # video_clip = concatenate_videoclips([video_clip, last_frame])
176
+
177
+ # # Set the composite audio as the video's audio and write the output file
178
+ # final_clip = video_clip.set_audio(composite_audio_clip)
179
+ # final_clip.write_videofile(output_filename, codec='libx264', audio_codec="aac")
180
+
181
+ # # Cleanup
182
+ # video_clip.close()
183
+ # main_audio_clip.close()
184
+ # if 'overlay_audio_clip' in locals():
185
+ # overlay_audio_clip.close()
186
+
187
+ # return output_filename
188
 
189
  # def merge_audio_video(video_filename, audio_filename, output_filename, overlay_audio_file=None):
190
  # try:
 
249
  st.error("OpenAI API key is not set in .env.local")
250
  return
251
 
252
+ uploaded_video_file = st.file_uploader("Select a video file", type=["mp4", "avi"])
253
+ if uploaded_video_file is not None:
254
+ # Display a preview of the uploaded video file
255
+ st.video(uploaded_video_file)
256
+
257
+ uploaded_audio_file = st.file_uploader("Upload overlay audio (optional)", type=["mp3", "wav"])
258
+ if uploaded_audio_file is not None:
259
+ # Convert the uploaded audio file to bytes for st.audio to display
260
+ # Streamlit's st.audio requires the data to be in bytes
261
+ audio_bytes = uploaded_audio_file.read()
262
+ # Display a preview of the uploaded audio file
263
+ st.audio(audio_bytes, format='audio/wav')
264
 
265
  voice_options = {'Echo (Male)': 'echo', 'Fable (Male)': 'fable', 'Onyx (Male)': 'onyx', 'Nova (Female)': 'nova', 'Shimmer (Female)': 'shimmer', 'Alloy (Female)': 'alloy'}
266
  voice = st.selectbox('Choose the voice you want', list(voice_options.keys()))
 
319
  output_video_filename = os.path.splitext(video_filename)[0] + "_output.mp4"
320
  final_video_filename = merge_audio_video(video_filename, audio_filename, output_video_filename, overlay_audio_path)
321
 
322
+ # Display the generated script to the user
323
+ st.subheader("Generated Script")
324
+ st.write(text) # Use st.text(text) if you prefer plain text without markdown
325
+
326
  if final_video_filename:
327
  st.video(final_video_filename)
328