Created
May 31, 2025 13:46
-
-
Save hkfuertes/7a366f41bf452374e2b8276ef438c2d1 to your computer and use it in GitHub Desktop.
Waveform for Audio File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Verifica que se hayan proporcionado al menos dos parámetros | |
if [ "$#" -lt 2 ]; then | |
echo "Uso: $0 <archivo_audio> <archivo_salida> [titulo] [artista]" | |
exit 1 | |
fi | |
AUDIO_FILE=$1 | |
OUTPUT_FILE=$2 | |
TITLE="" | |
ARTIST="" | |
# Asigna título y artista si se proporcionan | |
if [ "$#" -ge 3 ]; then | |
TITLE=$3 | |
fi | |
if [ "$#" -ge 4 ]; then | |
ARTIST=$4 | |
fi | |
# Obtén la duración del audio en segundos | |
DURATION=$(ffprobe -i "$AUDIO_FILE" -show_entries format=duration -v quiet -of csv="p=0") | |
# Define las dimensiones del video | |
WIDTH=1920 | |
HEIGHT=1080 | |
# Altura de la waveform | |
WAVEFORM_HEIGHT=900 | |
CURSOR_HEIGHT=400 | |
# Genera la imagen de la waveform (blanca sobre fondo negro) | |
ffmpeg -i "$AUDIO_FILE" \ | |
-filter_complex "[0:a]aformat=channel_layouts=mono,showwavespic=s=${WIDTH}x${WAVEFORM_HEIGHT}:colors=0xffffff:split_channels=0[wave]" \ | |
-map "[wave]" -frames:v 1 -y "./waveform.png" | |
# Construye el filter_complex base | |
# Fondo blanco, waveform con fondo negro hecho transparente, cursor rojo | |
FILTER="[1:v]colorkey=0x000000:0.1:0.1[wave_transp]; \ | |
[0:v][wave_transp]overlay=x=0:y=(main_h-overlay_h)/2[bg_with_wave]; \ | |
[bg_with_wave][2:v]overlay=x='t/${DURATION}*${WIDTH}':y=(main_h-overlay_h)/2[v]" | |
FILTER="$FILTER; [v]drawtext=text='$TITLE':fontcolor=white:fontsize=30:x=10:y=10[v]" | |
FILTER="$FILTER; [v]drawtext=text='$ARTIST':fontcolor=gray:fontsize=24:x=10:y=50[v]" | |
# Añade drawtext para el timestamp justo debajo del artista | |
FILTER="$FILTER; [v]drawtext=text='%{pts\\:hms}':fontcolor=white:fontsize=24:x=10:y=90[v]" | |
# Genera el video | |
ffmpeg -y \ | |
-f lavfi -i color=c=black:s=${WIDTH}x${HEIGHT}:d=$DURATION \ | |
-i "./waveform.png" \ | |
-f lavfi -i color=c=red:s=3x${CURSOR_HEIGHT}:d=$DURATION \ | |
-i "$AUDIO_FILE" \ | |
-filter_complex "$FILTER" \ | |
-map "[v]" -map 3:a \ | |
-c:v libx264 -c:a aac -strict experimental -shortest "$OUTPUT_FILE" | |
echo "Video generado: $OUTPUT_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment