File size: 5,597 Bytes
1d6e11c 27da968 b4b73cd 9e9aafb b4b73cd 9e9aafb 0f8a88a 0b9f87b 7aa02d3 0f8a88a 27da968 4942f7f 27da968 921c510 0b3f50e 517ea30 921c510 0b3f50e 517ea30 921c510 0b3f50e 517ea30 0b9f87b 9e9aafb 1d6e11c df83468 0f8a88a bc43b88 0b9f87b 1d6e11c 0b9f87b 921c510 0b3f50e 1d6e11c 921c510 1133a56 0b9f87b 3c24bb1 0b9f87b 3c24bb1 921c510 0b3f50e 921c510 3c24bb1 0b3f50e 4942f7f 0b3f50e 0b9f87b df83468 0b3f50e 921c510 b4b73cd 4942f7f 0b9f87b df83468 0b9f87b 921c510 0b9f87b 0b3f50e 0b9f87b 0b3f50e 3c24bb1 4942f7f 921c510 0f8a88a 921c510 0f8a88a 27da968 921c510 27da968 0f8a88a 3c24bb1 4942f7f 921c510 df83468 921c510 0b9f87b 921c510 27da968 921c510 0f8a88a 3c24bb1 0b9f87b | 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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | 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)
|