Spaces:
Sleeping

vibesync / app.py
Shafiq786's picture
Update app.py
921c510 verified
Raw History Blame Contribute Delete
5.6 kB
import os, numpy as np, librosa, cv2, threading
from flask import Flask, render_template, request, send_file, jsonify
try:
from moviepy.editor import VideoClip, AudioFileClip, ImageClip, CompositeVideoClip
except ImportError:
from moviepy.video.VideoClip import VideoClip
from moviepy.audio.io.AudioFileClip import AudioFileClip
from moviepy.video.VideoClip import ImageClip
from moviepy.video.compositing.CompositeVideoClip import CompositeVideoClip
app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 100 * 1024 * 1024
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(BASE_DIR, 'static', 'uploads')
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
progress_status = 0
is_processing = False
# Pyare Hearts banane ka logic
def draw_heart(img, center, size, color):
cx, cy = center
# Heart shape points
pts = np.array([
[cx, cy + size // 2], [cx - size, cy - size // 2],
[cx - size // 2, cy - size], [cx, cy - size // 2],
[cx + size // 2, cy - size], [cx + size, cy - size // 2]
], np.int32)
cv2.fillPoly(img, [pts], color)
# Hearts ki Initial Position (Left side se shuru honge)
hearts = [{'x': np.random.randint(-500, 0), 'y': np.random.randint(100, 1100), 's': np.random.randint(5, 15)} for _ in range(30)]
def get_circular_icon(img_path, size=250):
img = cv2.imread(img_path)
if img is None: return np.zeros((size, size, 3), dtype="uint8")
h_orig, w_orig = img.shape[:2]
min_dim = min(h_orig, w_orig)
img = img[(h_orig-min_dim)//2:(h_orig+min_dim)//2, (w_orig-min_dim)//2:(w_orig+min_dim)//2]
img = cv2.resize(img, (size, size))
# Avee Style White Border
cv2.circle(img, (size//2, size//2), (size//2)-5, (255, 255, 255), 6)
mask = np.zeros((size, size), dtype="uint8")
cv2.circle(mask, (size//2, size//2), size//2, 255, -1)
return cv2.bitwise_and(img, img, mask=mask)
def create_visual_frame(t, y, sr, size, icon1, icon2):
w, h = size
idx = int(t * sr)
amp = np.abs(y[idx]) if idx < len(y) else 0
frame = np.zeros((h, w, 3), dtype="uint8")
# 1. MOVING HEARTS (Left to Right)
for heart in hearts:
# Kick (Bass) aane par hearts fast bhagenge
heart['x'] += heart['s'] + int(amp * 120)
if heart['x'] > w + 50:
heart['x'] = np.random.randint(-200, -50)
heart['y'] = np.random.randint(50, h - 50)
# Heart ka color (Pink/Red Neon)
draw_heart(frame, (int(heart['x']), heart['y']), 12, (180, 0, 255))
# 2. DUAL PULSING ICONS
pulse = int(amp * 130)
i_s = 250
mid_y = h // 2
for x_base, icon in [(int(w*0.18), icon1), (int(w*0.82-i_s), icon2)]:
z_size = i_s + pulse
z_icon = cv2.resize(icon, (z_size, z_size))
y_off, x_off = mid_y - (z_size // 2), x_base - (pulse // 2)
try:
mask = np.zeros((z_size, z_size), dtype="uint8")
cv2.circle(mask, (z_size//2, z_size//2), z_size//2, 255, -1)
roi = frame[y_off:y_off+z_size, x_off:x_off+z_size]
fg = cv2.bitwise_and(z_icon, z_icon, mask=mask)
bg = cv2.bitwise_and(roi, roi, mask=cv2.bitwise_not(mask))
frame[y_off:y_off+z_size, x_off:x_off+z_size] = cv2.add(fg, bg)
except: pass
# 3. GLOW EFFECT (Hearts chamkenge)
glow = cv2.GaussianBlur(frame, (35, 35), 0)
frame = cv2.addWeighted(frame, 1, glow, 2.0, 0)
return frame
def render_video_thread(audio_p, i1_p, i2_p, bg_p, out_p, size_type):
global progress_status, is_processing
try:
y, sr = librosa.load(audio_p)
duration = librosa.get_duration(y=y, sr=sr)
v_size = (720, 1280) if size_type == "9:16" else (1280, 720)
ic1, ic2 = get_circular_icon(i1_p), get_circular_icon(i2_p)
def make_frame(t):
global progress_status
progress_status = int((t / duration) * 100)
return create_visual_frame(t, y, sr, v_size, ic1, ic2)
anim_clip = VideoClip(make_frame, duration=duration)
bg_clip = ImageClip(bg_p).set_duration(duration).resize(height=v_size[1]).set_position("center")
final_video = CompositeVideoClip([bg_clip, anim_clip.set_opacity(0.85)], size=v_size)
final_video.set_audio(AudioFileClip(audio_p)).write_videofile(
out_p, fps=24, codec="libx264", audio_codec="aac",
preset="ultrafast", threads=4, logger=None
)
progress_status = 100
except: progress_status = -1
finally: is_processing = False
@app.route('/')
def index(): return render_template('index.html')
@app.route('/progress')
def get_progress(): return jsonify({"progress": progress_status})
@app.route('/process', methods=['POST'])
def process():
global progress_status, is_processing
f = request.files
a_p = os.path.join(UPLOAD_FOLDER, "s.mp3")
i1_p, i2_p = os.path.join(UPLOAD_FOLDER, "l.png"), os.path.join(UPLOAD_FOLDER, "r.png")
bg_p = os.path.join(UPLOAD_FOLDER, "b.jpg")
out_p = os.path.join(UPLOAD_FOLDER, "Heart_VibeSync.mp4")
f['audio'].save(a_p); f['img1'].save(i1_p); f['img2'].save(i2_p); f['bg'].save(bg_p)
is_processing, progress_status = True, 1
threading.Thread(target=render_video_thread, args=(a_p, i1_p, i2_p, bg_p, out_p, request.form.get('size'))).start()
return jsonify({"status": "ok"})
@app.route('/download')
def download(): return send_file(os.path.join(UPLOAD_FOLDER, "Heart_VibeSync.mp4"), as_attachment=True)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)