#!/usr/bin/env bash
# v4 交付件：对比条 / 上下对比 / 抽帧 / 身份对照 / 白模-v4 对应帧拼接 / meta / sha256
# 注意：所有中间产物写在项目目录内，不用 /tmp
set -eu
D="/home/zyw/Downloads/dl-hub/10-视频生成流水线/20261008_1800_H3换人_源15v4_照片锚脸水手服_中式庭院"
T="$D/tmp_deliver"
mkdir -p "$T"
cd "$D"
OUT="20261008_H3换人_源15v4_中式庭院_水手服盘发_原声版_1184x896_47.67s_H264.mp4"
REF="m15_ref_padded_768x576_1240f.mp4"
SRC15="/home/zyw/Downloads/dl-hub/56-抖音视频下载/白模素材/15_模来AIGC_是否真的四大皆空-要镜像的可以下载视频去处理一下-四连自取-禁止恶搞-武家坡_2904x2160.mp4"

echo "[1/6] 对比条 + 上下对比"
./make_compare.sh "$REF" "$OUT" "20261008_H3换人_源15v4_对比" 1/6 2>&1 | tail -1

echo "[2/6] 抽帧"
for t in 2 20 44; do ffmpeg -v error -y -ss $t -i "$OUT" -frames:v 1 -q:v 3 "20261008_H3换人_源15v4_抽帧_${t}s.jpg"; done

echo "[3/6] 身份对照（face1 + 三帧）"
ffmpeg -v error -y -i face1_front.jpg -i "20261008_H3换人_源15v4_抽帧_2s.jpg" -i "20261008_H3换人_源15v4_抽帧_20s.jpg" -i "20261008_H3换人_源15v4_抽帧_44s.jpg" \
  -filter_complex "[0:v]scale=-2:520[a];[1:v]scale=-2:520[b];[2:v]scale=-2:520[c];[3:v]scale=-2:520[d];[a][b][c][d]hstack=inputs=4" -frames:v 1 "20261008_H3换人_源15v4_身份对照_照片与成片.jpg"

echo "[4/6] 白模 vs v4 对应帧拼接（每 0.5 秒）"
ts="0.0 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0 8.5 9.0 9.5 10.0 10.5 11.0 11.5 12.0 12.5 13.0 13.5 14.0 14.5 15.0 15.5 16.0 16.5 17.0 17.5 18.0 18.5 19.0 19.5 20.0 20.5 21.0 21.5 22.0 22.5 23.0 23.5"
i=0
for t in $ts; do
  i=$((i+1)); n=$(printf %02d $i)
  ffmpeg -v error -y -ss $t -i "$REF" -frames:v 1 -vf "scale=200:-2,drawtext=text='白模 ${t}s':fontsize=11:fontcolor=cyan:x=3:y=3:box=1:boxcolor=black@0.65" "$T/s_$n.png"
  ffmpeg -v error -y -ss $t -i "$OUT" -frames:v 1 -vf "scale=200:-2,drawtext=text='v4 ${t}s':fontsize=11:fontcolor=lime:x=3:y=3:box=1:boxcolor=black@0.65" "$T/o_$n.png"
done
for g in 1 2 3 4; do
  a=$(( (g-1)*12 + 1 )); lst=""
  for k in $(seq $a $((a+11))); do n=$(printf %02d $k); lst="$lst -i $T/s_$n.png"; done
  eval ffmpeg -v error -y $lst -filter_complex "hstack=inputs=12" -frames:v 1 "$T/rowS_$g.png"
  lst=""
  for k in $(seq $a $((a+11))); do n=$(printf %02d $k); lst="$lst -i $T/o_$n.png"; done
  eval ffmpeg -v error -y $lst -filter_complex "hstack=inputs=12" -frames:v 1 "$T/rowO_$g.png"
  ffmpeg -v error -y -i "$T/rowS_$g.png" -i "$T/rowO_$g.png" -filter_complex "[0:v][1:v]vstack=inputs=2" -frames:v 1 "$T/pair_$g.png"
done
ffmpeg -v error -y -i "$T/pair_1.png" -i "$T/pair_2.png" -i "$T/pair_3.png" -i "$T/pair_4.png" -filter_complex "[0:v][1:v]vstack=inputs=2[a];[2:v][3:v]vstack=inputs=2[b];[a][b]vstack=inputs=2" -frames:v 1 "20261008_H3换人_源15v4_与白模对应帧拼接_每0.5秒.jpg"

echo "[5/6] meta + sha256"
python3 - <<'PYEOF'
import json, subprocess
def nf(p):
    return int(subprocess.run(["ffprobe","-v","error","-select_streams","v","-count_frames","-show_entries","stream=nb_read_frames","-of","csv=p=0",p],capture_output=True,text=True).stdout.strip() or 0)
out="20261008_H3换人_源15v4_中式庭院_水手服盘发_原声版_1184x896_47.67s_H264.mp4"
meta={
 "task_id":"20261008_1800_H3换人_源15v4_照片锚脸水手服_中式庭院",
 "requirement":"源15 white-clay replication with the new reference chain: real photos anchor the face, headless three-view supplies the costume, a new Chinese courtyard image supplies scene+lighting+style; action and camera left entirely to the clay reference",
 "prompt_style":"official Ref2VA format, lean: only character / costume / scene / light / photographic mood are described; action, gestures, limbs, camera, framing and timing delegated to <Video 1>",
 "references":{"Picture 1":"face1_front.jpg (face + low bun)","Picture 2":"outfit_threeview_B.png (sailor blouse, navy pleated skirt, black tights, dark brown heeled Mary-Janes)","Picture 3":"face4_smile.jpg (real smile)","Picture 4":"scene_courtyard.jpg (courtyard, moon gate, koi pond, hazy warm light - light/colour/style follow this)"},
 "scene_dynamics":"vines and leaves sway slightly, pond water keeps slow shallow ripples, koi drift and turn naturally",
 "generation":{"segments":"10 x 124 (source padded with 96 black frames to 1240 = 10x124)","continuity":"latent, overlap 22, keepTail=1","pass1":"768x576 4 steps","pass2":"h3_latent 3 steps denoise 0.2 -> 1184x896","seed":20261015,"prompt_id":"1ed1e678-f30e-4e68-8c7b-e7a0b31799f4"},
 "raw_export":"h3_bm15v4_courtyard_00001_.mp4 = 1348 frames = 1240 + 9x12 (nothing cut)",
 "post":"content region 1252 frames -> picture speed-up 1.094405 (setpts + minterpolate) -> 1144 frames = 47.667s, matching the original song at native speed",
 "deliverable":{"file":out,"frames":nf(out),"size":"1184x896","fps":24,"duration_s":47.666667},
 "note":"no FaceRefine (full-body dance + chunking caused face drift)"
}
open("meta_v4.json","w",encoding="utf-8").write(json.dumps(meta,ensure_ascii=False,indent=1))
print("meta_v4.json 帧数:", meta["deliverable"]["frames"])
PYEOF
sha256sum "$OUT" h3_bm15v4_courtyard_00001_.mp4 "$REF" > sha256_v4.txt

echo "[6/6] 清理项目内中间文件"
rm -rf "$T" "$D/tmp_v4_fast_mci.mp4"
df -h /tmp | tail -1
ls -la *.mp4 *.jpg meta_v4.json 2>/dev/null | tail -8
