#!/usr/bin/env bash
# Reel-shaped test of the Koldflux add-audio tool: a 1080x1920 clip, a phone-style clip stored 1920x1080 with a rotation flag, own voice and music files.
# Run from ~/rt/add-music-to-instagram-reels; writes test-results.txt
set -u
export PATH=$HOME/.nvm/versions/node/v24.21.0/bin:$PATH
cd "$(dirname "$0")"; mkdir -p in out work; R=test-results.txt; M=/home/ubuntu/hub-repo/public/hub-media
cp $M/how-to-add-music-to-video/voice-sample.mp3 in/voice.mp3; cp $M/how-to-add-music-to-video/music-20s.mp3 in/music.mp3
{
echo "Test log: add music to an Instagram Reel file. Run $(date +%F) by Koldflux. $(ffmpeg -version | head -1 | cut -c1-40)"
echo "Tools: ffmpeg/ffprobe on Ubuntu; Playwright headless Chromium against https://koldflux.com/tools/video/add-audio (live)."
echo "Inputs: voice.mp3 = Kokoro-82M voice (our script, 6.4 s); music.mp3 = Beethoven WoO 80 recording, Wikimedia Commons, CC0 (20 s); both reused from the how-to-add-music-to-video test. Video is ffmpeg testsrc2, no third-party footage."
} > $R
# inputs
ffmpeg -v error -y -f lavfi -i testsrc2=size=1080x1920:rate=30:duration=20 -i in/voice.mp3 -filter_complex "[1:a]apad=whole_dur=20[a]" -map 0:v -map "[a]" -c:v libx264 -pix_fmt yuv420p -c:a aac -b:a 128k -t 20 in/reel-voice.mp4
ffmpeg -v error -y -f lavfi -i testsrc2=size=1920x1080:rate=30:duration=20 -c:v libx264 -pix_fmt yuv420p -an in/landscape-raw.mp4
ffmpeg -v error -y -display_rotation 90 -i in/landscape-raw.mp4 -c copy in/phone-rot.mp4
echo "## input probe" >> $R
for f in reel-voice phone-rot; do echo "-- $f"; ffprobe -v error -show_entries stream=codec_type,codec_name,width,height,duration:stream_side_data=rotation -of default=nw=1 in/$f.mp4; done >> $R 2>&1
# tool runs
echo "## tool runs" >> $R
node tool.cjs in/reel-voice.mp4 in/music.mp3 mix 30 out/A-mix30.mp4 >> $R 2>&1
node tool.cjs in/reel-voice.mp4 in/music.mp3 replace 100 out/B-replace.mp4 >> $R 2>&1
node tool.cjs in/phone-rot.mp4 in/music.mp3 replace 100 out/C-phone.mp4 >> $R 2>&1
node tool.cjs in/phone-rot.mp4 in/voice.mp3 mix 100 out/D-phone-voice.mp4 >> $R 2>&1
# measurements
{
echo "## output probe (stream type, codec, size, rotation flag, duration)"
for f in A-mix30 B-replace C-phone D-phone-voice; do echo "-- $f"; ffprobe -v error -show_entries stream=codec_type,codec_name,width,height,duration:stream_side_data=rotation -of default=nw=1 out/$f.mp4; done
echo "## displayed picture size (first decoded frame, ffmpeg applies the rotation flag)"
for f in in/phone-rot out/C-phone in/reel-voice out/A-mix30; do ffmpeg -v error -y -i $f.mp4 -frames:v 1 work/disp.png; echo "$f $(ffprobe -v error -show_entries stream=width,height -of csv=p=0 work/disp.png)"; done
echo "## picture stream checksum before and after (md5 of the copied video packets)"
for p in "in/phone-rot out/C-phone" "in/phone-rot out/D-phone-voice" "in/reel-voice out/A-mix30" "in/reel-voice out/B-replace"; do set -- $p; echo "$1 $(ffmpeg -v error -i $1.mp4 -map 0:v -c copy -f md5 -) | $2 $(ffmpeg -v error -i $2.mp4 -map 0:v -c copy -f md5 -)"; done
echo "## loudness (ebur128 integrated, LUFS) and level in the voice window 1.8-6.0 s and the music-only window 10-14 s (volumedetect mean_volume, dB)"
lufs() { ffmpeg -nostats -i "$1" -map 0:a -af ebur128 -f null - 2>&1 | awk '/I:/ {v=$2} END {print v}'; }
win() { ffmpeg -nostats -ss $2 -t $3 -i "$1" -map 0:a -af volumedetect -f null - 2>&1 | awk -F': ' '/mean_volume/ {print $2}'; }
for f in in/voice.mp3 in/music.mp3 out/A-mix30.mp4 out/B-replace.mp4 out/D-phone-voice.mp4 in/reel-voice.mp4; do echo "$f integrated $(lufs $f) LUFS | voice window $(win $f 1.8 4.2) | music window $(win $f 10 4)"; done
echo "## duration of results"
for f in in/reel-voice out/A-mix30 out/B-replace in/phone-rot out/C-phone out/D-phone-voice; do echo "$f $(ffprobe -v error -show_entries format=duration -of csv=p=0 $f.mp4)"; done
} >> $R 2>&1
echo done
bash levels.sh >> $R 2>&1
bash stereo.sh >> $R 2>&1
bash commands.sh > commands.log 2>&1
echo "all parts done"
