-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert-jpg-mp4
More file actions
executable file
·171 lines (146 loc) · 4.37 KB
/
Copy pathconvert-jpg-mp4
File metadata and controls
executable file
·171 lines (146 loc) · 4.37 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
# Convert JPG images to MP4 video
# Usage: ./convert-jpg-mp4 [OPTIONS]
set -euo pipefail
# Main function wrapper
main() {
# Check if common-functions exists
if [[ ! -f "$(dirname "$0")/common-functions" ]]; then
echo "Downloading common-functions from GitHub..."
if ! curl -fsSL https://raw.githubusercontent.com/Flower7C3/bash-tools/master/common-functions -o "$(dirname "$0")/common-functions"; then
echo "Failed to download common-functions"
exit 1
fi
fi
# Source common functions
source "$(dirname "$0")/common-functions"
# Call the actual main function
converter_main "$@"
}
converter_main() {
# Function to display usage
function show_usage() {
log_usage_title '[OPTIONS]'
echo
echo 'Convert JPG images to MP4 video'
echo
log_header 'Options'
log_usage_options_line '-s;--source <DIR>' \
'Source directory containing JPG images'
log_usage_options_line '-f;--framerate <FPS>' \
'Frame rate for output video (default: %s)' "$DEFAULT_FRAMERATE"
log_usage_options_line '-u;--update' \
'Update app'
log_usage_options_line '-h;--help' \
'Show this help message'
echo
log_header 'Examples'
log_usage_example_line '--source /path/to/images'
log_usage_example_line '-s /path/to/images -f 30'
log_usage_example_line "$(styler reset) Will prompt for source directory"
exit 0
}
# Default values
readonly DEFAULT_FRAMERATE=15
source_dir=""
framerate=$DEFAULT_FRAMERATE
# Function to parse command line arguments
parse_arguments() {
while [[ $# -gt 0 ]]; do
case "$1" in
-s | --source)
source_dir="$2"
shift 2
;;
-f | --framerate)
framerate="$2"
if ! validate_positive_number "$framerate" "Framerate"; then
exit 1
fi
shift 2
;;
-h | --help)
show_usage
;;
-u | --update)
update_application
;;
-*)
log_error 'Unknown option: <b>%s</b>' "$1"
echo
show_usage
;;
*)
log_error 'Unexpected argument: <b>%s</b>' "$1"
echo
show_usage
;;
esac
done
}
# Function to prompt for directory
prompt_for_directory() {
while true; do
prompt_input '_source_dir' "Enter source directory path" ""
if [ -z "$_source_dir" ]; then
log_error 'Directory path cannot be empty'
continue
fi
if ! verify_directory "$_source_dir"; then
continue
fi
break
done
}
# Function to verify directory and JPG files
verify_directory() {
local _source_dir="$1"
if [[ "$_source_dir" != */ ]]; then
_source_dir="${_source_dir}/"
fi
if ! validate_directory "$_source_dir"; then
return 1
fi
# Check if directory contains JPG files
local _jpg_count
_jpg_count=$(find "$_source_dir" -maxdepth 1 -name "*.JPG" -o -name "*.jpg" | wc -l | tr -d ' ')
if [ "$_jpg_count" -eq 0 ]; then
die 11 "No JPG files found in <u>%s</u> directory" "$_source_dir"
fi
log_info ---icon '🖼' 'Found <b>%s</b> JPG files in <u>%s</u> directory' "$_jpg_count" "$_source_dir"
source_dir="$_source_dir"
return 0
}
check_dependencies ffmpeg
parse_arguments "$@"
log_title 'JPG to MP4 Converter'
if [ -z "$source_dir" ]; then
prompt_for_directory
else
if ! verify_directory "$source_dir"; then
exit 1
fi
fi
cd "$source_dir" || die "Cannot access <u>$source_dir</u> directory"
confirm_or_exit "Convert those files to MP4 video with <b>$framerate</b> framerate?"
temp_dir="/tmp/animate_jpg_$$"
read -r -a jpg_files <<<"$(find "$source_dir" -maxdepth 1 -name "*.JPG" -o -name "*.jpg" | sort)"
first_file=$(basename "${jpg_files[0]}")
extension="${first_file##*.}"
output_file="video_$(date +%Y%m%d_%H%M%S).mp4"
mkdir -p "$temp_dir"
# Rename files to sequential pattern for ffmpeg
for i in "${!jpg_files[@]}"; do
new_name=$(printf "img%05d.%s" $((i + 1)) "$extension")
cp "${jpg_files[$i]}" "$temp_dir/$new_name"
done
if ffmpeg -framerate "$framerate" -i "$temp_dir/img%05d.$extension" -vf format=yuv420p "$output_file"; then
rm -rf "$temp_dir"
log_success ---icon '🎬' ---status "$source_dir$output_file" "Video created"
else
rm -rf "$temp_dir"
die 12 "Video conversion failed"
fi
}
# Call main function
main "$@"