# make-tests.sh for interview-to-vertical-short: the batch script (all guides of the run) followed by the correction passes
#!/bin/bash
# Batch 3 tests, 2026-11-21, ffmpeg 6.1.1 on the hub server. One folder and one commands.log per guide.
# Every "published:" line is the command exactly as the guide prints it; exit code and probe of the result follow.
set -u
ROOT=~/b3-work; mkdir -p $ROOT; cd $ROOT
F=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
Q="-y -hide_banner -loglevel error"
probe(){ ffprobe -v error -show_entries stream=codec_name,pix_fmt,width,height,r_frame_rate,channels,sample_rate,color_transfer:format=duration,size -of default=nw=1 "$1" 2>&1 | tr "\n" " "; echo; }
run(){ echo "published: $*"; local t0=$(date +%s.%N); eval "$* $Q"; local rc=$?; echo "exit=$rc time=$(echo "$(date +%s.%N)-$t0" | bc | cut -c1-5)s"; }
hdr(){ echo "# $1: commands.log (2026-11-21, ffmpeg 6.1.1)"; }
# drawn "person": a head-and-shoulders block at horizontal centre $1 on a WxH frame
person(){ echo "drawbox=x=$1-90:y=ih*0.22:w=180:h=180:color=0xf2c9a0:t=fill,drawbox=x=$1-170:y=ih*0.22+190:w=340:h=ih*0.5:color=$2:t=fill"; }

############ landscape-to-vertical
d=landscape-to-vertical; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=8" -f lavfi -i "sine=f=220:d=8" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest wide.mp4
ffmpeg $Q -f lavfi -i "testsrc2=s=1440x1080:r=30:d=4" -c:v libx264 -crf 20 -pix_fmt yuv420p wide-4x3.mp4
ffmpeg $Q -f lavfi -i "testsrc2=s=2560x1080:r=30:d=4" -c:v libx264 -crf 20 -pix_fmt yuv420p wide-21x9.mp4
echo "test file: wide.mp4 (test pattern, 1920x1080, 8 s, tone)"; probe wide.mp4
echo; echo "## 1. centre crop"
run "ffmpeg -i wide.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:a copy center-crop.mp4"; probe center-crop.mp4
echo "edge: 4:3 source 1440x1080, no audio"; run "ffmpeg -i wide-4x3.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:a copy edge-crop-4x3.mp4"; probe edge-crop-4x3.mp4
echo "edge: 21:9 source 2560x1080, no audio"; run "ffmpeg -i wide-21x9.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:a copy edge-crop-21x9.mp4"; probe edge-crop-21x9.mp4
echo; echo "## 2. whole picture with a blurred fill"
run "ffmpeg -i wide.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 blur-fill.mp4"; probe blur-fill.mp4
echo "edge: 21:9 source"; run "ffmpeg -i wide-21x9.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-blur-21x9.mp4"; probe edge-blur-21x9.mp4
echo; echo "## 3. whole picture with black bars"
run "ffmpeg -i wide.mp4 -vf \"scale=1080:-2,pad=1080:1920:0:(oh-ih)/2\" -c:a copy bars.mp4"; probe bars.mp4
echo "edge: 4:3 source"; run "ffmpeg -i wide-4x3.mp4 -vf \"scale=1080:-2,pad=1080:1920:0:(oh-ih)/2\" -c:a copy edge-bars-4x3.mp4"; probe edge-bars-4x3.mp4
echo; echo "## arithmetic"
python3 - <<PY
for n,w,h in (("16:9 1920x1080",1920,1080),("4:3 1440x1080",1440,1080),("21:9 2560x1080",2560,1080),("1:1 1080x1080",1080,1080)):
    cw=h*9/16; fh=h*1080/w
    print(f"{n}: crop keeps {cw:.1f} px = {cw/w*100:.1f}% of the width, zoom x{1920/h:.3f}; fitted picture {fh:.1f} px tall = {fh/1920*100:.1f}% of the frame, scale x{1080/w:.4f}")
PY
for f in center-crop blur-fill bars; do ffmpeg $Q -ss 2 -i $f.mp4 -frames:v 1 $f.png; done; ffmpeg $Q -ss 2 -i wide.mp4 -frames:v 1 wide.png
} > commands.log 2>&1; cd ..

############ square-to-vertical
d=square-to-vertical; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=1080x1080:r=30:d=6" -f lavfi -i "sine=f=330:d=6" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest square.mp4
ffmpeg $Q -f lavfi -i "testsrc2=s=720x720:r=30:d=4" -c:v libx264 -crf 20 -pix_fmt yuv420p square-720.mp4
echo "test file: square.mp4 (test pattern, 1080x1080, 6 s, tone)"; probe square.mp4
echo; echo "## 1. keep the whole square, centred, bars above and below"
run "ffmpeg -i square.mp4 -vf \"scale=1080:-2,pad=1080:1920:0:(oh-ih)/2\" -c:a copy square-centered.mp4"; probe square-centered.mp4
echo "edge: 720x720 source, no audio"; run "ffmpeg -i square-720.mp4 -vf \"scale=1080:-2,pad=1080:1920:0:(oh-ih)/2\" -c:a copy edge-centered-720.mp4"; probe edge-centered-720.mp4
echo; echo "## 2. keep the whole square, placed 168 px from the top"
run "ffmpeg -i square.mp4 -vf \"scale=1080:-2,pad=1080:1920:0:168\" -c:a copy square-high.mp4"; probe square-high.mp4
echo "edge: 720x720 source"; run "ffmpeg -i square-720.mp4 -vf \"scale=1080:-2,pad=1080:1920:0:168\" -c:a copy edge-high-720.mp4"; probe edge-high-720.mp4
echo; echo "## 3. fill the frame: crop the middle of the square"
run "ffmpeg -i square.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:a copy square-cropped.mp4"; probe square-cropped.mp4
echo "edge: 720x720 source"; run "ffmpeg -i square-720.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:a copy edge-cropped-720.mp4"; probe edge-cropped-720.mp4
echo; echo "## arithmetic (1080x1920 frame, combined safe box for Shorts+Reels+TikTok: top 288, bottom 672 -> rows 288..1248)"
python3 - <<PY
for name,top in (("centred",420),("168 px from the top",168),("at the top margin 288",288)):
    bottom=top+1080; over_top=max(0,288-top); over_bot=max(0,bottom-1248)
    print(f"square {name}: rows {top}..{bottom}; {over_top} px under the top margin, {over_bot} px under the bottom margin; {1080-over_top-over_bot} of 1080 rows clear = {(1080-over_top-over_bot)/1080*100:.1f}%")
print(f"crop: keeps {1080*9/16:.1f} px of 1080 = {9/16*100:.2f}% of the width; zoom x{1920/1080:.3f}")
print(f"720 source upscaled x{1080/720:.2f} before padding")
PY
} > commands.log 2>&1; cd ..

############ talking-head-to-vertical-short
d=talking-head-to-vertical-short; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "color=c=0x2b3a55:s=1920x1080:r=30:d=6" -f lavfi -i "sine=f=200:d=6" -vf "$(person 1250 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest head.mp4
ffmpeg $Q -f lavfi -i "color=c=0x2b3a55:s=1920x1080:r=30:d=4" -vf "$(person 1800 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p head-edge.mp4
echo "test file: head.mp4 (drawn figure: head-and-shoulders block centred at x=1250 of 1920, 6 s, tone)"; probe head.mp4
echo; echo "## 1. centre crop (figure is off centre, so it is cut)"
run "ffmpeg -i head.mp4 -vf \"crop=608:1080,scale=1080:1920\" -c:a copy head-center.mp4"; probe head-center.mp4
echo; echo "## 2. crop placed on the figure: x = figure centre - 304 = 946"
run "ffmpeg -i head.mp4 -vf \"crop=608:1080:946:0,scale=1080:1920\" -c:a copy head-placed.mp4"; probe head-placed.mp4
echo "edge: figure centred at x=1800, so x would be 1496, past the last valid value 1312"
run "ffmpeg -i head-edge.mp4 -vf \"crop=608:1080:1496:0,scale=1080:1920\" -c:a copy edge-placed.mp4"; probe edge-placed.mp4
ffmpeg $Q -i edge-placed.mp4 -frames:v 1 edge-placed.png; ffmpeg $Q -i head-edge.mp4 -vf "crop=608:1080:1312:0,scale=1080:1920" -frames:v 1 edge-1312.png
echo "pixel difference between x=1496 output and x=1312 output, first frame (0 = identical):"; ffmpeg -hide_banner -loglevel error -i edge-placed.png -i edge-1312.png -lavfi "blend=all_mode=difference,blackframe=amount=100:threshold=8" -f null - 2>&1 | grep -c "pblack:100"
echo; echo "## arithmetic"
python3 - <<PY
for c in (1250,960,1800,200):
    x=c-304; cl=min(max(x,0),1312)
    print(f"figure centre {c}: x = {x}; valid range 0..1312; used {cl}; figure sits at {(c-cl)/608*100:.0f}% of the cropped width")
