Spaces:
Sleeping
Sleeping
Commit
·
4cdd885
1
Parent(s):
599e71e
Added volume mixing and download of final mixed video
Browse files- def __init__.py +0 -1
- main.py +82 -11
def __init__.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
from .logger import logging
|
|
|
|
|
|
main.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from engine import DescribeVideo, GenerateAudio
|
| 3 |
import os
|
| 4 |
-
from moviepy.editor import VideoFileClip
|
|
|
|
| 5 |
|
| 6 |
# Define model maps
|
| 7 |
video_model_map = {
|
|
@@ -35,6 +36,7 @@ genre_map = {
|
|
| 35 |
"Lo-Fi": "Lo-Fi",
|
| 36 |
}
|
| 37 |
|
|
|
|
| 38 |
st.set_page_config(
|
| 39 |
page_title="VidTune: Where Videos Find Their Melody", layout="centered"
|
| 40 |
)
|
|
@@ -60,7 +62,17 @@ if "music_bpm" not in st.session_state:
|
|
| 60 |
st.session_state.music_bpm = 100
|
| 61 |
if "user_keywords" not in st.session_state:
|
| 62 |
st.session_state.user_keywords = None
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# Sidebar
|
| 65 |
st.sidebar.title("Settings")
|
| 66 |
|
|
@@ -141,12 +153,11 @@ if os.path.exists("temp.mp4") and uploaded_video is not None:
|
|
| 141 |
st.video(uploaded_video)
|
| 142 |
|
| 143 |
# Submit button if video is not uploaded
|
| 144 |
-
if generate_button
|
| 145 |
-
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
-
# Submit Button and music generation if video is uploaded
|
| 149 |
-
if generate_button and uploaded_video is not None:
|
| 150 |
with st.spinner("Analyzing video..."):
|
| 151 |
video_description = video_descriptor.describe_video(
|
| 152 |
"temp.mp4",
|
|
@@ -181,9 +192,69 @@ if generate_button and uploaded_video is not None:
|
|
| 181 |
)
|
| 182 |
music_prompt = [music_prompt] * st.session_state.num_samples
|
| 183 |
audio_generator.generate_audio(music_prompt, duration=video_duration)
|
| 184 |
-
audio_paths = audio_generator.save_audio()
|
| 185 |
st.success("Music generated successfully.")
|
| 186 |
-
for i, audio_path in enumerate(audio_paths):
|
| 187 |
-
st.audio(audio_path, format="audio/wav")
|
| 188 |
-
|
| 189 |
st.balloons()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from engine import DescribeVideo, GenerateAudio
|
| 3 |
import os
|
| 4 |
+
from moviepy.editor import VideoFileClip, AudioFileClip, CompositeAudioClip
|
| 5 |
+
from moviepy.audio.fx.volumex import volumex
|
| 6 |
|
| 7 |
# Define model maps
|
| 8 |
video_model_map = {
|
|
|
|
| 36 |
"Lo-Fi": "Lo-Fi",
|
| 37 |
}
|
| 38 |
|
| 39 |
+
# Streamlit page configuration
|
| 40 |
st.set_page_config(
|
| 41 |
page_title="VidTune: Where Videos Find Their Melody", layout="centered"
|
| 42 |
)
|
|
|
|
| 62 |
st.session_state.music_bpm = 100
|
| 63 |
if "user_keywords" not in st.session_state:
|
| 64 |
st.session_state.user_keywords = None
|
| 65 |
+
if "selected_audio" not in st.session_state:
|
| 66 |
+
st.session_state.selected_audio = "None"
|
| 67 |
+
if "audio_paths" not in st.session_state:
|
| 68 |
+
st.session_state.audio_paths = []
|
| 69 |
+
if "selected_audio_path" not in st.session_state:
|
| 70 |
+
st.session_state.selected_audio_path = None
|
| 71 |
+
if "orig_audio_vol" not in st.session_state:
|
| 72 |
+
st.session_state.orig_audio_vol = 100
|
| 73 |
+
if "generated_audio_vol" not in st.session_state:
|
| 74 |
+
st.session_state.generated_audio_vol = 100
|
| 75 |
+
|
| 76 |
# Sidebar
|
| 77 |
st.sidebar.title("Settings")
|
| 78 |
|
|
|
|
| 153 |
st.video(uploaded_video)
|
| 154 |
|
| 155 |
# Submit button if video is not uploaded
|
| 156 |
+
if generate_button:
|
| 157 |
+
if uploaded_video is None:
|
| 158 |
+
st.error("Please upload a video before generating music.")
|
| 159 |
+
st.stop()
|
| 160 |
|
|
|
|
|
|
|
| 161 |
with st.spinner("Analyzing video..."):
|
| 162 |
video_description = video_descriptor.describe_video(
|
| 163 |
"temp.mp4",
|
|
|
|
| 192 |
)
|
| 193 |
music_prompt = [music_prompt] * st.session_state.num_samples
|
| 194 |
audio_generator.generate_audio(music_prompt, duration=video_duration)
|
| 195 |
+
st.session_state.audio_paths = audio_generator.save_audio()
|
| 196 |
st.success("Music generated successfully.")
|
|
|
|
|
|
|
|
|
|
| 197 |
st.balloons()
|
| 198 |
+
|
| 199 |
+
# Callback function for radio button selection change
|
| 200 |
+
def on_audio_selection_change():
|
| 201 |
+
selected_index = audio_options.index(st.session_state.selected_audio) - 1
|
| 202 |
+
if selected_index >= 0:
|
| 203 |
+
st.session_state.selected_audio_path = st.session_state.audio_paths[selected_index]
|
| 204 |
+
else:
|
| 205 |
+
st.session_state.selected_audio_path = None
|
| 206 |
+
|
| 207 |
+
# Display radio buttons and handle audio selections
|
| 208 |
+
if st.session_state.audio_paths:
|
| 209 |
+
for i, audio_path in enumerate(st.session_state.audio_paths):
|
| 210 |
+
st.audio(audio_path, format="audio/wav")
|
| 211 |
+
|
| 212 |
+
audio_options = ["None"]+[f"Sample {i+1}" for i in range(len(st.session_state.audio_paths))]
|
| 213 |
+
st.radio(
|
| 214 |
+
"Select one of the generated audio files for further processing:",
|
| 215 |
+
audio_options,
|
| 216 |
+
index=0,
|
| 217 |
+
key="selected_audio",
|
| 218 |
+
on_change=on_audio_selection_change
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
if st.session_state.selected_audio_path:
|
| 222 |
+
st.write(f"**Selected Audio:** {st.session_state.selected_audio_path}")
|
| 223 |
+
|
| 224 |
+
# Handle Audio Mixing and Export
|
| 225 |
+
if st.session_state.selected_audio_path is not None:
|
| 226 |
+
orig_clip = VideoFileClip("temp.mp4")
|
| 227 |
+
orig_clip_audio = orig_clip.audio
|
| 228 |
+
generated_audio = AudioFileClip(st.session_state.selected_audio_path)
|
| 229 |
+
|
| 230 |
+
st.session_state.orig_audio_vol = st.slider(
|
| 231 |
+
"Original Audio Volume", 0, 200, st.session_state.orig_audio_vol
|
| 232 |
+
)
|
| 233 |
+
|
| 234 |
+
st.session_state.generated_audio_vol = st.slider(
|
| 235 |
+
"Selected Sample Volume", 0, 200, st.session_state.generated_audio_vol
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
orig_clip_audio = volumex(orig_clip_audio, float(st.session_state.orig_audio_vol/100))
|
| 239 |
+
generated_audio = volumex(generated_audio, float(st.session_state.generated_audio_vol/100))
|
| 240 |
+
|
| 241 |
+
orig_clip.audio = CompositeAudioClip([orig_clip_audio, generated_audio])
|
| 242 |
+
|
| 243 |
+
final_video_path="out_tmp.mp4"
|
| 244 |
+
orig_clip.write_videofile(final_video_path)
|
| 245 |
+
|
| 246 |
+
orig_clip.close()
|
| 247 |
+
generated_audio.close()
|
| 248 |
+
|
| 249 |
+
st.session_state.final_video_path = final_video_path
|
| 250 |
+
|
| 251 |
+
st.video(final_video_path)
|
| 252 |
+
|
| 253 |
+
if st.session_state.final_video_path:
|
| 254 |
+
with open(st.session_state.final_video_path, "rb") as video_file:
|
| 255 |
+
st.download_button(
|
| 256 |
+
label="Download final video",
|
| 257 |
+
data=video_file,
|
| 258 |
+
file_name="final_video.mp4",
|
| 259 |
+
mime="video/mp4",
|
| 260 |
+
)
|