#!/usr/bin/env bash
# Runs every shell command published in the guide as written, on the test file and on one edge input; writes the log format the quality gate reads.
# Needs the files made by test-run.sh (in/ and out/). Names in the guide: original.mp4, result.mp4, mono.mp4, stereo.mp4, music.mp3.
here="$(cd "$(dirname "$0")" && pwd)"
rm -rf "$here/cmd" && mkdir -p "$here/cmd" && cd "$here/cmd"
cp ../in/phone-rot.mp4 original.mp4          # phone-style clip: stored 1920x1080, rotation flag 90, no sound
cp ../out/C-phone.mp4 result.mp4             # the same clip after the Koldflux add-audio tool (music, replace mode)
cp ../in/reel-voice.mp4 mono.mp4             # 1080x1920 clip, mono sound
cp ../in/reel-voice-stereo.mp4 stereo.mp4    # same clip, stereo sound
cp ../in/music.mp3 music.mp3

run() { # $1 = published command, $2 = label; the command is run with bash exactly as written
  echo "published: $1"
  echo "file: $2"
  bash -c "set -o pipefail; $1" 2>&1
  echo "exit=$?"
  echo
}

MIX='ffmpeg -v error -i mono.mp4 -i music.mp3 -filter_complex "[0:a]aformat=channel_layouts=stereo,aresample=44100[o];[1:a]volume=0.3[m];[o][m]amix=inputs=2:duration=first:normalize=0[a]" -map 0:v -map "[a]" -c:v copy -c:a aac -b:a 192k -shortest mixed.mp4'

echo "== test file: the phone-style clip and its result =="
run "ffprobe -v error -show_entries stream=codec_type,width,height:stream_side_data=rotation -of default=nw=1 result.mp4" "result.mp4 (stored 1920x1080, rotation 90)"
run "ffmpeg -v error -i original.mp4 -map 0:v -c copy -f md5 -" "original.mp4 picture checksum"
run "ffmpeg -v error -i result.mp4 -map 0:v -c copy -f md5 -" "result.mp4 picture checksum (same as the original)"
run "ffprobe -v error -select_streams a:0 -show_entries stream=channels,sample_rate -of csv=p=0 mono.mp4" "mono.mp4 (sample rate, channels)"
rm -f mixed.mp4
run "$MIX" "mono.mp4 + music at 30%, forced to stereo 44.1 kHz"
run "ffprobe -v error -select_streams a:0 -show_entries stream=channels,sample_rate -of csv=p=0 mixed.mp4" "mixed.mp4 (sample rate, channels)"
run "ffmpeg -nostats -ss 10 -t 4 -i mixed.mp4 -map 0:a -af volumedetect -f null - 2>&1 | grep mean_volume" "mixed.mp4, music-only window 10-14 s (music alone is -26.4 dB; at 30% about -36.9 dB)"

echo "== edge: inputs the commands were not written for =="
echo "edge: a clip with no rotation flag prints no rotation line"
run "ffprobe -v error -show_entries stream=codec_type,width,height:stream_side_data=rotation -of default=nw=1 stereo.mp4" "stereo.mp4 (1080x1920, no flag)"
echo "edge: the mix command on a clip with no sound at all stops with an error, because there is no 0:a to mix"
rm -f mixed.mp4
run "${MIX/mono.mp4/original.mp4} 2>&1 | tail -1" "original.mp4 has no audio stream"
echo "edge: for a silent clip, map the music alone instead"
run "ffmpeg -v error -i original.mp4 -i music.mp3 -map 0:v -map 1:a -c:v copy -c:a aac -b:a 192k -shortest music-only.mp4" "original.mp4 + music.mp3"
run "ffprobe -v error -show_entries stream=codec_type,channels,sample_rate:stream_side_data=rotation -of default=nw=1 music-only.mp4" "music-only.mp4 keeps the rotation flag"
echo "edge: channels of a stereo clip"
run "ffprobe -v error -select_streams a:0 -show_entries stream=channels,sample_rate -of csv=p=0 stereo.mp4" "stereo.mp4"
echo "edge: music shorter than the clip. -shortest ends the file at the shorter input, so the video is cut (8 s music, 20 s clip)"
ffmpeg -v error -y -i music.mp3 -t 8 -c copy music8.mp3
run "ffmpeg -v error -i original.mp4 -i music8.mp3 -map 0:v -map 1:a -c:v copy -c:a aac -b:a 192k -shortest short-cut.mp4" "original.mp4 + music8.mp3 (8 s)"
run "ffprobe -v error -show_entries format=duration -of default=nw=1 short-cut.mp4" "short-cut.mp4, 9.9 s, not 20 s: the cut falls at a video keyframe after the 8 s of music"
echo "edge: loop the short music first with -stream_loop -1 on the music input, then -shortest ends at the video"
run "ffmpeg -v error -i original.mp4 -stream_loop -1 -i music8.mp3 -map 0:v -map 1:a -c:v copy -c:a aac -b:a 192k -shortest short-looped.mp4" "original.mp4 + music8.mp3 looped"
run "ffprobe -v error -show_entries format=duration -of default=nw=1 short-looped.mp4" "short-looped.mp4, expect about 20 s"