print("eye line: top third of a 1920 px frame is row 640; our figure's head spans 22% to 39% of the height = rows 422 to 742 after scaling")
PY
for f in head-center head-placed; do ffmpeg $Q -i $f.mp4 -frames:v 1 $f.png; done; ffmpeg $Q -i head.mp4 -frames:v 1 head.png
} > commands.log 2>&1; cd ..

############ interview-to-vertical-short
d=interview-to-vertical-short; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "color=c=0x30343f:s=1920x1080:r=30:d=6" -f lavfi -i "sine=f=180:d=6" -vf "$(person 500 0x8a4f7d),$(person 1420 0x3f7cac)" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest two.mp4
ffmpeg $Q -f lavfi -i "color=c=0x30343f:s=1280x720:r=25:d=4" -vf "$(person 330 0x8a4f7d),$(person 950 0x3f7cac)" -c:v libx264 -crf 20 -pix_fmt yuv420p two-720.mp4
echo "test file: two.mp4 (two drawn figures centred at x=500 and x=1420 of 1920, 6 s, tone)"; probe two.mp4
echo; echo "## 1. stack the two halves: left speaker on top, right speaker below"
run "ffmpeg -i two.mp4 -filter_complex \"[0:v]crop=iw/2:iw*4/9:0:(ih-oh)/2,scale=1080:960[top];[0:v]crop=iw/2:iw*4/9:iw/2:(ih-oh)/2,scale=1080:960[bottom];[top][bottom]vstack\" -c:a copy stacked.mp4"; probe stacked.mp4
echo "edge: 1280x720 source at 25 fps, no audio"; run "ffmpeg -i two-720.mp4 -filter_complex \"[0:v]crop=iw/2:iw*4/9:0:(ih-oh)/2,scale=1080:960[top];[0:v]crop=iw/2:iw*4/9:iw/2:(ih-oh)/2,scale=1080:960[bottom];[top][bottom]vstack\" -c:a copy edge-stacked-720.mp4"; probe edge-stacked-720.mp4
echo; echo "## 2. one speaker at a time: crop on the left speaker (x = 500 - 304 = 196)"
run "ffmpeg -i two.mp4 -vf \"crop=608:1080:196:0,scale=1080:1920\" -c:a copy left-speaker.mp4"; probe left-speaker.mp4
echo "edge: 1280x720 source, same numbers (608 px is 84% of a 720 px high frame, and 608x1080 does not fit)"; run "ffmpeg -i two-720.mp4 -vf \"crop=608:1080:196:0,scale=1080:1920\" -c:a copy edge-left-720.mp4"; ls -l edge-left-720.mp4 2>&1 | awk '{print "bytes "$5}'
echo; echo "## arithmetic"
python3 - <<PY
for w,h in ((1920,1080),(1280,720)):
    cw=w/2; ch=w*4/9
    print(f"{w}x{h}: each half is {cw:.0f}x{ch:.1f} px (9:8), taken from rows {(h-ch)/2:.1f} to {(h+ch)/2:.1f}; scaled x{1080/cw:.3f} to 1080x960; {ch/h*100:.1f}% of the height kept")
print(f"single speaker crop on 1920x1080: 608 px = 31.7% of the width, zoom x{1920/1080:.3f}")
PY
for f in stacked left-speaker; do ffmpeg $Q -i $f.mp4 -frames:v 1 $f.png; done; ffmpeg $Q -i two.mp4 -frames:v 1 two.png
} > commands.log 2>&1; cd ..

############ screen-recording-to-vertical-short
d=screen-recording-to-vertical-short; mkdir -p $d; cd $d; {
hdr $d
printf "const total = items.reduce((sum, item) => sum + item.price, 0);" > t.txt
ffmpeg $Q -f lavfi -i "color=c=0x1e1e2e:s=1920x1080:r=30:d=8" -f lavfi -i "sine=f=220:d=8" -vf "drawbox=x=0:y=0:w=iw:h=40:color=0x313244:t=fill,drawtext=fontfile=$F:textfile=t.txt:x=(w-tw)/2:y=h/2:fontsize=16:fontcolor=0xa6e3a1" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest screen.mp4
ffmpeg $Q -f lavfi -i "color=c=0x4a3f35:s=1280x720:r=30:d=8" -vf "$(person 640 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p cam.mp4
ffmpeg $Q -f lavfi -i "color=c=0x4a3f35:s=1280x720:r=25:d=5" -vf "$(person 640 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p cam-short-25fps.mp4
echo "test files: screen.mp4 (editor mock, 1920x1080, 30 fps, 8 s, tone) and cam.mp4 (drawn figure, 1280x720, 30 fps, 8 s, no audio)"; probe screen.mp4; probe cam.mp4
echo; echo "## 1. screen on top (60% of the frame), webcam below (40%)"
run "ffmpeg -i screen.mp4 -i cam.mp4 -filter_complex \"[0:v]crop=ih*15/16:ih,scale=1080:1152[s];[1:v]crop=ih*45/32:ih,scale=1080:768[c];[s][c]vstack\" -map 0:a? -c:a copy -shortest stacked.mp4"; probe stacked.mp4
echo "edge: webcam clip is shorter (5 s) and 25 fps"; run "ffmpeg -i screen.mp4 -i cam-short-25fps.mp4 -filter_complex \"[0:v]crop=ih*15/16:ih,scale=1080:1152[s];[1:v]crop=ih*45/32:ih,scale=1080:768[c];[s][c]vstack\" -map 0:a? -c:a copy -shortest edge-stacked.mp4"; probe edge-stacked.mp4
echo; echo "## 2. screen only, webcam as a small picture in the corner"
run "ffmpeg -i screen.mp4 -i cam.mp4 -filter_complex \"[0:v]crop=ih*9/16:ih,scale=1080:1920[s];[1:v]crop=ih:ih,scale=300:300[c];[s][c]overlay=W-w-60:H-h-720\" -map 0:a? -c:a copy -shortest corner.mp4"; probe corner.mp4
echo "edge: shorter 25 fps webcam clip"; run "ffmpeg -i screen.mp4 -i cam-short-25fps.mp4 -filter_complex \"[0:v]crop=ih*9/16:ih,scale=1080:1920[s];[1:v]crop=ih:ih,scale=300:300[c];[s][c]overlay=W-w-60:H-h-720\" -map 0:a? -c:a copy -shortest edge-corner.mp4"; probe edge-corner.mp4
echo; echo "## arithmetic"
python3 - <<PY
print(f"stack: screen window {1080*15/16:.1f}x1080 px = {1080*15/16/1920*100:.1f}% of a 1920 px wide screen, scaled x{1152/1080:.3f}: 16 px text -> {16*1152/1080:.1f} px")
print(f"full-height 9:16 crop: 607.5 px = 31.6% of the width, scaled x{1920/1080:.3f}: 16 px text -> {16*1920/1080:.1f} px")
print(f"webcam strip 1080x768 = {768/1920*100:.0f}% of the frame; it starts at row 1152 and the bottom margin of the combined safe box starts at row 1248, so rows 1248..1920 of the webcam ({(1920-1248)/768*100:.1f}% of the strip) sit under app interface")
print(f"corner picture 300x300 at x=720..1020, y=900..1200: inside the combined safe box (x 120..888)? right edge 1020 > 888, so {1020-888} px of it sit under the right margin")
PY
for f in stacked corner; do ffmpeg $Q -ss 1 -i $f.mp4 -frames:v 1 $f.png; done
} > commands.log 2>&1; cd ..

############ podcast-long-video-to-highlight-clip
d=podcast-long-video-to-highlight-clip; mkdir -p $d; cd $d; {
hdr $d
S1="Most shows lose listeners in the first minute. The intro is too long."
S2="Here is what we changed. We cut the intro to eight seconds and opened with the guest's best line."
S3="Completion went up, and nobody wrote in to ask where the theme music went."
i=0; for s in "$S1" "$S2" "$S3"; do i=$((i+1)); printf "%s" "$s" > s$i.txt; ffmpeg $Q -f lavfi -i "flite=textfile=s$i.txt:voice=slt" s$i.wav; done
ffmpeg $Q -f lavfi -t 1.2 -i anullsrc=r=16000:cl=mono -i s1.wav -f lavfi -t 1.5 -i anullsrc=r=16000:cl=mono -i s2.wav -f lavfi -t 0.9 -i anullsrc=r=16000:cl=mono -i s3.wav -f lavfi -t 1.1 -i anullsrc=r=16000:cl=mono -filter_complex "[0][1][2][3][4][5][6]concat=n=7:v=0:a=1,aresample=48000[a]" -map "[a]" voice.wav
D=$(ffprobe -v error -show_entries format=duration -of csv=p=0 voice.wav)
ffmpeg $Q -f lavfi -i "color=c=0x202830:s=1280x720:r=25:d=$D" -i voice.wav -c:v libx264 -crf 24 -pix_fmt yuv420p -c:a aac -b:a 128k -shortest episode.mp4
ffmpeg $Q -i voice.wav -f lavfi -i "anoisesrc=d=$D:c=pink:a=0.05:r=48000" -filter_complex "amix=inputs=2:duration=first:normalize=0" noisy.wav
ffmpeg $Q -f lavfi -i "color=c=0x202830:s=1280x720:r=25:d=$D" -i noisy.wav -c:v libx264 -crf 24 -pix_fmt yuv420p -c:a aac -b:a 128k -shortest episode-noisy.mp4
echo "test file: episode.mp4 (synthetic voice reading three of our own sentences with pauses of 1.2, 1.5, 0.9 and 1.1 s; plain picture)"; probe episode.mp4
echo "sentence lengths (s): $(for n in 1 2 3; do ffprobe -v error -show_entries format=duration -of csv=p=0 s$n.wav; done | tr '\n' ' ')"
echo; echo "## 1. list the pauses"
echo "published: ffmpeg -i episode.mp4 -af silencedetect=noise=-35dB:d=0.4 -f null -"
ffmpeg -hide_banner -nostats -i episode.mp4 -af silencedetect=noise=-35dB:d=0.4 -f null - 2>&1 | grep -E "silence_(start|end)" | sed 's/^\[[^]]*\] //' ; echo "exit=${PIPESTATUS[0]}"
echo "edge: the same speech with pink noise mixed in (episode-noisy.mp4)"
echo "published: ffmpeg -i episode-noisy.mp4 -af silencedetect=noise=-35dB:d=0.4 -f null -"
n=$(ffmpeg -hide_banner -nostats -i episode-noisy.mp4 -af silencedetect=noise=-35dB:d=0.4 -f null - 2>&1 | grep -c "silence_start"); echo "exit=0 pauses found: $n"
echo "noise floor of episode-noisy.mp4 in the first second (before speech):"; ffmpeg -hide_banner -nostats -t 1 -i episode-noisy.mp4 -af volumedetect -f null - 2>&1 | grep -E "mean_volume|max_volume" | sed 's/^\[[^]]*\] //'
echo "edge, raised threshold: silencedetect=noise=-15dB:d=0.4 on the noisy file finds: $(ffmpeg -hide_banner -nostats -i episode-noisy.mp4 -af silencedetect=noise=-15dB:d=0.4 -f null - 2>&1 | grep -c silence_start) pauses"
echo; echo "## 2. cut the second sentence, from the middle of the pause before it to the middle of the pause after it"
ST=$(ffmpeg -hide_banner -nostats -i episode.mp4 -af silencedetect=noise=-35dB:d=0.4 -f null - 2>&1 | grep -E "silence_(start|end)" | sed 's/^\[[^]]*\] //' | python3 -c "
import sys,re
t=sys.stdin.read(); s=[float(x) for x in re.findall(r'silence_start: ([0-9.]+)',t)]; e=[float(x) for x in re.findall(r'silence_end: ([0-9.]+)',t)]
print(round((s[1]+e[1])/2,2), round((s[2]+e[2])/2,2))")
A=${ST% *}; B=${ST#* }; echo "pause midpoints: start $A s, end $B s"
run "ffmpeg -ss $A -to $B -i episode.mp4 -c:v libx264 -crf 20 -c:a aac -b:a 160k highlight.mp4"; probe highlight.mp4
echo "check: silence at the head and tail of highlight.mp4:"; ffmpeg -hide_banner -nostats -i highlight.mp4 -af silencedetect=noise=-35dB:d=0.2 -f null - 2>&1 | grep -E "silence_(start|end)" | sed 's/^\[[^]]*\] //'
echo "edge: a cut 0.5 s late (starts inside the sentence): first 0.2 s mean volume:"; ffmpeg $Q -ss $(python3 -c "print($A+1.3)") -to $B -i episode.mp4 -c:v libx264 -crf 20 -c:a aac late.mp4; ffmpeg -hide_banner -nostats -t 0.2 -i late.mp4 -af volumedetect -f null - 2>&1 | grep mean_volume | sed 's/^\[[^]]*\] //'; echo "same measure on highlight.mp4:"; ffmpeg -hide_banner -nostats -t 0.2 -i highlight.mp4 -af volumedetect -f null - 2>&1 | grep mean_volume | sed 's/^\[[^]]*\] //'
} > commands.log 2>&1; cd ..

############ long-youtube-video-to-batch-pack
d=long-youtube-video-to-batch-pack; mkdir -p $d; cd $d; {
hdr $d
printf "%s" "%{pts:hms}" > clock.txt
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=120" -f lavfi -i "sine=f=200:d=120" -vf "drawtext=fontfile=$F:textfile=clock.txt:x=(w-tw)/2:y=h/2:fontsize=64:fontcolor=white:box=1:boxcolor=black" -c:v libx264 -preset veryfast -crf 26 -g 60 -pix_fmt yuv420p -c:a aac -shortest long.mp4
printf "hook,00:00:05,00:00:20\nmain-point,00:00:40,00:01:05\nending,00:01:50,00:02:30\n" > cuts.csv
echo "test file: long.mp4 (test pattern with a burned clock, 1920x1080, 120 s, tone)"; probe long.mp4
echo "cuts.csv:"; cat cuts.csv
echo; echo "## 1. one clip per row of cuts.csv, cropped to 9:16"
run "while IFS=, read -r name start end; do ffmpeg -nostdin -ss \"\$start\" -to \"\$end\" -i long.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:v libx264 -crf 20 -c:a aac \"\$name.mp4\"; done < cuts.csv"
for n in hook main-point ending; do printf "%s: " $n; probe $n.mp4; done
echo "note: the third row asks for 00:01:50 to 00:02:30 but the file ends at 00:02:00, so that clip is 10 s"
echo "edge: the same loop WITHOUT -nostdin (not published)"; mkdir -p nostdin; cd nostdin; cp ../cuts.csv ../long.mp4 .
while IFS=, read -r name start end; do ffmpeg -ss "$start" -to "$end" -i long.mp4 -vf "crop=ih*9/16:ih,scale=1080:1920" -c:v libx264 -crf 20 -c:a aac "$name.mp4" -y -hide_banner -loglevel error; done < cuts.csv
echo "files written without -nostdin: $(ls *.mp4 | grep -v long.mp4 | tr '\n' ' ')"; cd ..
echo "edge: a row whose name has a space"; printf "two words,00:00:10,00:00:14\n" > cuts.csv.bak; cp cuts.csv cuts.orig; cp cuts.csv.bak cuts.csv
run "while IFS=, read -r name start end; do ffmpeg -nostdin -ss \"\$start\" -to \"\$end\" -i long.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:v libx264 -crf 20 -c:a aac \"\$name.mp4\"; done < cuts.csv"; ls -1 "two words.mp4"; cp cuts.orig cuts.csv
echo; echo "## 2. the same clips as square files for a feed post"
run "while IFS=, read -r name start end; do ffmpeg -nostdin -ss \"\$start\" -to \"\$end\" -i long.mp4 -vf \"crop=ih:ih,scale=1080:1080\" -c:v libx264 -crf 20 -c:a aac \"\$name-square.mp4\"; done < cuts.csv"
for n in hook main-point ending; do printf "%s-square: " $n; probe $n-square.mp4; done
echo "edge: an empty last line in cuts.csv"; printf "hook,00:00:05,00:00:08\n\n" > cuts.csv
run "while IFS=, read -r name start end; do ffmpeg -nostdin -ss \"\$start\" -to \"\$end\" -i long.mp4 -vf \"crop=ih:ih,scale=1080:1080\" -c:v libx264 -crf 20 -c:a aac \"\$name-square.mp4\"; done < cuts.csv"; ls -la -- "-square.mp4" 2>&1 | tail -1; cp cuts.orig cuts.csv
ffmpeg $Q -i hook.mp4 -frames:v 1 hook.png
} > commands.log 2>&1; cd ..

############ gif-to-video
d=gif-to-video; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=481x271:r=10:d=2" -vf "split[a][b];[a]palettegen[p];[b][p]paletteuse" -loop 0 anim.gif
ffmpeg $Q -f lavfi -i "testsrc2=s=480x270:r=10:d=2" -vf "split[a][b];[a]palettegen[p];[b][p]paletteuse" -loop -1 once.gif
echo "test file: anim.gif (test pattern, 481x271, 10 fps, 2 s, loops forever)"; probe anim.gif; ls -l anim.gif | awk '{print "bytes "$5}'
echo; echo "## 1. GIF to MP4"
run "ffmpeg -i anim.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" anim.mp4"; probe anim.mp4
echo "edge: a GIF set to play once (once.gif, 480x270)"; run "ffmpeg -i once.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" once.mp4"; probe once.mp4
echo "edge: the odd-sized GIF WITHOUT the scale step (not published)"; ffmpeg -i anim.gif -movflags +faststart -pix_fmt yuv420p fail.mp4 -y -hide_banner -loglevel error 2>&1 | head -1; echo "exit=${PIPESTATUS[0]}"
echo; echo "## 2. repeat the GIF five times so the video is long enough"
run "ffmpeg -stream_loop 4 -i anim.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" anim-x5.mp4"; probe anim-x5.mp4
echo "edge: the play-once GIF"; run "ffmpeg -stream_loop 4 -i once.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" once-x5.mp4"; probe once-x5.mp4
echo; echo "## 3. upscale a small GIF to 1080 wide without blur (nearest neighbour)"
run "ffmpeg -stream_loop 4 -i anim.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=1080:-2:flags=neighbor\" anim-1080.mp4"; probe anim-1080.mp4
echo "edge: the play-once GIF"; run "ffmpeg -stream_loop 4 -i once.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=1080:-2:flags=neighbor\" once-1080.mp4"; probe once-1080.mp4
echo; echo "## sizes"; ls -l anim.gif anim.mp4 anim-x5.mp4 anim-1080.mp4 | awk '{print $5" bytes "$9}'
python3 - <<PY
import os
g=os.path.getsize("anim.gif"); m=os.path.getsize("anim.mp4"); print(f"anim.mp4 is {m/g*100:.1f}% of anim.gif ({g} -> {m} bytes)")
PY
} > commands.log 2>&1; cd ..

############ webm-recording-for-social
d=webm-recording-for-social; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=1280x720:r=30:d=8" -f lavfi -i "sine=f=260:d=8" -c:v libvpx-vp9 -b:v 0 -crf 34 -deadline realtime -cpu-used 8 -c:a libopus -b:a 96k -shortest rec.webm
ffmpeg $Q -f lavfi -i "testsrc2=s=1280x720:r=30:d=8" -f lavfi -i "sine=f=260:d=8" -c:v libvpx-vp9 -b:v 0 -crf 34 -deadline realtime -cpu-used 8 -c:a libopus -b:a 96k -shortest -live 1 rec-live.webm
echo "test file: rec.webm (test pattern, VP9 + Opus, 1280x720, 8 s)"; probe rec.webm
echo "edge file: rec-live.webm (same content written as a live stream: no duration in the header, like many browser recordings)"; probe rec-live.webm
echo; echo "## 1. WebM to MP4 (H.264 + AAC)"
run "ffmpeg -i rec.webm -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart rec.mp4"; probe rec.mp4
echo "edge: the live-style file with no duration"; run "ffmpeg -i rec-live.webm -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart rec-live.mp4"; probe rec-live.mp4
echo; echo "## 2. same, forcing a constant 30 frames per second"
run "ffmpeg -i rec.webm -c:v libx264 -crf 20 -pix_fmt yuv420p -r 30 -fps_mode cfr -c:a aac -b:a 160k -movflags +faststart rec-cfr.mp4"; probe rec-cfr.mp4
ffmpeg $Q -f lavfi -i "testsrc2=s=1280x720:r=30:d=6" -vf "setpts='PTS+0.02*sin(N)/TB'" -fps_mode vfr -c:v libvpx-vp9 -b:v 0 -crf 34 -deadline realtime -cpu-used 8 rec-vfr.webm
echo "edge: a file with uneven frame timing (rec-vfr.webm, no audio); distinct frame gaps before:"; ffprobe -v error -select_streams v:0 -show_entries packet=pts_time -of csv=p=0 rec-vfr.webm | python3 -c "
import sys; t=[float(x) for x in sys.stdin.read().split() if x]; d=sorted({round(b-a,3) for a,b in zip(t,t[1:])}); print(len(d),'distinct gaps, from',d[0],'to',d[-1],'s')"
run "ffmpeg -i rec-vfr.webm -c:v libx264 -crf 20 -pix_fmt yuv420p -r 30 -fps_mode cfr -c:a aac -b:a 160k -movflags +faststart rec-vfr-cfr.mp4"; probe rec-vfr-cfr.mp4
echo "distinct frame gaps after:"; ffprobe -v error -select_streams v:0 -show_entries packet=pts_time -of csv=p=0 rec-vfr-cfr.mp4 | python3 -c "
import sys; t=sorted(float(x) for x in sys.stdin.read().split() if x); d=sorted({round(b-a,3) for a,b in zip(t,t[1:])}); print(len(d),'distinct gaps:',d)"
echo; echo "## 3. what a plain copy into MP4 does (not published as a fix)"
ffmpeg -i rec.webm -c copy copy.mp4 -y -hide_banner -loglevel error 2>&1 | head -2; echo "exit=${PIPESTATUS[0]}"; [ -f copy.mp4 ] && probe copy.mp4
echo; ls -l rec.webm rec.mp4 | awk '{print $5" bytes "$9}'
} > commands.log 2>&1; cd ..

############ iphone-video-for-upload
d=iphone-video-for-upload; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=5" -f lavfi -i "sine=f=240:d=5" -c:v libx265 -preset fast -crf 24 -pix_fmt yuv420p10le -color_primaries bt2020 -color_trc arib-std-b67 -colorspace bt2020nc -tag:v hvc1 -x265-params log-level=error -c:a aac -shortest hdr.mov
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=4" -c:v libx265 -preset fast -crf 24 -pix_fmt yuv420p -tag:v hvc1 -x265-params log-level=error sdr-hevc.mov
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=4" -c:v libx264 -crf 20 -pix_fmt yuv420p -metadata:s:v rotate=90 -c:a aac rot.mov 2>/dev/null
echo "test file: hdr.mov (derived by us, not recorded on an iPhone: test pattern encoded as HEVC 10-bit, HLG transfer, BT.2020, in a MOV)"; probe hdr.mov
echo; echo "## 1. HEVC to H.264, colours left as tagged (SDR sources)"
run "ffmpeg -i sdr-hevc.mov -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart sdr.mp4"; probe sdr.mp4
echo "edge: the HLG 10-bit file through the same command"; run "ffmpeg -i hdr.mov -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart hdr-naive.mp4"; probe hdr-naive.mp4
echo; echo "## 2. HDR (HLG) to SDR with tone mapping"
run "ffmpeg -i hdr.mov -vf \"zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=hable,zscale=t=bt709:m=bt709:r=tv,format=yuv420p\" -c:v libx264 -crf 20 -c:a aac -b:a 160k -movflags +faststart hdr-to-sdr.mp4"; probe hdr-to-sdr.mp4
echo "edge: an SDR 8-bit HEVC file through the tone-mapping command"; run "ffmpeg -i sdr-hevc.mov -vf \"zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=hable,zscale=t=bt709:m=bt709:r=tv,format=yuv420p\" -c:v libx264 -crf 20 -c:a aac -b:a 160k -movflags +faststart edge-sdr-tonemapped.mp4"; probe edge-sdr-tonemapped.mp4
echo; echo "## what changed: transfer tag and average brightness (YAVG from signalstats, 8-bit scale) of each output"
for f in hdr-naive.mp4 hdr-to-sdr.mp4 sdr.mp4 edge-sdr-tonemapped.mp4; do printf "%s: transfer=%s yavg=" $f "$(ffprobe -v error -select_streams v:0 -show_entries stream=color_transfer -of csv=p=0 $f)"; ffmpeg -hide_banner -loglevel error -i $f -vf "format=yuv420p,signalstats,metadata=print:key=lavfi.signalstats.YAVG:file=-" -frames:v 1 -f null - | grep -o "YAVG=[0-9.]*"; done
ls -l hdr.mov hdr-to-sdr.mp4 | awk '{print $5" bytes "$9}'
} > commands.log 2>&1; cd ..

