xvbench / split_video.sh
qiuchenwang
update
35928b7
#!/usr/bin/env bash
set -euo pipefail
INPUT_DIR=""
OUTPUT_DIR="./video_chunks"
CHUNK_DURATION=60
MIN_SOURCE_DURATION=1
MIN_CHUNK_DURATION=2
usage() {
cat <<'EOF'
Usage: ./split_video.sh -i INPUT_DIR [-o OUTPUT_DIR] [-d CHUNK_DURATION]
Split videos into fixed-duration chunks for XVBench-style video evidence.
Options:
-i INPUT_DIR Directory containing source videos.
-o OUTPUT_DIR Directory for output clips. Default: ./video_chunks
-d CHUNK_DURATION Clip duration in seconds. Default: 60
-h Show this help message.
EOF
}
check_dependencies() {
command -v ffmpeg >/dev/null 2>&1 || { echo "ffmpeg is required." >&2; exit 1; }
command -v ffprobe >/dev/null 2>&1 || { echo "ffprobe is required." >&2; exit 1; }
}
duration_seconds() {
ffprobe -v error -show_entries format=duration \
-of default=noprint_wrappers=1:nokey=1 "$1" 2>/dev/null || echo "0"
}
float_lt() {
awk -v a="$1" -v b="$2" 'BEGIN { exit !(a < b) }'
}
float_add() {
awk -v a="$1" -v b="$2" 'BEGIN { printf "%.3f", a + b }'
}
parse_args() {
while getopts ":i:o:d:h" opt; do
case "$opt" in
i) INPUT_DIR="$OPTARG" ;;
o) OUTPUT_DIR="$OPTARG" ;;
d) CHUNK_DURATION="$OPTARG" ;;
h) usage; exit 0 ;;
*) usage; exit 1 ;;
esac
done
if [[ -z "$INPUT_DIR" || ! -d "$INPUT_DIR" ]]; then
echo "A valid input directory is required. Use -i INPUT_DIR." >&2
exit 1
fi
if ! [[ "$CHUNK_DURATION" =~ ^[0-9]+$ ]] || [[ "$CHUNK_DURATION" -le 0 ]]; then
echo "CHUNK_DURATION must be a positive integer." >&2
exit 1
fi
}
split_one_video() {
local input_file="$1"
local filename extension base_name total_duration process_file temp_mp4
local start_time=0
local chunk_index=1
filename=$(basename "$input_file")
extension="${filename##*.}"
extension=$(echo "$extension" | tr '[:upper:]' '[:lower:]')
base_name="${filename%.*}"
temp_mp4=""
total_duration=$(duration_seconds "$input_file")
if [[ -z "$total_duration" || "$total_duration" == "0" ]] || float_lt "$total_duration" "$MIN_SOURCE_DURATION"; then
echo "Skip invalid or too-short video: $filename" >&2
return 1
fi
if [[ "$extension" != "mp4" ]]; then
temp_mp4="${OUTPUT_DIR}/.tmp_${base_name}_$$.mp4"
if ! ffmpeg -i "$input_file" -c:v libx264 -c:a aac -strict experimental \
"$temp_mp4" -y -loglevel error; then
rm -f "$temp_mp4"
echo "Failed to convert video: $filename" >&2
return 1
fi
process_file="$temp_mp4"
else
process_file="$input_file"
fi
while float_lt "$start_time" "$total_duration"; do
local output_file chunk_duration
output_file="${OUTPUT_DIR}/${base_name}_____${chunk_index}.mp4"
if ffmpeg -i "$process_file" -ss "$start_time" -t "$CHUNK_DURATION" \
-c copy "$output_file" -y -loglevel error; then
chunk_duration=$(duration_seconds "$output_file")
if [[ -z "$chunk_duration" ]] || float_lt "$chunk_duration" "$MIN_CHUNK_DURATION"; then
rm -f "$output_file"
fi
else
rm -f "$output_file"
echo "Failed to create chunk: $output_file" >&2
fi
start_time=$(float_add "$start_time" "$CHUNK_DURATION")
chunk_index=$((chunk_index + 1))
done
[[ -n "$temp_mp4" && -f "$temp_mp4" ]] && rm -f "$temp_mp4"
}
main() {
parse_args "$@"
check_dependencies
mkdir -p "$OUTPUT_DIR"
while IFS= read -r -d '' input_file; do
local filename
filename=$(basename "$input_file")
if [[ "$filename" =~ _____[0-9]+\.mp4$ ]]; then
continue
fi
split_one_video "$input_file" || true
done < <(find "$INPUT_DIR" -maxdepth 1 -type f \( \
-iname "*.mp4" -o -iname "*.avi" -o -iname "*.mkv" -o \
-iname "*.mov" -o -iname "*.wmv" -o -iname "*.flv" -o \
-iname "*.webm" \) -print0)
}
main "$@"