-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_audio_chunks.sh
More file actions
executable file
·147 lines (130 loc) · 3.52 KB
/
process_audio_chunks.sh
File metadata and controls
executable file
·147 lines (130 loc) · 3.52 KB
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
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
# ANSI color codes
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to print colored output
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}" >&2
}
print_info() {
echo "$1"
}
# Function to display help message
show_help() {
echo "Usage: $0 [options] <directory> <workers>"
echo
echo "Process multiple audio chunks in parallel using generate_tiktok.sh"
echo
echo "Arguments:"
echo " directory Directory containing chunk_* subdirectories"
echo " workers Number of parallel workers (default: 1)"
echo
echo "Options:"
echo " -h, --help Show this help message"
echo " --debug Enable debug mode"
echo " --regenerate Force regeneration of outputs"
}
# Parse command line arguments
DEBUG=""
REGENERATE=""
DIRECTORY=""
WORKERS=1
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
--debug)
DEBUG="--debug"
shift
;;
--regenerate)
REGENERATE="--regenerate"
shift
;;
*)
if [[ -z "$DIRECTORY" ]]; then
DIRECTORY="$1"
elif [[ -z "$WORKERS" || "$WORKERS" -eq 1 ]]; then
if [[ "$1" =~ ^[0-9]+$ ]]; then
WORKERS="$1"
else
print_error "Error: Workers must be a number"
show_help
exit 1
fi
else
print_error "Error: Unexpected argument '$1'"
show_help
exit 1
fi
shift
;;
esac
done
# Validate arguments
if [[ -z "$DIRECTORY" ]]; then
print_error "Error: Directory is required"
show_help
exit 1
fi
if [[ ! -d "$DIRECTORY" ]]; then
print_error "Error: Directory '$DIRECTORY' does not exist"
exit 1
fi
# Function to process a chunk directory
process_chunk() {
local chunk_dir="$1"
print_info "Processing $chunk_dir..."
if ./generate_tiktok.sh $DEBUG $REGENERATE "$chunk_dir"; then
print_success "Completed processing $chunk_dir"
return 0
else
print_error "Failed processing $chunk_dir"
return 1
fi
}
# Find all chunk directories and sort them numerically
chunk_dirs=($(find "$DIRECTORY" -type d -name "chunk_*" | sort -t_ -k2 -n))
if [ ${#chunk_dirs[@]} -eq 0 ]; then
print_error "No chunk_* directories found in $DIRECTORY"
exit 1
fi
print_info "Found ${#chunk_dirs[@]} chunks to process"
print_info "Using $WORKERS parallel workers"
# Process chunks in parallel with maximum number of workers
pids=()
for chunk_dir in "${chunk_dirs[@]}"; do
# Wait if we've reached maximum number of workers
while [ ${#pids[@]} -ge $WORKERS ]; do
for i in "${!pids[@]}"; do
if ! kill -0 ${pids[$i]} 2>/dev/null; then
wait ${pids[$i]}
exit_code=$?
unset pids[$i]
if [ $exit_code -ne 0 ]; then
print_error "A worker process failed"
exit 1
fi
fi
done
sleep 1
done
# Start new process
process_chunk "$chunk_dir" &
pids+=($!)
done
# Wait for remaining processes to complete
for pid in "${pids[@]}"; do
wait $pid
if [ $? -ne 0 ]; then
print_error "A worker process failed"
exit 1
fi
done
print_success "All chunks processed successfully!"