Skip to content

Instantly share code, notes, and snippets.

@salvatorecapolupo
Created February 8, 2026 09:03
Show Gist options
  • Select an option

  • Save salvatorecapolupo/d4de567025e90b2acdabb6854a8ddeab to your computer and use it in GitHub Desktop.

Select an option

Save salvatorecapolupo/d4de567025e90b2acdabb6854a8ddeab to your computer and use it in GitHub Desktop.
Creare un video da un file MP3 con sincronizzazione labbra basica.
import numpy as np
import cv2
import librosa
from numba import jit
import os
from moviepy import VideoFileClip, AudioFileClip
# --- CONFIGURAZIONE ---
AUDIO_FILE = 'Carmina Burana.mp3'
TEMP_VIDEO = 'temp_silent.mp4'
FINAL_OUTPUT = 'temp_silent.mp4'
WIDTH, HEIGHT = 800, 450
FPS = 30
if not os.path.exists(AUDIO_FILE):
print(f"ERRORE: Inserisci il file {AUDIO_FILE} nella stessa cartella dello script!")
exit()
# --- ANALISI AUDIO ---
print("Analizzando l'audio...")
y, sr = librosa.load(AUDIO_FILE, sr=None)
duration = len(y) / sr
total_frames = int(duration * FPS)
hop_length = sr // FPS
# Estrazione volume e frequenze
rms = librosa.feature.rms(y=y, hop_length=hop_length)[0]
rms = rms / (np.max(rms) + 1e-6)
stft = np.abs(librosa.stft(y, n_fft=2048, hop_length=hop_length))
vocal_energy = np.mean(stft[10:50, :], axis=0) # Bande basse/medie
vocal_energy = vocal_energy / (np.max(vocal_energy) + 1e-6)
# --- GENERAZIONE VIDEO ---
out = cv2.VideoWriter(TEMP_VIDEO, cv2.VideoWriter_fourcc(*'mp4v'), FPS, (WIDTH, HEIGHT))
print(f"Rendering di {total_frames} frame...")
for i in range(total_frames):
frame = np.zeros((HEIGHT, WIDTH, 3), dtype=np.uint8)
vol = rms[i] if i < len(rms) else 0
voc = vocal_energy[i] if i < len(vocal_energy) else 0
# Labbra stilizzate (due ellissi che si aprono)
center = (WIDTH // 2, HEIGHT // 2)
apertura = int(50 * voc + 10)
larghezza = int(150 + 20 * vol)
# Disegna labbra rosse
color = (50, 50, 200 + int(55 * voc)) # BGR
cv2.ellipse(frame, center, (larghezza, apertura), 0, 0, 360, color, 3)
cv2.line(frame, (center[0]-larghezza, center[1]), (center[0]+larghezza, center[1]), color, 1)
# Effetto particelle psichedeliche
for _ in range(int(vol * 20)):
rand_x = np.random.randint(0, WIDTH)
rand_y = np.random.randint(0, HEIGHT)
cv2.circle(frame, (rand_x, rand_y), int(5 * vol), (255, 100, 100), -1)
out.write(frame)
out.release()
# --- UNIONE AUDIO/VIDEO ---
print("Montaggio finale...")
video = VideoFileClip(TEMP_VIDEO)
audio = AudioFileClip(AUDIO_FILE)
video.with_audio(audio).write_videofile(FINAL_OUTPUT, codec="libx264")
# Pulizia
video.close()
audio.close()
os.remove(TEMP_VIDEO)
print(f"FATTO! Video creato: {FINAL_OUTPUT}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment