Ahmadkhan12 commited on
Commit
51ed60c
·
verified ·
1 Parent(s): 013f2af

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -0
app.py ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from random import choice
3
+ from moviepy.editor import VideoFileClip, AudioFileClip
4
+ import tempfile
5
+ import gradio as gr
6
+
7
+ # Updated music directory path (relative to the Hugging Face repo)
8
+ MUSIC_DIR = "chunks" # Path to the folder where your music files are stored on Hugging Face
9
+ DEFAULT_MUSIC = "default_music.mp3" # Optional: path to default music if you want a fallback
10
+
11
+ def load_music_files():
12
+ """
13
+ Load all music files from the directory. Use default if no files found.
14
+ """
15
+ try:
16
+ # Ensure directory exists and is readable
17
+ if not os.path.exists(MUSIC_DIR):
18
+ raise FileNotFoundError(f"Music directory not found: {MUSIC_DIR}")
19
+
20
+ music_files = [os.path.join(MUSIC_DIR, f) for f in os.listdir(MUSIC_DIR) if f.endswith(".mp3")]
21
+ if not music_files:
22
+ print("No music files found! Using default music.")
23
+ if not os.path.exists(DEFAULT_MUSIC):
24
+ raise FileNotFoundError("Default music file is missing!")
25
+ music_files.append(DEFAULT_MUSIC)
26
+ print(f"Loaded {len(music_files)} music files.")
27
+ return music_files
28
+ except Exception as e:
29
+ raise FileNotFoundError(f"Error loading music files: {e}")
30
+
31
+ def generate_music(scene_analysis):
32
+ """
33
+ Select a random music file from the available files.
34
+ """
35
+ music_files = load_music_files()
36
+ selected_music = choice(music_files) # Pick a random music file
37
+ print(f"Selected music: {selected_music}")
38
+ return selected_music
39
+
40
+ def analyze_video(video_path):
41
+ """
42
+ Dummy scene analysis function. Replace with an actual analysis implementation.
43
+ """
44
+ print(f"Analyzing video: {video_path}")
45
+ return {"scene_type": "Generic", "detected_objects": []}
46
+
47
+ def process_video(video_path, music_path):
48
+ """
49
+ Combines the video with background music.
50
+ """
51
+ try:
52
+ print(f"Processing video: {video_path} with music: {music_path}")
53
+
54
+ video_clip = VideoFileClip(video_path)
55
+ music_clip = AudioFileClip(music_path)
56
+
57
+ # Trim music to match video duration
58
+ music_clip = music_clip.subclip(0, min(video_clip.duration, music_clip.duration))
59
+
60
+ # Add music to video
61
+ final_video = video_clip.set_audio(music_clip)
62
+
63
+ # Save the processed video
64
+ output_path = tempfile.mktemp(suffix=".mp4")
65
+ final_video.write_videofile(output_path, codec="libx264", audio_codec="aac")
66
+ return output_path
67
+
68
+ except Exception as e:
69
+ print(f"Error in process_video: {e}")
70
+ raise e
71
+
72
+ def main(video_path):
73
+ try:
74
+ analysis = analyze_video(video_path)
75
+ music_path = generate_music(analysis) # Get a fresh music track every time
76
+ output_path = process_video(video_path, music_path)
77
+ return output_path, f"Music file used: {os.path.basename(music_path)}"
78
+ except Exception as e:
79
+ print(f"Error in main: {e}")
80
+ return None, f"Error: {e}"
81
+
82
+ # Gradio Interface
83
+ def gradio_interface(video_file):
84
+ output_path, message = main(video_file)
85
+ if output_path:
86
+ return output_path, message
87
+ else:
88
+ return None, message
89
+
90
+ iface = gr.Interface(
91
+ fn=gradio_interface,
92
+ inputs=gr.Video(label="Upload Video"),
93
+ outputs=[
94
+ gr.Video(label="Processed Video"),
95
+ gr.Textbox(label="Result Info"),
96
+ ],
97
+ title="Music Video Processor",
98
+ description="Upload a video, and it will randomly select one of the uploaded or default music files to combine with the video."
99
+ )
100
+
101
+ # Optional: Download default music if it's not included in your Hugging Face repo
102
+ if not os.path.exists(DEFAULT_MUSIC):
103
+ print("Downloading default music...")
104
+ os.system('wget -O default_music.mp3 "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3"')
105
+
106
+ iface.launch(debug=True)