Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,63 @@ import os
|
|
| 2 |
import random
|
| 3 |
import ffmpeg
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
# Function to add music to video
|
| 7 |
def add_music_to_video(video_path, chunks_folder="chunks", output_path="output_with_music.mp4"):
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Define Gradio interface
|
| 38 |
def process_video(uploaded_video):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Gradio Interface
|
| 44 |
interface = gr.Interface(
|
|
@@ -46,8 +66,8 @@ interface = gr.Interface(
|
|
| 46 |
inputs=gr.Video(label="Upload Video"),
|
| 47 |
outputs=gr.Video(label="Video with Background Music"),
|
| 48 |
title="Add Background Music to Video",
|
| 49 |
-
description="Upload a video, and this app will add a
|
| 50 |
)
|
| 51 |
|
| 52 |
# Launch Gradio app
|
| 53 |
-
interface.launch(share=True)
|
|
|
|
| 2 |
import random
|
| 3 |
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 |
+
# List all MP3 files in the chunks folder
|
| 11 |
+
if not os.path.exists(chunks_folder):
|
| 12 |
+
return "Error: Chunks folder does not exist."
|
| 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 |
+
return "Error: No audio files found in the chunks folder."
|
| 17 |
+
|
| 18 |
+
# Randomly select one MP3 file
|
| 19 |
+
music_file_path = random.choice(music_files)
|
| 20 |
+
print(f"Selected music file: {music_file_path}")
|
| 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 |
+
print(f"Video duration: {video_duration} seconds")
|
| 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 |
+
print(f"Trimmed music saved at: {trimmed_music_path}")
|
| 33 |
+
|
| 34 |
+
# Combine video and audio
|
| 35 |
+
video_input = ffmpeg.input(video_path)
|
| 36 |
+
audio_input = ffmpeg.input(trimmed_music_path)
|
| 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 |
+
print(f"Output video saved at: {output_path}")
|
| 41 |
+
|
| 42 |
+
return output_path
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print("Error during processing:", e)
|
| 45 |
+
traceback.print_exc()
|
| 46 |
+
return f"Error: {str(e)}"
|
| 47 |
|
| 48 |
# Define Gradio interface
|
| 49 |
def process_video(uploaded_video):
|
| 50 |
+
try:
|
| 51 |
+
print("Video received for processing.")
|
| 52 |
+
video_path = uploaded_video.name
|
| 53 |
+
print(f"Uploaded video path: {video_path}")
|
| 54 |
+
output_video = add_music_to_video(video_path, chunks_folder="/content/chunks")
|
| 55 |
+
if "Error" in output_video:
|
| 56 |
+
return output_video
|
| 57 |
+
return output_video
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print("Error in process_video:", e)
|
| 60 |
+
traceback.print_exc()
|
| 61 |
+
return f"Error: {str(e)}"
|
| 62 |
|
| 63 |
# Gradio Interface
|
| 64 |
interface = gr.Interface(
|
|
|
|
| 66 |
inputs=gr.Video(label="Upload Video"),
|
| 67 |
outputs=gr.Video(label="Video with Background Music"),
|
| 68 |
title="Add Background Music to Video",
|
| 69 |
+
description="Upload a video, and this app will add a background music track that matches the video duration."
|
| 70 |
)
|
| 71 |
|
| 72 |
# Launch Gradio app
|
| 73 |
+
interface.launch(share=True, debug=True)
|