Spaces:
Sleeping
Sleeping
File size: 3,601 Bytes
c7810b4 f92b394 dabc55d f92b394 a065125 b88d490 a065125 b88d490 e4b8ade b88d490 f92b394 ba76d39 dabc55d e4b8ade b88d490 dabc55d e4b8ade dabc55d b88d490 e4b8ade dabc55d b88d490 e4b8ade dabc55d b88d490 e4b8ade dabc55d e4b8ade dabc55d e4b8ade dabc55d e4b8ade b88d490 dabc55d b88d490 f92b394 dabc55d b88d490 e4b8ade b88d490 ba76d39 dabc55d a065125 b88d490 a065125 dabc55d b88d490 dabc55d a065125 51ed60c a065125 e4b8ade a065125 e4b8ade 51ed60c f92b394 e4b8ade |
1 2 3 4 5 6 7 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
import os
import random
import ffmpeg
import gradio as gr
import traceback
# Debug log list
debug_logs = []
def log_message(message):
"""Log messages for debugging."""
debug_logs.append(message)
print(message)
# Function to add music to video
def add_music_to_video(video_path, chunks_folder="./chunks", output_path="output_with_music.mp4"):
try:
log_message("Starting add_music_to_video process...")
# List all MP3 files in the chunks folder
music_files = [os.path.join(chunks_folder, f) for f in os.listdir(chunks_folder) if f.endswith('.mp3')]
if not music_files:
error_message = "Error: No audio files found in the chunks folder."
log_message(error_message)
return error_message
# Randomly select one MP3 file
music_file_path = random.choice(music_files)
log_message(f"Selected music file: {music_file_path}")
# Get the duration of the video
video_info = ffmpeg.probe(video_path)
video_duration = float(video_info['streams'][0]['duration'])
log_message(f"Video duration: {video_duration} seconds")
# Trim the music file to match the video duration
trimmed_music_path = "trimmed_music.mp3"
ffmpeg.input(music_file_path).output(
trimmed_music_path, ss=0, t=video_duration
).run(overwrite_output=True)
log_message("Music trimmed successfully.")
# Combine video and audio
video_input = ffmpeg.input(video_path)
audio_input = ffmpeg.input(trimmed_music_path)
ffmpeg.concat(video_input, audio_input, v=1, a=1).output(
output_path, vcodec="libx264", acodec="aac", strict="experimental"
).run(overwrite_output=True)
log_message(f"Video with music saved to: {output_path}")
return output_path
except Exception as e:
error_message = f"Error in add_music_to_video: {str(e)}"
log_message(error_message)
traceback.print_exc()
return error_message
# Define Gradio interface
def process_video(uploaded_video):
try:
log_message("Video received for processing.")
# Ensure the correct handling of uploaded_video as a string or file object
if isinstance(uploaded_video, str):
video_path = uploaded_video # If it's already a string path
else:
video_path = uploaded_video.name # File-like object with a .name attribute
log_message(f"Uploaded video path: {video_path}")
output_video = add_music_to_video(video_path, chunks_folder="./chunks")
if "Error" in output_video:
return None, "\n".join(debug_logs)
return output_video, "\n".join(debug_logs)
except Exception as e:
error_message = f"Error in process_video: {str(e)}"
log_message(error_message)
traceback.print_exc()
return None, "\n".join(debug_logs)
# Gradio Interface with more nature and music-themed emojis
interface = gr.Interface(
fn=process_video,
inputs=gr.Video(label="πΏ Upload Your Video π±"),
outputs=[
gr.Video(label="πΆ Your Video with Nature's Vibes π³π§"),
gr.Textbox(label="π Debug Logs π")
],
title="Create Nature Vibes πΏππΆ",
description="π³π» Upload a video, and this app will add a 10-second nature-inspired background music track πΆ that matches the video duration πΏπ. Let the natural vibes flow! π§"
)
# Launch Gradio app
interface.launch(share=True)
|