44# Extracts the first frame from all videos in source/_static/videos/
55# and saves them as poster images in source/_static/images/poster/
66
7- set -e
8-
97# Define directories
108SCRIPT_DIR=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
119SOURCE_DIR=" $SCRIPT_DIR /source/_static/videos"
@@ -22,6 +20,17 @@ if ! command -v ffmpeg &> /dev/null; then
2220 exit 1
2321fi
2422
23+ # Check if timeout command is available
24+ if ! command -v timeout & > /dev/null; then
25+ echo " Warning: 'timeout' command not found. Script may hang on problematic videos."
26+ echo " On Ubuntu/Debian: sudo apt-get install coreutils"
27+ echo " On macOS: brew install coreutils"
28+ FFMPEG_TIMEOUT=" "
29+ else
30+ # Timeout for ffmpeg command (in seconds)
31+ FFMPEG_TIMEOUT=" timeout 30"
32+ fi
33+
2534# Check if source directory exists
2635if [ ! -d " $SOURCE_DIR " ]; then
2736 echo " Error: Source directory '$SOURCE_DIR ' does not exist."
@@ -35,6 +44,7 @@ echo "Created target directory: $TARGET_DIR"
3544# Counter for processed files
3645processed=0
3746skipped=0
47+ failed=0
3848
3949# Process all video files
4050echo " Processing videos in: $SOURCE_DIR "
@@ -53,36 +63,79 @@ for video_file in "$SOURCE_DIR"/*; do
5363 if [[ " ${VIDEO_EXTENSIONS[*]} " =~ " ${extension,,} " ]]; then
5464 output_file=" $TARGET_DIR /${filename_noext} .jpg"
5565
56- # Check if output file already exists
66+ # Check if output file already exists and is valid
5767 if [ -f " $output_file " ]; then
58- echo " � Skipping '$filename ' (poster already exists)"
59- (( skipped++ ))
60- continue
68+ # Verify the existing poster is valid (not empty and is an image)
69+ if [ -s " $output_file " ]; then
70+ echo " ✓ Skipping '$filename ' (poster already exists)"
71+ (( skipped++ ))
72+ continue
73+ else
74+ echo " ⚠ Removing invalid poster for '$filename '"
75+ rm -f " $output_file "
76+ fi
6177 fi
6278
63- echo " <� Processing '$filename '..."
79+ echo " ⏳ Processing '$filename '..."
6480
65- # Extract first frame using ffmpeg
66- if ffmpeg -i " $video_file " -vframes 1 -q:v 2 " $output_file " -y -loglevel error; then
67- echo " Created poster: ${filename_noext} .jpg"
68- (( processed++ ))
81+ # Extract first frame using ffmpeg with timeout
82+ if [ -n " $FFMPEG_TIMEOUT " ]; then
83+ # Use timeout if available
84+ if timeout 30 ffmpeg -i " $video_file " -vframes 1 -q:v 2 " $output_file " -y -loglevel error 2>&1 ; then
85+ ffmpeg_success=true
86+ else
87+ ffmpeg_success=false
88+ fi
6989 else
70- echo " L Failed to process '$filename '"
90+ # No timeout available, run ffmpeg directly
91+ if ffmpeg -i " $video_file " -vframes 1 -q:v 2 " $output_file " -y -loglevel error 2>&1 ; then
92+ ffmpeg_success=true
93+ else
94+ ffmpeg_success=false
95+ fi
96+ fi
97+
98+ if $ffmpeg_success ; then
99+ # Verify the output file was created and is valid
100+ if [ -f " $output_file " ] && [ -s " $output_file " ]; then
101+ echo " ✓ Created poster: ${filename_noext} .jpg"
102+ (( processed++ ))
103+ else
104+ echo " ✗ Failed to create valid poster for '$filename '"
105+ rm -f " $output_file " # Remove any partial output
106+ (( failed++ ))
107+ fi
108+ else
109+ # ffmpeg failed
110+ exit_code=$?
111+ if [ $exit_code -eq 124 ]; then
112+ echo " ✗ Timeout processing '$filename ' (after 30s)"
113+ else
114+ echo " ✗ Failed to process '$filename ' (exit code: $exit_code )"
115+ fi
71116 rm -f " $output_file " # Remove any partial output
117+ (( failed++ ))
72118 fi
73119 else
74- echo " � Skipping '$filename ' (not a supported video format)"
120+ echo " ⊘ Skipping '$filename ' (not a supported video format)"
75121 (( skipped++ ))
76122 fi
77123done
78124
79125echo " ------------------------------------------------"
80126echo " Poster generation completed!"
81- echo " =� Summary:"
82- echo " Processed: $processed videos"
83- echo " Skipped: $skipped files"
127+ echo " 📊 Summary:"
128+ echo " ✓ Processed: $processed videos"
129+ echo " → Skipped: $skipped files"
130+ if [ $failed -gt 0 ]; then
131+ echo " ✗ Failed: $failed videos"
132+ fi
84133echo " Posters saved to: $TARGET_DIR "
85134
86135if [ $processed -eq 0 ]; then
87- echo " � No new posters were created."
136+ if [ $failed -eq 0 ]; then
137+ echo " ℹ No new posters were created."
138+ else
139+ echo " ⚠ Some posters failed to generate. Please check the error messages above."
140+ fi
88141fi
0 commit comments