#!/bin/bash set -e export PATH="/tmp/ffmpeg-master-latest-linux64-gpl/bin:$PATH" WORKSPACE="/home/runner/.openclaw/workspace" CHUNK_DIR="/tmp/tts_chunks" OUT_DIR="/tmp/tts_giselle" mkdir -p "$OUT_DIR" VOICE="scarlett_johansson" FMT="ogg" LOG="$OUT_DIR/batch.log" echo "=== Batch TTS started at $(date -u +%H:%M:%S) ===" | tee "$LOG" for chunkfile in $(ls "$CHUNK_DIR"/chunk_*.txt | sort); do idx=$(basename "$chunkfile" .txt | sed 's/chunk_//') outfile="$OUT_DIR/chunk_${idx}.ogg" if [ -f "$outfile" ] && [ $(stat -c%s "$outfile") -gt 1000 ]; then echo "Chunk $idx already exists ($(stat -c%s "$outfile") bytes), skipping" | tee -a "$LOG" continue fi chars=$(wc -c < "$chunkfile") echo "=== Starting chunk $idx at $(date -u +%H:%M:%S) (${chars} chars) ===" | tee -a "$LOG" cd "$WORKSPACE" result=$(python3 scripts/voice.py --file "$chunkfile" "$VOICE" "$FMT" 2>&1) exitcode=$? if [ $exitcode -eq 0 ]; then gen_file=$(echo "$result" | tail -1) if [ -f "$gen_file" ] && [ $(stat -c%s "$gen_file") -gt 1000 ]; then cp "$gen_file" "$outfile" echo "=== Chunk $idx done: $(stat -c%s "$outfile") bytes at $(date -u +%H:%M:%S) ===" | tee -a "$LOG" else echo "=== Chunk $idx FAILED: file missing/small, retrying in 30s ===" | tee -a "$LOG" sleep 30 result=$(python3 scripts/voice.py --file "$chunkfile" "$VOICE" "$FMT" 2>&1) if [ $? -eq 0 ]; then gen_file=$(echo "$result" | tail -1) if [ -f "$gen_file" ] && [ $(stat -c%s "$gen_file") -gt 1000 ]; then cp "$gen_file" "$outfile" echo "=== Chunk $idx retry OK: $(stat -c%s "$outfile") bytes ===" | tee -a "$LOG" fi fi fi else echo "=== Chunk $idx FAILED (exit=$exitcode), retrying in 30s ===" | tee -a "$LOG" sleep 30 result=$(python3 scripts/voice.py --file "$chunkfile" "$VOICE" "$FMT" 2>&1) if [ $? -eq 0 ]; then gen_file=$(echo "$result" | tail -1) if [ -f "$gen_file" ] && [ $(stat -c%s "$gen_file") -gt 1000 ]; then cp "$gen_file" "$outfile" echo "=== Chunk $idx retry OK: $(stat -c%s "$outfile") bytes ===" | tee -a "$LOG" fi fi fi sleep 2 done echo "=== Concatenating at $(date -u +%H:%M:%S) ===" | tee -a "$LOG" concat_list="$OUT_DIR/concat.txt" > "$concat_list" for chunkfile in $(ls "$CHUNK_DIR"/chunk_*.txt | sort); do idx=$(basename "$chunkfile" .txt | sed 's/chunk_//') f="$OUT_DIR/chunk_${idx}.ogg" if [ -f "$f" ]; then echo "file '$f'" >> "$concat_list" fi done ffmpeg -y -f concat -safe 0 -i "$concat_list" -c copy "$OUT_DIR/giselle_60min_scarjo.ogg" 2>/dev/null final_size=$(stat -c%s "$OUT_DIR/giselle_60min_scarjo.ogg" 2>/dev/null || echo 0) duration=$(ffprobe -v quiet -show_entries format=duration -of csv=p=0 "$OUT_DIR/giselle_60min_scarjo.ogg" 2>/dev/null || echo "unknown") echo "=== FINAL: $OUT_DIR/giselle_60min_scarjo.ogg ===" | tee -a "$LOG" echo "=== SIZE: ${final_size} bytes ===" | tee -a "$LOG" echo "=== DURATION: ${duration}s ===" | tee -a "$LOG" echo "=== Batch complete at $(date -u +%H:%M:%S) ===" | tee -a "$LOG"