############ long-youtube-video-to-vertical-short
d=long-youtube-video-to-vertical-short; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=90" -f lavfi -i "sine=f=200:d=90" -c:v libx264 -preset veryfast -crf 26 -pix_fmt yuv420p -c:a aac -shortest original.mp4
ffmpeg $Q -i original.mp4 -vf scale=1280:720 -c:v libx264 -preset veryfast -crf 26 -c:a copy download-720p.mp4
echo "test files: original.mp4 (test pattern, 1920x1080, 90 s) and download-720p.mp4 (the same scaled to 1280x720, standing in for a lower-resolution download)"; probe original.mp4; probe download-720p.mp4
echo; echo "## 1. cut 45 seconds and crop to 9:16 in one pass"
run "ffmpeg -ss 00:00:30 -to 00:01:15 -i original.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:v libx264 -crf 20 -c:a aac -b:a 160k short.mp4"; probe short.mp4
echo "edge: the 720p copy through the same command"; run "ffmpeg -ss 00:00:30 -to 00:01:15 -i download-720p.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:v libx264 -crf 20 -c:a aac -b:a 160k edge-short-720.mp4"; probe edge-short-720.mp4
echo; echo "## arithmetic"
python3 - <<PY
for n,w,h in (("1080p",1920,1080),("720p",1280,720),("4K",3840,2160)):
    print(f"{n}: 9:16 window {h*9/16:.1f}x{h} px; scale to 1080x1920 is x{1920/h:.3f} ({'upscaled' if h<1920 else 'downscaled'}); the window holds {h*9/16*h/1e6:.2f} MP against {1080*1920/1e6:.2f} MP in the output = {h*9/16*h/(1080*1920)*100:.1f}%")
PY
echo; echo "## sharpness check: SSIM of each short against the 1080p-source short (1.0 = identical)"
ffmpeg -hide_banner -loglevel info -i edge-short-720.mp4 -i short.mp4 -lavfi ssim -f null - 2>&1 | grep -o "All:[0-9.]*"
} > commands.log 2>&1; cd ..

############ podcast-long-video-to-batch-pack
d=podcast-long-video-to-batch-pack; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "sine=f=180:d=300" -f lavfi -i "sine=f=260:d=300" -filter_complex "[0][1]amerge=inputs=2,volume=0.3" -ar 48000 -c:a pcm_s16le episode.wav
ffmpeg $Q -f lavfi -i "sine=f=180:d=60" -ar 44100 -ac 1 -c:a pcm_s16le mono.wav
echo "test file: episode.wav (two tones as a stereo stand-in for a 5 minute episode, 48 kHz 16-bit)"; probe episode.wav
echo; echo "## 1. feed file: MP3, 44.1 kHz stereo, 128 kbps"
run "ffmpeg -i episode.wav -ar 44100 -ac 2 -c:a libmp3lame -b:a 128k episode.mp3"; probe episode.mp3
echo "edge: a mono 44.1 kHz source through the same command"; run "ffmpeg -i mono.wav -ar 44100 -ac 2 -c:a libmp3lame -b:a 128k edge-mono.mp3"; probe edge-mono.mp3
echo; echo "## 2. small audio-only copy for the transcript tool: mono 16 kHz"
run "ffmpeg -i episode.wav -ar 16000 -ac 1 -c:a aac -b:a 48k for-transcript.m4a"; probe for-transcript.m4a
echo "edge: mono source"; run "ffmpeg -i mono.wav -ar 16000 -ac 1 -c:a aac -b:a 48k edge-for-transcript.m4a"; probe edge-for-transcript.m4a
echo; echo "## sizes and arithmetic"
ls -l episode.wav episode.mp3 for-transcript.m4a | awk '{print $5" bytes "$9}'
python3 - <<PY
import os
w=os.path.getsize("episode.wav"); m=os.path.getsize("episode.mp3"); t=os.path.getsize("for-transcript.m4a")
print(f"per minute: WAV {w/5/1e6:.2f} MB, MP3 {m/5/1e6:.2f} MB, transcript copy {t/5/1e6:.2f} MB")
print(f"a 60 minute episode: WAV {w/5*60/1e6:.0f} MB, MP3 {m/5*60/1e6:.1f} MB, transcript copy {t/5*60/1e6:.1f} MB (the browser tools take files up to 100 MB)")
print(f"128 kbps arithmetic: 128000/8*60 = {128000/8*60/1e6:.2f} MB per minute")
PY
} > commands.log 2>&1; cd ..
echo ALL-DONE
# ---- b3fix.sh
#!/bin/bash
# Batch 3 tests, 2026-11-21, ffmpeg 6.1.1 on the hub server. One folder and one commands.log per guide.
# Every "published:" line is the command exactly as the guide prints it; exit code and probe of the result follow.
set -u
ROOT=~/b3-work; mkdir -p $ROOT; cd $ROOT
F=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
Q="-y -hide_banner -loglevel error"
probe(){ ffprobe -v error -show_entries stream=codec_name,pix_fmt,width,height,r_frame_rate,channels,sample_rate,color_transfer:format=duration,size -of default=nw=1 "$1" 2>&1 | tr "\n" " "; echo; }
run(){ echo "published: $*"; local t0=$(date +%s.%N); eval "$* $Q"; local rc=$?; echo "exit=$rc time=$(echo "$(date +%s.%N)-$t0" | bc | cut -c1-5)s"; }
hdr(){ echo "# $1: commands.log (2026-11-21, ffmpeg 6.1.1)"; }
# drawn "person": a head-and-shoulders block at horizontal centre $1 on a WxH frame
person(){ echo "drawbox=x=$1-90:y=ih*0.22:w=180:h=180:color=0xf2c9a0:t=fill,drawbox=x=$1-170:y=ih*0.22+190:w=340:h=ih*0.5:color=$2:t=fill"; }

############ talking-head-to-vertical-short
d=talking-head-to-vertical-short; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "color=c=0x2b3a55:s=1920x1080:r=30:d=6" -f lavfi -i "sine=f=200:d=6" -vf "$(person 1250 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest head.mp4
ffmpeg $Q -f lavfi -i "color=c=0x2b3a55:s=1920x1080:r=30:d=4" -vf "$(person 1800 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p head-edge.mp4
echo "test file: head.mp4 (drawn figure: head-and-shoulders block centred at x=1250 of 1920, 6 s, tone)"; probe head.mp4
echo; echo "## 1. centre crop (figure is off centre, so it is cut)"
run "ffmpeg -i head.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:a copy head-center.mp4"; probe head-center.mp4
echo; echo "## 2. crop placed on the figure: x = figure centre - 304 = 946"
run "ffmpeg -i head.mp4 -vf \"crop=ih*9/16:ih:946:0,scale=1080:1920\" -c:a copy head-placed.mp4"; probe head-placed.mp4
echo "edge: figure centred at x=1800, so x would be 1496, past the last valid value 1312"
run "ffmpeg -i head-edge.mp4 -vf \"crop=ih*9/16:ih:1496:0,scale=1080:1920\" -c:a copy edge-placed.mp4"; probe edge-placed.mp4
ffmpeg $Q -i edge-placed.mp4 -frames:v 1 edge-placed.png; ffmpeg $Q -i head-edge.mp4 -vf "crop=ih*9/16:ih:1312:0,scale=1080:1920" -frames:v 1 edge-1312.png
echo "is the x=1496 output the same picture as an x=1312 crop? PSNR of the two first frames (inf = identical):"; ffmpeg -hide_banner -i edge-placed.png -i edge-1312.png -lavfi psnr -f null - 2>&1 | grep -o "average:[a-z0-9.]*"
ffmpeg $Q -i head-edge.mp4 -vf "crop=ih*9/16:ih:1000:0,scale=1080:1920" -frames:v 1 edge-1000.png; echo "control, x=1000 against x=1312 (must differ):"; ffmpeg -hide_banner -i edge-1000.png -i edge-1312.png -lavfi psnr -f null - 2>&1 | grep -o "average:[a-z0-9.]*"
ffmpeg $Q -f lavfi -i "color=c=0x2b3a55:s=1280x720:r=30:d=4" -vf "$(person 800 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p head-720.mp4
echo "edge: 1280x720 source, figure centred at x=800: x = 800 - 202 = 598 (the window is 405 px wide at this size)"
run "ffmpeg -i head-720.mp4 -vf \"crop=ih*9/16:ih:598:0,scale=1080:1920\" -c:a copy edge-720.mp4"; probe edge-720.mp4
echo; echo "## arithmetic"
python3 - <<PY
print("window width: 1080p 607.5 px (ffmpeg uses 608, half = 304); 720p 405 px (ffmpeg uses 404, half = 202); 4K 1215 px (half = 607)")
for c in (1250,960,1800,200):
    x=c-304; cl=min(max(x,0),1312)
    print(f"figure centre {c}: x = {x}; valid range 0..1312; used {cl}; figure sits at {(c-cl)/608*100:.0f}% of the cropped width")
