-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.sh
More file actions
executable file
·300 lines (253 loc) · 7.86 KB
/
Copy pathcreate.sh
File metadata and controls
executable file
·300 lines (253 loc) · 7.86 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/env bash
# Program:
# Convert your mp4 file to KDE Plasma splash screen
# 2025-12-07 Coldrain Release 1.0.0
set -Eeuo pipefail
tmp_dir=""
work_created=0
log_info() {
printf '[INFO] %s\n' "$1"
}
log_ok() {
printf '[OK] %s\n' "$1"
}
log_error() {
printf '[ERROR] %s\n' "$1" >&2
}
log_done() {
printf '[DONE] %s\n' "$1"
}
cleanup() {
if [[ -n "${tmp_dir:-}" && -d "$tmp_dir" ]]; then
rm -rf "$tmp_dir"
fi
}
handle_error() {
local exit_code=$?
log_error "exit=$exit_code at line $LINENO: $BASH_COMMAND"
if [[ "$work_created" -eq 1 && -n "${work_path:-}" && -d "$work_path" ]]; then
rm -rf "$work_path"
log_info "Removed incomplete work directory: $work_path"
fi
exit "$exit_code"
}
trap cleanup EXIT
trap handle_error ERR
script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
template_path="$script_dir/template"
target_path="$HOME/.local/share/plasma/look-and-feel"
print_header() {
printf '\n'
printf '== ✨ Video to Plasma Splash Screen ✨ ==\n'
printf '\n'
}
check_dependencies() {
log_info "Checking dependencies..."
for command_name in ffmpeg jq; do
if ! command -v "$command_name" >/dev/null 2>&1; then
log_error "'$command_name' is required but not installed."
exit 1
fi
log_ok "$command_name"
done
}
prompt_slug() {
local prompt_text="$1"
local value
while true; do
read -r -p "$prompt_text" value
if [[ "$value" =~ ^[A-Za-z0-9._-]+$ ]]; then
printf '%s\n' "$value"
return
fi
log_error "Please use only letters, numbers, '.', '_' or '-'."
done
}
prompt_existing_file() {
local prompt_text="$1"
local value
while true; do
read -r -p "$prompt_text" value
if [[ "$value" == "exit" ]]; then
log_info "Exit now."
exit 0
fi
if [[ -f "$value" ]]; then
printf '%s\n' "$value"
return
fi
log_error "File not found. Please enter an existing file path."
done
}
time_to_seconds() {
local time_value="$1"
local hours minutes seconds
IFS=: read -r hours minutes seconds <<<"$time_value"
printf '%d\n' "$((10#$hours * 3600 + 10#$minutes * 60 + 10#$seconds))"
}
prompt_time_range() {
local value begin end begin_seconds end_seconds
while true; do
read -r -p "[4/7] Clip range (hh:mm:ss~hh:mm:ss, less than 5 seconds recommended): " value
if [[ ! "$value" =~ ^[0-9]{2}:[0-9]{2}:[0-9]{2}~[0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
log_error "Invalid format. Example: 00:00:00~00:00:04"
continue
fi
begin="${value%%~*}"
end="${value##*~}"
begin_seconds="$(time_to_seconds "$begin")"
end_seconds="$(time_to_seconds "$end")"
if ((end_seconds <= begin_seconds)); then
log_error "End time must be greater than begin time."
continue
fi
printf '%s\n' "$value"
return
done
}
prompt_number() {
local prompt_text="$1"
local value
while true; do
read -r -p "$prompt_text" value
if [[ "$value" =~ ^[0-9]+$ && "$value" -gt 0 ]]; then
printf '%s\n' "$value"
return
fi
log_error "Please enter a positive number."
done
}
prompt_preview() {
local choice
while true; do
printf '[7/7] Preview image:\n'
printf ' 1) Auto-generate from video\n'
printf ' 2) Use custom png\n'
printf ' 3) Skip\n'
read -r -p "Choose [1/2/3] (default: 1): " choice
choice="${choice:-1}"
case "$choice" in
1)
preview_mode="auto"
preview_path=""
return
;;
2)
preview_path="$(prompt_existing_file "Preview png path: ")"
preview_mode="custom"
return
;;
3)
preview_mode="skip"
preview_path=""
return
;;
*)
log_error "Please choose 1, 2 or 3."
;;
esac
done
}
confirm_summary() {
printf '\n'
printf 'Generation summary:\n'
printf ' Author: %s\n' "$author_name"
printf ' Work: %s\n' "$work_name"
printf ' Output: %s\n' "$work_path"
printf ' Video: %s\n' "$video_path"
printf ' Clip: %s ~ %s\n' "$begin_time" "$end_time"
printf ' FPS: %s\n' "$fps"
printf ' Width: %s\n' "$scale_width"
case "$preview_mode" in
auto) printf ' Preview: auto-generate from video\n' ;;
custom) printf ' Preview: %s\n' "$preview_path" ;;
skip) printf ' Preview: skip\n' ;;
esac
printf '\n'
read -r -p "Continue? [Y/n]: " confirm
case "${confirm:-Y}" in
y | Y | yes | YES) ;;
*)
log_info "Cancelled."
exit 0
;;
esac
}
write_metadata() {
jq --arg author_name "$author_name" \
--arg plugin_id "$new_name" \
--arg plugin_name "$work_name" \
'.KPlugin.Authors[0].Name = $author_name
| .KPlugin.Id = $plugin_id
| .KPlugin.Description = $plugin_name
| .KPlugin.Name = $plugin_name' \
"$work_path/metadata.json" >"$metadata_tmp"
mv "$metadata_tmp" "$work_path/metadata.json"
}
write_preview() {
case "$preview_mode" in
auto)
mkdir -p "$work_path/contents/previews"
ffmpeg -i "$tmp_video" -frames:v 1 -loglevel quiet -hide_banner -y "$work_path/contents/previews/splash.png"
log_ok "Preview image generated"
;;
custom)
mkdir -p "$work_path/contents/previews"
cp "$preview_path" "$work_path/contents/previews/splash.png"
log_ok "Preview image configured"
;;
skip)
log_info "Preview image skipped. You can add it later at '$work_path/contents/previews/splash.png'."
;;
esac
}
print_header
check_dependencies
author_name="$(prompt_slug "[1/7] Author name: ")"
work_name="$(prompt_slug "[2/7] Work name: ")"
new_name="$author_name.$work_name"
work_path="$target_path/$new_name"
if [[ -e "$work_path" ]]; then
log_error "'$work_path' already exists. Please remove it or choose another name."
exit 1
fi
video_path="$(prompt_existing_file "[3/7] MP4 video path (enter 'exit' to exit): ")"
log_ok "Video found"
time_range="$(prompt_time_range)"
begin_time="${time_range%%~*}"
end_time="${time_range##*~}"
begin_seconds="$(time_to_seconds "$begin_time")"
end_seconds="$(time_to_seconds "$end_time")"
duration_seconds="$((end_seconds - begin_seconds))"
fps="$(prompt_number "[5/7] FPS (30~120 recommended): ")"
scale_width="$(prompt_number "[6/7] Width scale (1920 recommended): ")"
preview_mode=""
preview_path=""
prompt_preview
confirm_summary
tmp_dir="$(mktemp -d)"
tmp_video="$tmp_dir/tmp_video.mp4"
palette="$tmp_dir/palette.png"
metadata_tmp="$tmp_dir/metadata.json"
log_info "Creating theme directory..."
mkdir -p "$target_path"
cp -r "$template_path" "$work_path"
work_created=1
log_ok "Theme directory created"
log_info "Extracting video clip..."
ffmpeg -ss "$begin_time" -i "$video_path" -t "$duration_seconds" -c:v libx264 -c:a aac -loglevel quiet -hide_banner -y "$tmp_video"
log_ok "Video clip extracted"
log_info "Generating palette..."
ffmpeg -i "$tmp_video" -vf "fps=$fps,scale=$scale_width:-2:flags=lanczos,palettegen" -loglevel quiet -hide_banner -y "$palette"
log_ok "Palette generated"
log_info "Generating GIF..."
ffmpeg -i "$tmp_video" -i "$palette" -lavfi "fps=$fps,scale=$scale_width:-2:flags=lanczos[x];[x][1:v]paletteuse" -loglevel quiet -hide_banner -y "$work_path/contents/splash/images/$work_name.gif"
log_ok "GIF generated"
log_info "Writing theme metadata..."
write_metadata
sed -i "s/gif_file_here/$work_name/" "$work_path/contents/splash/Splash.qml"
log_ok "Theme metadata written"
write_preview
printf '\n'
log_done "Your splash screen has been created:"
printf ' %s\n' "$work_path"