File size: 3,814 Bytes
35928b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/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 "$@"