#!/bin/bash
set -u
F=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
probe(){ ffprobe -v error -show_entries stream=codec_name,width,height,r_frame_rate,channels:format=duration,size -of default=nw=1 "$1" | tr "\n" " "; echo; }
run(){ echo "published: $*"; local t0=$(date +%s.%N); eval "$* -y -hide_banner -loglevel error"; local rc=$?; echo "exit=$rc time=$(echo "$(date +%s.%N)-$t0" | bc | cut -c1-5)s"; }
printf "File  Edit  View  Run  Terminal  Help" > t1.txt; printf "const total = items.reduce((sum, item) => sum + item.price, 0);" > t2.txt; printf "function checkout(cart) {" > t3.txt; printf "Sidebar item" > t4.txt
DT="drawbox=x=0:y=0:w=iw:h=40:color=0x313244:t=fill,drawtext=fontfile=$F:textfile=t1.txt:x=16:y=12:fontsize=14:fontcolor=white,drawtext=fontfile=$F:textfile=t2.txt:x=(w-tw)/2:y=h/2:fontsize=14:fontcolor=0xa6e3a1,drawtext=fontfile=$F:textfile=t3.txt:x=(w-tw)/2-20:y=h/2-30:fontsize=14:fontcolor=0x89b4fa,drawtext=fontfile=$F:textfile=t4.txt:x=24:y=200:fontsize=14:fontcolor=0xcdd6f4"
echo "# screen recording to vertical: commands.log (2026-11-21, ffmpeg 6.1.1)"
ffmpeg -y -hide_banner -loglevel error -f lavfi -i "color=c=0x1e1e2e:s=1920x1080:r=30:d=12" -f lavfi -i "sine=f=220:d=12" -vf "$DT" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest screen-1080p.mp4
ffmpeg -y -hide_banner -loglevel error -f lavfi -i "color=c=0x1e1e2e:s=2560x1440:r=30:d=6" -vf "$DT" -c:v libx264 -crf 20 -pix_fmt yuv420p screen-1440p-noaudio.mp4
echo "test file: screen-1080p.mp4 (drawn in code: dark editor mock with 14 px text, 12 s, tone)"; probe screen-1080p.mp4
echo "edge file: screen-1440p-noaudio.mp4 (2560x1440, no audio track, 6 s)"; probe screen-1440p-noaudio.mp4
echo; echo "## 1. crop a 9:16 window from the middle and scale it to 1080x1920"
run "ffmpeg -i screen-1080p.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:a copy crop-vertical.mp4"; probe crop-vertical.mp4
echo "edge: 2560x1440 input without audio, same command"
run "ffmpeg -i screen-1440p-noaudio.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:a copy edge-crop.mp4"; probe edge-crop.mp4
echo; echo "## 1b. same window, moved: left edge at x=0 (the sidebar)"
run "ffmpeg -i screen-1080p.mp4 -vf \"crop=ih*9/16:ih:0:0,scale=1080:1920\" -c:a copy crop-left.mp4"; probe crop-left.mp4
echo "edge: 2560x1440 input without audio, same command"
run "ffmpeg -i screen-1440p-noaudio.mp4 -vf \"crop=ih*9/16:ih:0:0,scale=1080:1920\" -c:a copy edge-crop-left.mp4"; probe edge-crop-left.mp4
echo; echo "## 2. keep the whole screen: fit the width, a blurred copy fills the rest"
run "ffmpeg -i screen-1080p.mp4 -filter_complex \"[0:v]split[a][b];[a]scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,boxblur=20[bg];[b]scale=1080:-2[fg];[bg][fg]overlay=0:(H-h)/2\" -c:a copy fit-vertical.mp4"; probe fit-vertical.mp4
echo "edge: 2560x1440 input without audio, same command"
run "ffmpeg -i screen-1440p-noaudio.mp4 -filter_complex \"[0:v]split[a][b];[a]scale=1080:1920:force_original_aspect_ratio=increase,crop=1080:1920,boxblur=20[bg];[b]scale=1080:-2[fg];[bg][fg]overlay=0:(H-h)/2\" -c:a copy edge-fit.mp4"; probe edge-fit.mp4
ffmpeg -y -hide_banner -loglevel error -ss 2 -i crop-vertical.mp4 -frames:v 1 crop-frame.png; ffmpeg -y -hide_banner -loglevel error -ss 2 -i fit-vertical.mp4 -frames:v 1 fit-frame.png; ffmpeg -y -hide_banner -loglevel error -ss 2 -i screen-1080p.mp4 -frames:v 1 src-frame.png
echo; echo "## text size arithmetic (python)"
python3 - <<PY
for name,w,h in (("1920x1080",1920,1080),("2560x1440",2560,1440)):
    cw=h*9/16; z=1920/h; fit=1080/w
    print(f"{name}: 9:16 window {cw:.1f} px wide = {cw/w*100:.1f}% of the width; zoom x{z:.3f}; 14 px text -> {14*z:.1f} px (crop) vs {14*fit:.1f} px (fit width); fitted picture is {h*fit:.1f} px tall = {h*fit/1920*100:.1f}% of 1920")
print(f"phone: a 1080 px wide video shown 390 pt wide: 24.9 px -> {24.9*390/1080:.1f} pt, 18.7 px -> {18.7*390/1080:.1f} pt, 7.9 px -> {7.9*390/1080:.1f} pt")
PY