print("eye line: top third of a 1920 px frame is row 640; our figure's head spans 22% to 39% of the height = rows 422 to 742 after scaling")
PY
for f in head-center head-placed; do ffmpeg $Q -i $f.mp4 -frames:v 1 $f.png; done; ffmpeg $Q -i head.mp4 -frames:v 1 head.png
} > commands.log 2>&1; cd ..

############ interview-to-vertical-short
d=interview-to-vertical-short; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "color=c=0x30343f:s=1920x1080:r=30:d=6" -f lavfi -i "sine=f=180:d=6" -vf "$(person 500 0x8a4f7d),$(person 1420 0x3f7cac)" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest two.mp4
ffmpeg $Q -f lavfi -i "color=c=0x30343f:s=1280x720:r=25:d=4" -vf "$(person 330 0x8a4f7d),$(person 950 0x3f7cac)" -c:v libx264 -crf 20 -pix_fmt yuv420p two-720.mp4
echo "test file: two.mp4 (two drawn figures centred at x=500 and x=1420 of 1920, 6 s, tone)"; probe two.mp4
echo; echo "## 1. stack the two halves: left speaker on top, right speaker below"
run "ffmpeg -i two.mp4 -filter_complex \"[0:v]crop=iw/2:iw*4/9:0:(ih-oh)/2,scale=1080:960[top];[0:v]crop=iw/2:iw*4/9:iw/2:(ih-oh)/2,scale=1080:960[bottom];[top][bottom]vstack\" -c:a copy stacked.mp4"; probe stacked.mp4
echo "edge: 1280x720 source at 25 fps, no audio"; run "ffmpeg -i two-720.mp4 -filter_complex \"[0:v]crop=iw/2:iw*4/9:0:(ih-oh)/2,scale=1080:960[top];[0:v]crop=iw/2:iw*4/9:iw/2:(ih-oh)/2,scale=1080:960[bottom];[top][bottom]vstack\" -c:a copy edge-stacked-720.mp4"; probe edge-stacked-720.mp4
echo; echo "## 2. one speaker at a time: crop on the left speaker (x = 500 - 304 = 196)"
run "ffmpeg -i two.mp4 -vf \"crop=ih*9/16:ih:196:0,scale=1080:1920\" -c:a copy left-speaker.mp4"; probe left-speaker.mp4
echo "edge: 1280x720 source at 25 fps, same command (left figure centred at x=330, so x=196 puts it right of centre)"; run "ffmpeg -i two-720.mp4 -vf \"crop=ih*9/16:ih:196:0,scale=1080:1920\" -c:a copy edge-left-720.mp4"; probe edge-left-720.mp4; echo "not published: fixed pixel sizes on the 720p file"; ffmpeg -i two-720.mp4 -vf "crop=608:1080:196:0,scale=1080:1920" nope.mp4 -y -hide_banner -loglevel error 2>&1 | head -1; echo "exit=${PIPESTATUS[0]}"
echo; echo "## arithmetic"
python3 - <<PY
for w,h in ((1920,1080),(1280,720)):
    cw=w/2; ch=w*4/9
    print(f"{w}x{h}: each half is {cw:.0f}x{ch:.1f} px (9:8), taken from rows {(h-ch)/2:.1f} to {(h+ch)/2:.1f}; scaled x{1080/cw:.3f} to 1080x960; {ch/h*100:.1f}% of the height kept")
print(f"single speaker crop on 1920x1080: 608 px = 31.7% of the width, zoom x{1920/1080:.3f}")
PY
for f in stacked left-speaker; do ffmpeg $Q -i $f.mp4 -frames:v 1 $f.png; done; ffmpeg $Q -i two.mp4 -frames:v 1 two.png
} > commands.log 2>&1; cd ..

echo FIX-DONE
# ---- b3fix2.sh
#!/bin/bash
# Batch 3 tests, 2026-11-21, ffmpeg 6.1.1 on the hub server. One folder and one commands.log per guide.
# Every "published:" line is the command exactly as the guide prints it; exit code and probe of the result follow.
set -u
ROOT=~/b3-work; mkdir -p $ROOT; cd $ROOT
F=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
Q="-y -hide_banner -loglevel error"
probe(){ ffprobe -v error -show_entries stream=codec_name,pix_fmt,width,height,r_frame_rate,channels,sample_rate,color_transfer:format=duration,size -of default=nw=1 "$1" 2>&1 | tr "\n" " "; echo; }
run(){ echo "published: $*"; local t0=$(date +%s.%N); eval "$* $Q"; local rc=$?; echo "exit=$rc time=$(echo "$(date +%s.%N)-$t0" | bc | cut -c1-5)s"; }
runloop(){ echo "published: $*"; local t0=$(date +%s.%N); eval "$*" 2>/dev/null; local rc=$?; echo "exit=$rc time=$(echo "$(date +%s.%N)-$t0" | bc | cut -c1-5)s"; }
hdr(){ echo "# $1: commands.log (2026-11-21, ffmpeg 6.1.1)"; }
# drawn "person": a head-and-shoulders block at horizontal centre $1 on a WxH frame
person(){ echo "drawbox=x=$1-90:y=ih*0.22:w=180:h=180:color=0xf2c9a0:t=fill,drawbox=x=$1-170:y=ih*0.22+190:w=340:h=ih*0.5:color=$2:t=fill"; }

############ screen-recording-to-vertical-short
d=screen-recording-to-vertical-short; rm -rf $d; mkdir -p $d; cd $d; {
hdr $d
printf "const total = items.reduce((sum, item) => sum + item.price, 0);" > t.txt
ffmpeg $Q -f lavfi -i "color=c=0x1e1e2e:s=1920x1080:r=30:d=8" -f lavfi -i "sine=f=220:d=8" -vf "drawbox=x=0:y=0:w=iw:h=40:color=0x313244:t=fill,drawtext=fontfile=$F:textfile=t.txt:x=(w-tw)/2:y=h/2:fontsize=16:fontcolor=0xa6e3a1" -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -shortest screen.mp4
ffmpeg $Q -f lavfi -i "color=c=0x4a3f35:s=1280x720:r=30:d=8" -vf "$(person 640 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p cam.mp4
ffmpeg $Q -f lavfi -i "color=c=0x4a3f35:s=1280x720:r=25:d=5" -vf "$(person 640 0x3d7a5a)" -c:v libx264 -crf 20 -pix_fmt yuv420p cam-short-25fps.mp4
echo "test files: screen.mp4 (editor mock, 1920x1080, 30 fps, 8 s, tone) and cam.mp4 (drawn figure, 1280x720, 30 fps, 8 s, no audio)"; probe screen.mp4; probe cam.mp4
echo; echo "## 1. screen on top (60% of the frame), webcam below (40%)"
run "ffmpeg -i screen.mp4 -i cam.mp4 -filter_complex \"[0:v]crop=ih*15/16:ih,scale=1080:1152[s];[1:v]crop=ih*45/32:ih,scale=1080:768[c];[s][c]vstack\" -map 0:a? -c:a copy -shortest stacked.mp4"; probe stacked.mp4
echo "edge: webcam clip is shorter (5 s) and 25 fps"; run "ffmpeg -i screen.mp4 -i cam-short-25fps.mp4 -filter_complex \"[0:v]crop=ih*15/16:ih,scale=1080:1152[s];[1:v]crop=ih*45/32:ih,scale=1080:768[c];[s][c]vstack\" -map 0:a? -c:a copy -shortest edge-stacked.mp4"; probe edge-stacked.mp4
echo; echo "## 2. screen only, webcam as a small picture in the corner"
run "ffmpeg -i screen.mp4 -i cam.mp4 -filter_complex \"[0:v]crop=ih*9/16:ih,scale=1080:1920[s];[1:v]crop=ih:ih,scale=300:300[c];[s][c]overlay=588:948\" -map 0:a? -c:a copy -shortest corner.mp4"; probe corner.mp4
echo "edge: shorter 25 fps webcam clip"; run "ffmpeg -i screen.mp4 -i cam-short-25fps.mp4 -filter_complex \"[0:v]crop=ih*9/16:ih,scale=1080:1920[s];[1:v]crop=ih:ih,scale=300:300[c];[s][c]overlay=588:948\" -map 0:a? -c:a copy -shortest edge-corner.mp4"; probe edge-corner.mp4
echo; echo "## arithmetic"
python3 - <<PY
print(f"stack: screen window {1080*15/16:.1f}x1080 px = {1080*15/16/1920*100:.1f}% of a 1920 px wide screen, scaled x{1152/1080:.3f}: 16 px text -> {16*1152/1080:.1f} px")
print(f"full-height 9:16 crop: 607.5 px = 31.6% of the width, scaled x{1920/1080:.3f}: 16 px text -> {16*1920/1080:.1f} px")
print(f"webcam strip 1080x768 = {768/1920*100:.0f}% of the frame; it starts at row 1152 and the bottom margin of the combined safe box starts at row 1248, so rows 1248..1920 of the webcam ({(1920-1248)/768*100:.1f}% of the strip) sit under app interface")
print("corner picture 300x300 at x=588..888, y=948..1248: inside the combined safe box (x 120..888, y 288..1248)")
print(f"stack, screen on top: screen rows 0..1152, clear of app interface rows 288..1152 = {(1152-288)/1152*100:.1f}% of the screen strip")
print(f"stack, webcam on top instead: webcam rows 0..768 (clear 288..768 = {(768-288)/768*100:.1f}%), screen rows 768..1920 (clear 768..1248 = {(1248-768)/1152*100:.1f}%)")
PY
for f in stacked corner; do ffmpeg $Q -ss 1 -i $f.mp4 -frames:v 1 $f.png; done
} > commands.log 2>&1; cd ..

############ long-youtube-video-to-batch-pack
d=long-youtube-video-to-batch-pack; rm -rf $d; mkdir -p $d; cd $d; {
hdr $d
printf "%s" "%{pts:hms}" > clock.txt
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=120" -f lavfi -i "sine=f=200:d=120" -vf "drawtext=fontfile=$F:textfile=clock.txt:x=(w-tw)/2:y=h/2:fontsize=64:fontcolor=white:box=1:boxcolor=black" -c:v libx264 -preset veryfast -crf 26 -g 60 -pix_fmt yuv420p -c:a aac -shortest long.mp4
printf "hook,00:00:05,00:00:20\nmain-point,00:00:40,00:01:05\nending,00:01:50,00:02:30\n" > cuts.csv
echo "test file: long.mp4 (test pattern with a burned clock, 1920x1080, 120 s, tone)"; probe long.mp4
echo "cuts.csv:"; cat cuts.csv
echo; echo "## 1. one clip per row of cuts.csv, cropped to 9:16"
runloop "while IFS=, read -r name start end; do ffmpeg -nostdin -ss \"\$start\" -to \"\$end\" -i long.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:v libx264 -crf 20 -c:a aac \"\$name.mp4\"; done < cuts.csv"
for n in hook main-point ending; do printf "%s: " $n; probe $n.mp4; done
echo "note: the third row asks for 00:01:50 to 00:02:30 but the file ends at 00:02:00, so that clip is 10 s"
echo "edge: the same loop WITHOUT -nostdin (not published)"; mkdir -p nostdin; cd nostdin; cp ../cuts.csv ../long.mp4 .
while IFS=, read -r name start end; do ffmpeg -ss "$start" -to "$end" -i long.mp4 -vf "crop=ih*9/16:ih,scale=1080:1920" -c:v libx264 -crf 20 -c:a aac "$name.mp4" -y -hide_banner -loglevel error; done < cuts.csv 2>/dev/null
echo "files written without -nostdin: $(ls *.mp4 | grep -v long.mp4 | tr '\n' ' ')"; cd ..
echo "edge: a row whose name has a space"; printf "two words,00:00:10,00:00:14\n" > cuts.csv.bak; cp cuts.csv cuts.orig; cp cuts.csv.bak cuts.csv
runloop "while IFS=, read -r name start end; do ffmpeg -nostdin -ss \"\$start\" -to \"\$end\" -i long.mp4 -vf \"crop=ih*9/16:ih,scale=1080:1920\" -c:v libx264 -crf 20 -c:a aac \"\$name.mp4\"; done < cuts.csv"; ls -1 "two words.mp4"; cp cuts.orig cuts.csv
echo; echo "## 2. the same clips as square files for a feed post"
runloop "while IFS=, read -r name start end; do ffmpeg -nostdin -ss \"\$start\" -to \"\$end\" -i long.mp4 -vf \"crop=ih:ih,scale=1080:1080\" -c:v libx264 -crf 20 -c:a aac \"\$name-square.mp4\"; done < cuts.csv"
for n in hook main-point ending; do printf "%s-square: " $n; probe $n-square.mp4; done
echo "edge: an empty last line in cuts.csv"; printf "hook,00:00:05,00:00:08\n\n" > cuts.csv
runloop "while IFS=, read -r name start end; do ffmpeg -nostdin -ss \"\$start\" -to \"\$end\" -i long.mp4 -vf \"crop=ih:ih,scale=1080:1080\" -c:v libx264 -crf 20 -c:a aac \"\$name-square.mp4\"; done < cuts.csv"; echo "files after the empty-line run: $(ls | grep -c -- "^-square.mp4$") file named -square.mp4"; cp cuts.orig cuts.csv
ffmpeg $Q -i hook.mp4 -frames:v 1 hook.png
} > commands.log 2>&1; cd ..

############ gif-to-video
d=gif-to-video; rm -rf $d; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=480x270:r=10:d=2" -vf "pad=481:271,split[a][b];[a]palettegen[p];[b][p]paletteuse" -loop 0 anim.gif
ffmpeg $Q -f lavfi -i "testsrc=s=320x240:r=10:d=1.5" -vf "split[a][b];[a]palettegen[p];[b][p]paletteuse" -loop -1 once.gif
echo "test file: anim.gif (test pattern, 481x271, 10 fps, 2 s, loops forever)"; probe anim.gif; echo "edge file: once.gif"; probe once.gif; ls -l anim.gif | awk '{print "bytes "$5}'
echo; echo "## 1. GIF to MP4"
run "ffmpeg -i anim.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" anim.mp4"; probe anim.mp4
echo "edge: a GIF set to play once (once.gif, 320x240, 1.5 s)"; run "ffmpeg -i once.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" once.mp4"; probe once.mp4
echo "edge: the odd-sized GIF WITHOUT the scale step (not published)"; ffmpeg -i anim.gif -movflags +faststart -pix_fmt yuv420p fail.mp4 -y -hide_banner -loglevel error 2>&1 | head -1; echo "exit=${PIPESTATUS[0]}"
echo; echo "## 2. repeat the GIF five times so the video is long enough"
run "ffmpeg -stream_loop 4 -i anim.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" anim-x5.mp4"; probe anim-x5.mp4
echo "edge: the play-once GIF"; run "ffmpeg -stream_loop 4 -i once.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=trunc(iw/2)*2:trunc(ih/2)*2\" once-x5.mp4"; probe once-x5.mp4
echo; echo "## 3. upscale a small GIF to 1080 wide without blur (nearest neighbour)"
run "ffmpeg -stream_loop 4 -i anim.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=1080:-2:flags=neighbor\" anim-1080.mp4"; probe anim-1080.mp4
echo "edge: the play-once GIF"; run "ffmpeg -stream_loop 4 -i once.gif -movflags +faststart -pix_fmt yuv420p -vf \"scale=1080:-2:flags=neighbor\" once-1080.mp4"; probe once-1080.mp4
echo; echo "## sizes"; ls -l anim.gif anim.mp4 anim-x5.mp4 anim-1080.mp4 | awk '{print $5" bytes "$9}'
python3 - <<PY
import os
g=os.path.getsize("anim.gif"); m=os.path.getsize("anim.mp4"); print(f"anim.mp4 is {m/g*100:.1f}% of anim.gif ({g} -> {m} bytes)")
PY
} > commands.log 2>&1; cd ..

