#!/bin/bash
# 把 44 帧 + 44 段配音合成 mp4：每段 = 配音时长 + 0.7s 停顿
set -e
cd /data/simpleEval_sim/preview/f9_video
rm -f list.txt alist.txt; mkdir -p seg
for a in tts/*.mp3; do b=$(basename $a .mp3); f=frames/f$b.png
  d=$(ffprobe -v error -show_entries format=duration -of csv=p=0 $a)
  t=$(python3 -c "print(round($d+0.7,3))")
  echo "file '$PWD/$f'" >> list.txt; echo "duration $t" >> list.txt
  ffmpeg -v error -y -i $a -af "apad=pad_dur=0.7" -ar 44100 -ac 2 seg/$b.wav
  echo "file '$PWD/seg/$b.wav'" >> alist.txt
done
# concat demuxer 需要最后一帧再列一次
tail -2 list.txt | head -1 >> list.txt
ffmpeg -v error -y -f concat -safe 0 -i alist.txt -c:a aac -b:a 160k audio.m4a
ffmpeg -v error -y -f concat -safe 0 -i list.txt -i audio.m4a -vf "crop=1920:1080:0:0,fps=25,format=yuv420p" -c:v libx264 -preset medium -crf 20 -c:a copy -shortest -movflags +faststart f9_derivation.mp4
ffprobe -v error -show_entries format=duration:stream=width,height -of default=nw=1 f9_derivation.mp4; ls -la f9_derivation.mp4
