Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,32 +4,48 @@ import ffmpeg
|
|
| 4 |
import gradio as gr
|
| 5 |
import traceback
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Function to add music to video
|
| 8 |
def add_music_to_video(video_path, chunks_folder="chunks", output_path="output_with_music.mp4"):
|
| 9 |
try:
|
| 10 |
-
|
|
|
|
|
|
|
| 11 |
if not os.path.exists(chunks_folder):
|
| 12 |
-
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
music_files = [os.path.join(chunks_folder, f) for f in os.listdir(chunks_folder) if f.endswith('.mp3')]
|
| 15 |
if not music_files:
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Randomly select one MP3 file
|
| 19 |
music_file_path = random.choice(music_files)
|
| 20 |
-
|
| 21 |
|
| 22 |
# Get the duration of the video
|
| 23 |
video_info = ffmpeg.probe(video_path)
|
| 24 |
video_duration = float(video_info['streams'][0]['duration'])
|
| 25 |
-
|
| 26 |
|
| 27 |
# Trim the music file to match the video duration
|
| 28 |
trimmed_music_path = "trimmed_music.mp3"
|
| 29 |
ffmpeg.input(music_file_path).output(
|
| 30 |
trimmed_music_path, ss=0, t=video_duration
|
| 31 |
).run(overwrite_output=True)
|
| 32 |
-
|
| 33 |
|
| 34 |
# Combine video and audio
|
| 35 |
video_input = ffmpeg.input(video_path)
|
|
@@ -37,37 +53,51 @@ def add_music_to_video(video_path, chunks_folder="chunks", output_path="output_w
|
|
| 37 |
ffmpeg.concat(video_input, audio_input, v=1, a=1).output(
|
| 38 |
output_path, vcodec="libx264", acodec="aac", strict="experimental"
|
| 39 |
).run(overwrite_output=True)
|
| 40 |
-
|
| 41 |
|
| 42 |
return output_path
|
| 43 |
except Exception as e:
|
| 44 |
-
|
|
|
|
| 45 |
traceback.print_exc()
|
| 46 |
-
return
|
| 47 |
|
| 48 |
# Define Gradio interface
|
| 49 |
def process_video(uploaded_video):
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
video_path = uploaded_video.name
|
| 53 |
-
|
|
|
|
| 54 |
output_video = add_music_to_video(video_path, chunks_folder="/content/chunks")
|
| 55 |
if "Error" in output_video:
|
| 56 |
-
return
|
| 57 |
-
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
-
|
|
|
|
| 60 |
traceback.print_exc()
|
| 61 |
-
return
|
| 62 |
|
| 63 |
# Gradio Interface
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Launch Gradio app
|
| 73 |
interface.launch(share=True, debug=True)
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
import traceback
|
| 6 |
|
| 7 |
+
# Global variable to store logs
|
| 8 |
+
debug_logs = []
|
| 9 |
+
|
| 10 |
+
# Function to log messages
|
| 11 |
+
def log_message(message):
|
| 12 |
+
global debug_logs
|
| 13 |
+
debug_logs.append(message)
|
| 14 |
+
print(message) # Print to console as well for backend debugging
|
| 15 |
+
|
| 16 |
# Function to add music to video
|
| 17 |
def add_music_to_video(video_path, chunks_folder="chunks", output_path="output_with_music.mp4"):
|
| 18 |
try:
|
| 19 |
+
log_message("Starting the process to add music to video...")
|
| 20 |
+
|
| 21 |
+
# Check if chunks folder exists
|
| 22 |
if not os.path.exists(chunks_folder):
|
| 23 |
+
error_message = "Error: Chunks folder does not exist."
|
| 24 |
+
log_message(error_message)
|
| 25 |
+
return error_message
|
| 26 |
|
| 27 |
+
# List all MP3 files in the chunks folder
|
| 28 |
music_files = [os.path.join(chunks_folder, f) for f in os.listdir(chunks_folder) if f.endswith('.mp3')]
|
| 29 |
if not music_files:
|
| 30 |
+
error_message = "Error: No audio files found in the chunks folder."
|
| 31 |
+
log_message(error_message)
|
| 32 |
+
return error_message
|
| 33 |
|
| 34 |
# Randomly select one MP3 file
|
| 35 |
music_file_path = random.choice(music_files)
|
| 36 |
+
log_message(f"Selected music file: {music_file_path}")
|
| 37 |
|
| 38 |
# Get the duration of the video
|
| 39 |
video_info = ffmpeg.probe(video_path)
|
| 40 |
video_duration = float(video_info['streams'][0]['duration'])
|
| 41 |
+
log_message(f"Video duration: {video_duration} seconds")
|
| 42 |
|
| 43 |
# Trim the music file to match the video duration
|
| 44 |
trimmed_music_path = "trimmed_music.mp3"
|
| 45 |
ffmpeg.input(music_file_path).output(
|
| 46 |
trimmed_music_path, ss=0, t=video_duration
|
| 47 |
).run(overwrite_output=True)
|
| 48 |
+
log_message(f"Trimmed music saved at: {trimmed_music_path}")
|
| 49 |
|
| 50 |
# Combine video and audio
|
| 51 |
video_input = ffmpeg.input(video_path)
|
|
|
|
| 53 |
ffmpeg.concat(video_input, audio_input, v=1, a=1).output(
|
| 54 |
output_path, vcodec="libx264", acodec="aac", strict="experimental"
|
| 55 |
).run(overwrite_output=True)
|
| 56 |
+
log_message(f"Output video saved at: {output_path}")
|
| 57 |
|
| 58 |
return output_path
|
| 59 |
except Exception as e:
|
| 60 |
+
error_message = f"Error during processing: {str(e)}"
|
| 61 |
+
log_message(error_message)
|
| 62 |
traceback.print_exc()
|
| 63 |
+
return error_message
|
| 64 |
|
| 65 |
# Define Gradio interface
|
| 66 |
def process_video(uploaded_video):
|
| 67 |
try:
|
| 68 |
+
log_message("Video received for processing.")
|
| 69 |
video_path = uploaded_video.name
|
| 70 |
+
log_message(f"Uploaded video path: {video_path}")
|
| 71 |
+
|
| 72 |
output_video = add_music_to_video(video_path, chunks_folder="/content/chunks")
|
| 73 |
if "Error" in output_video:
|
| 74 |
+
return None, "\n".join(debug_logs)
|
| 75 |
+
|
| 76 |
+
return output_video, "\n".join(debug_logs)
|
| 77 |
except Exception as e:
|
| 78 |
+
error_message = f"Error in process_video: {str(e)}"
|
| 79 |
+
log_message(error_message)
|
| 80 |
traceback.print_exc()
|
| 81 |
+
return None, "\n".join(debug_logs)
|
| 82 |
|
| 83 |
# Gradio Interface
|
| 84 |
+
with gr.Blocks() as interface:
|
| 85 |
+
gr.Markdown("# Add Background Music to Video")
|
| 86 |
+
gr.Markdown("Upload a video, and this app will add a background music track that matches the video duration.")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
with gr.Column():
|
| 90 |
+
video_input = gr.Video(label="Upload Video")
|
| 91 |
+
submit_button = gr.Button("Generate Video")
|
| 92 |
+
with gr.Column():
|
| 93 |
+
video_output = gr.Video(label="Video with Background Music")
|
| 94 |
+
debug_output = gr.Textbox(label="Debug Logs", lines=10, interactive=False)
|
| 95 |
+
|
| 96 |
+
submit_button.click(
|
| 97 |
+
fn=process_video,
|
| 98 |
+
inputs=[video_input],
|
| 99 |
+
outputs=[video_output, debug_output]
|
| 100 |
+
)
|
| 101 |
|
| 102 |
# Launch Gradio app
|
| 103 |
interface.launch(share=True, debug=True)
|