echo FIX2-DONE
# ---- b3fix3.sh
#!/bin/bash
# Batch 3 tests, 2026-11-21, ffmpeg 6.1.1 on the hub server. One folder and one commands.log per guide.
# Every "published:" line is the command exactly as the guide prints it; exit code and probe of the result follow.
set -u
ROOT=~/b3-work; mkdir -p $ROOT; cd $ROOT
F=/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf
Q="-y -hide_banner -loglevel error"
probe(){ ffprobe -v error -show_entries stream=codec_name,pix_fmt,width,height,r_frame_rate,channels,sample_rate,color_transfer:format=duration,size -of default=nw=1 "$1" 2>&1 | tr "\n" " "; echo; }
run(){ echo "published: $*"; local t0=$(date +%s.%N); eval "$* $Q"; local rc=$?; echo "exit=$rc time=$(echo "$(date +%s.%N)-$t0" | bc | cut -c1-5)s"; }
hdr(){ echo "# $1: commands.log (2026-11-21, ffmpeg 6.1.1)"; }
# drawn "person": a head-and-shoulders block at horizontal centre $1 on a WxH frame
person(){ echo "drawbox=x=$1-90:y=ih*0.22:w=180:h=180:color=0xf2c9a0:t=fill,drawbox=x=$1-170:y=ih*0.22+190:w=340:h=ih*0.5:color=$2:t=fill"; }

############ webm-recording-for-social
d=webm-recording-for-social; rm -rf $d; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=1280x720:r=30:d=8" -f lavfi -i "sine=f=260:d=8" -c:v libvpx-vp9 -b:v 0 -crf 34 -deadline realtime -cpu-used 8 -c:a libopus -b:a 96k -shortest rec.webm
ffmpeg $Q -f lavfi -i "testsrc2=s=1280x720:r=30:d=8" -f lavfi -i "sine=f=260:d=8" -c:v libvpx-vp9 -b:v 0 -crf 34 -deadline realtime -cpu-used 8 -c:a libopus -b:a 96k -shortest -live 1 rec-live.webm
echo "test file: rec.webm (test pattern, VP9 + Opus, 1280x720, 8 s)"; probe rec.webm
echo "edge file: rec-live.webm (same content written as a live stream: no duration in the header, like many browser recordings)"; probe rec-live.webm
echo; echo "## 1. WebM to MP4 (H.264 + AAC)"
run "ffmpeg -i rec.webm -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart rec.mp4"; probe rec.mp4
echo "edge: the live-style file with no duration"; run "ffmpeg -i rec-live.webm -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart rec-live.mp4"; probe rec-live.mp4
echo; echo "## 2. same, forcing a constant 30 frames per second"
run "ffmpeg -i rec.webm -c:v libx264 -crf 20 -pix_fmt yuv420p -r 30 -fps_mode cfr -c:a aac -b:a 160k -movflags +faststart rec-cfr.mp4"; probe rec-cfr.mp4
ffmpeg $Q -f lavfi -i "testsrc2=s=1280x720:r=30:d=6" -vf "select='not(eq(mod(n\,3)\,1))'" -fps_mode vfr -c:v libvpx-vp9 -b:v 0 -crf 34 -deadline realtime -cpu-used 8 rec-vfr.webm
echo "edge: a file with uneven frame timing (rec-vfr.webm: every third frame dropped, as a busy recorder does; no audio); distinct frame gaps before:"; ffprobe -v error -select_streams v:0 -show_entries packet=pts_time -of csv=p=0 rec-vfr.webm | python3 -c "
import sys; t=[float(x) for x in sys.stdin.read().split() if x]; d=sorted({round(b-a,3) for a,b in zip(t,t[1:])}); print(len(d),'distinct gaps, from',d[0],'to',d[-1],'s')"
run "ffmpeg -i rec-vfr.webm -c:v libx264 -crf 20 -pix_fmt yuv420p -r 30 -fps_mode cfr -c:a aac -b:a 160k -movflags +faststart rec-vfr-cfr.mp4"; probe rec-vfr-cfr.mp4
echo "distinct frame gaps after:"; ffprobe -v error -select_streams v:0 -show_entries packet=pts_time -of csv=p=0 rec-vfr-cfr.mp4 | python3 -c "
import sys; t=sorted(float(x) for x in sys.stdin.read().split() if x); d=sorted({round(b-a,3) for a,b in zip(t,t[1:])}); print(len(d),'distinct gaps:',d)"
echo; echo "## 3. what a plain copy into MP4 does (not published as a fix)"
ffmpeg -i rec.webm -c copy copy.mp4 -y -hide_banner -loglevel error 2>&1 | head -2; echo "exit=${PIPESTATUS[0]}"; [ -f copy.mp4 ] && probe copy.mp4
echo; ls -l rec.webm rec.mp4 | awk '{print $5" bytes "$9}'
} > commands.log 2>&1; cd ..

############ iphone-video-for-upload
d=iphone-video-for-upload; rm -rf $d; mkdir -p $d; cd $d; {
hdr $d
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=5" -f lavfi -i "sine=f=240:d=5" -c:v libx265 -preset fast -crf 24 -pix_fmt yuv420p10le -color_primaries bt2020 -color_trc arib-std-b67 -colorspace bt2020nc -tag:v hvc1 -x265-params log-level=error -c:a aac -shortest hdr.mov
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=4" -c:v libx265 -preset fast -crf 24 -pix_fmt yuv420p -tag:v hvc1 -x265-params log-level=error sdr-hevc.mov
ffmpeg $Q -f lavfi -i "testsrc2=s=1920x1080:r=30:d=4" -c:v libx264 -crf 20 -pix_fmt yuv420p -metadata:s:v rotate=90 -c:a aac rot.mov 2>/dev/null
echo "test file: hdr.mov (derived by us, not recorded on an iPhone: test pattern encoded as HEVC 10-bit, HLG transfer, BT.2020, in a MOV)"; probe hdr.mov
echo; echo "## 0. what kind of file is it?"
echo "published: ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,pix_fmt,color_transfer -of default=nw=1 hdr.mov"; ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,pix_fmt,color_transfer -of default=nw=1 hdr.mov; echo "exit=$?"
echo "edge: the SDR file"; echo "published: ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,pix_fmt,color_transfer -of default=nw=1 sdr-hevc.mov"; ffprobe -v error -select_streams v:0 -show_entries stream=codec_name,pix_fmt,color_transfer -of default=nw=1 sdr-hevc.mov; echo "exit=$?"
echo; echo "## 1. HEVC to H.264, colours left as tagged (SDR sources)"
run "ffmpeg -i sdr-hevc.mov -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart sdr.mp4"; probe sdr.mp4
echo "edge: the HLG 10-bit file through the same command"; run "ffmpeg -i hdr.mov -c:v libx264 -crf 20 -pix_fmt yuv420p -c:a aac -b:a 160k -movflags +faststart hdr-naive.mp4"; probe hdr-naive.mp4
echo; echo "## 2. HDR (HLG) to SDR with tone mapping"
run "ffmpeg -i hdr.mov -vf \"zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=hable,zscale=t=bt709:m=bt709:r=tv,format=yuv420p\" -c:v libx264 -crf 20 -c:a aac -b:a 160k -movflags +faststart hdr-to-sdr.mp4"; probe hdr-to-sdr.mp4
echo "edge: an SDR 8-bit HEVC file through the tone-mapping command"; run "ffmpeg -i sdr-hevc.mov -vf \"zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=hable,zscale=t=bt709:m=bt709:r=tv,format=yuv420p\" -c:v libx264 -crf 20 -c:a aac -b:a 160k -movflags +faststart edge-sdr-tonemapped.mp4"; ls edge-sdr-tonemapped.mp4 >/dev/null 2>&1 && echo "file exists but is unreadable: $(stat -c %s edge-sdr-tonemapped.mp4) bytes"
echo; echo "## what changed: transfer tag and average brightness of the first frame (YAVG from signalstats, 8-bit scale)"
for f in hdr-naive.mp4 hdr-to-sdr.mp4 sdr.mp4; do printf "%s: transfer=%s " $f "$(ffprobe -v error -select_streams v:0 -show_entries stream=color_transfer -of csv=p=0 $f)"; ffmpeg -hide_banner -loglevel error -i $f -vf "format=yuv420p,signalstats,metadata=print:key=lavfi.signalstats.YAVG:file=-" -f null - | grep -o "YAVG=[0-9.]*" | head -1; done
echo "note: edge-sdr-tonemapped.mp4 was not written (the tone-mapping command stops on an SDR input, exit 187 above)"
ls -l hdr.mov hdr-to-sdr.mp4 | awk '{print $5" bytes "$9}'
} > commands.log 2>&1; cd ..

echo FIX3-DONE
