-
Notifications
You must be signed in to change notification settings - Fork 0
/
vydPngToVideo
executable file
·69 lines (60 loc) · 2.39 KB
/
vydPngToVideo
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
#!/bin/bash
# Define colors for output and default values
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
background_color="000000"
frame_rate=30
# Generate a unique temporary folder for this execution
temp_folder=$(mktemp -d -t processed_images_XXXXXX)
# Check for flags and parameters
while getopts ":f:r:" opt; do
case $opt in
f) force_replace=1 ;;
r) frame_rate="${OPTARG}" ;;
\?) echo -e "${RED}Invalid option: -$OPTARG${NC}" >&2; exit 1 ;;
esac
done
shift $((OPTIND-1))
# Function to display usage instructions
function usage {
echo -e "${RED}Usage: $0 [-f] [-r frame_rate] <input_folder> <output_folder> [<background_color>]${NC}"
echo -e " -f: Force replace existing output file without prompting."
echo -e " -r: Set the frame rate for the output video (default is 30)."
echo -e " <input_folder>: Folder containing PNG images to process."
echo -e " <output_folder>: Folder where processed images and output video will be saved."
echo -e " [<background_color>]: Optional hex color for the video background (default: #000000)."
echo ""
echo "Example:"
echo -e " ${RED}$0 -f -r 24 /path/to/input /path/to/output #000000${NC}"
exit 1
}
# Validate and assign arguments
if [ $# -lt 2 ]; then
echo -e "${RED}Error: Missing required arguments.${NC}"
usage
fi
input_folder="$1"
output_folder="$2"
background_color="${3:-$background_color}" # Default to black if not provided
# Create the output folder if it doesn't exist
mkdir -p "$output_folder"
# Apply background to PNG files using ImageMagick
find "$input_folder" -name '*.png' | sort -V | while IFS= read -r file; do
filename=$(basename -- "$file")
# Use ImageMagick to composite the PNG over a solid background
convert "$file" -background "#${background_color}" -flatten "$temp_folder/$filename"
echo -e "${GREEN}Processed with background: $filename${NC}"
done
# Generate high-quality video from processed images
output_video="$output_folder/output_${background_color}.mp4"
ffmpeg -framerate "$frame_rate" -pattern_type glob -i "$temp_folder/*.png" -c:v libx264 -pix_fmt yuv420p -crf 10 -preset slow "$output_video"
if [ $? -eq 0 ]; then
echo -e "${GREEN}Video generated: $output_video${NC}"
else
echo -e "${RED}Failed to generate video: $output_video${NC}"
exit 1
fi
# Cleanup temporary folder
rm -rf "$temp_folder"
echo -e "${GREEN}Temporary files cleaned up.${NC}"