Created
June 23, 2024 16:55
-
-
Save Imeguras/5f5076a8df0b3684459ef1af7bb498e3 to your computer and use it in GitHub Desktop.
Preprocess HandiTalk shell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# Base directory | |
BASE_DIR="<target>" | |
CATEGORIES=("Words" "Alphabet") | |
RESIZED_DIR="Resize" | |
FRAMES_DIR="Frames" | |
FRAME_COUNT=8 | |
# Function to resize video and save | |
resize_video() { | |
input_path="$1" | |
output_path="$2" | |
mkdir -p "$(dirname "$output_path")" | |
ffmpeg -i "$input_path" -vf "scale=500:500" "$output_path" | |
} | |
# Function to extract frames and save as PNG | |
extract_frames() { | |
input_path="$1" | |
output_dir="$2" | |
mkdir -p "$output_dir" | |
ffmpeg -i "$1" -vf "select=not(mod(n\,5)),scale=250:250" -vsync vfr -vframes $FRAME_COUNT "$output_dir/frame_%04d.png" | |
} | |
# Traverse directories and process videos | |
for category in "${CATEGORIES[@]}"; do | |
for class_dir in "$BASE_DIR/$category/Original"/*/; do | |
class_name=$(basename "$class_dir") | |
for video in "$class_dir"/*.mp4; do | |
video_name=$(basename "$video") | |
# Resize video | |
output_resized="$BASE_DIR/$category/$RESIZED_DIR/$class_name/$video_name" | |
resize_video "$video" "$output_resized" | |
# Extract frames | |
total_frames=$(ffmpeg -i "$video" -map 0:v:0 -c copy -f null - 2>&1 | grep -oP '(?<=frame=)\s*\d+' | tail -1) | |
output_frames="$BASE_DIR/$category/$FRAMES_DIR/$class_name/${video_name%.mp4}" | |
extract_frames "$BASE_DIR/$category/$RESIZED_DIR/$class_name/$video_name" "$output_frames" | |
done | |
done | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment