-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstrip-metadata
More file actions
executable file
·308 lines (274 loc) · 7.3 KB
/
strip-metadata
File metadata and controls
executable file
·308 lines (274 loc) · 7.3 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
301
302
303
304
305
306
307
308
#!/bin/bash
# SPDX-FileCopyrightText: 2021 - 2024 sudorook <daemon@nullcodon.com>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
set -Eeuo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
! check_command ffmpeg ffprobe sed && exit 3
function strip_titles {
echo "${1}"
}
function parse_streams {
local data
local codec_type
local codec_name
local lang
local title
local v_counter=0
local a_counter=0
local s_counter=0
local t_counter=0
local c_counter=0
data="$(ffprobe \
${PROBESIZE:+ -probesize ${PROBESIZE}} \
${ANALYZEDURATION:+ -analyzeduration ${ANALYZEDURATION}} \
-v error \
-show_entries stream=index,codec_type,codec_name:stream_tags=language,title \
-print_format compact file:"${IN}")"
while read -r line; do
codec_type="$(echo "${line}" | cut -d"|" -f4 | sed -n "s/^codec_type=\(.*\)/\1/p")"
codec_name="$(echo "${line}" | cut -d"|" -f3 | sed -n "s/^codec_name=\(.*\)/\1/p")"
lang="$(echo "${line}" | cut -d"|" -f5 | sed -n "s/^tag:language=\(.*\)/\1/p")"
title="$(echo "${line}" | cut -d"|" -f6 | sed -n "s/^tag:title=\(.*\)/\1/p")"
case "${codec_type}" in
video)
if [[ "${codec_name}" =~ png|jpg|jpeg ]]; then
STREAMS+=("cover")
COUNTERS+=("${c_counter}")
c_counter=$((c_counter + 1))
else
STREAMS+=("video")
COUNTERS+=("${v_counter}")
v_counter=$((v_counter + 1))
fi
;;
audio)
STREAMS+=("audio")
COUNTERS+=("${a_counter}")
a_counter=$((a_counter + 1))
;;
subtitle)
STREAMS+=("subtitle")
COUNTERS+=("${s_counter}")
s_counter=$((s_counter + 1))
;;
attachment)
STREAMS+=("attachment")
COUNTERS+=("${t_counter}")
s_counter=$((t_counter + 1))
;;
*) ;;
esac
if [ -n "${title}" ]; then
TITLES+=("$(strip_titles "${title}")")
else
TITLES+=("")
fi
if [ -n "${lang}" ]; then
LANGUAGES+=("$(strip_titles "${lang}")")
else
LANGUAGES+=("")
fi
done <<< "${data}"
}
function strip_generic {
local container="${1}"
local codec_type
local idx
local title
local lang
local counter
local cmd="ffmpeg"
cmd="${cmd}${PROBESIZE:+ -probesize ${PROBESIZE}}"
cmd="${cmd}${ANALYZEDURATION:+ -analyzeduration ${ANALYZEDURATION}}"
cmd="${cmd} -v info -i file:${IN@Q} -map_metadata -1 -map_chapters 0 -map 0 -c copy"
for ((idx = 0; idx < "${#STREAMS[@]}"; ++idx)); do
codec_type="${STREAMS[${idx}]}"
title="${TITLES[${idx}]}"
counter="${COUNTERS[${idx}]}"
lang="${LANGUAGES[${idx}]}"
case "${codec_type}" in
video) ;;
audio)
if [[ -n "${lang}" ]]; then
cmd="${cmd} -metadata:s:a:${counter} language=${lang}"
fi
if [[ -n "${title}" ]]; then
cmd="${cmd} -metadata:s:a:${counter} handler=${title}"
fi
;;
subtitle)
if [[ -n "${lang}" ]]; then
cmd="${cmd} -metadata:s:s:${counter} language=${lang}"
fi
if [[ -n "${title}" ]]; then
cmd="${cmd} -metadata:s:s:${counter} handler=${title}"
fi
;;
*) ;;
esac
done
case "${container,,}" in
mkv | matroska)
cmd="${cmd} -f matroska -y ${TMP}"
;;
mp4)
cmd="${cmd} -f mp4 -y ${TMP}"
;;
avi)
cmd="${cmd} -f avi -y ${TMP}"
;;
*)
show_error "ERROR: ${container} not supported. Exiting."
exit 3
;;
esac
show_info "${cmd}" >&2
eval "${cmd}"
if ${KEEP}; then
mv "${IN}" "${BACKUP}"
mv "${TMP}" "${IN}"
else
mv "${IN}" "${BACKUP}"
mv "${TMP}" "${IN}"
rm "${BACKUP}"
fi
}
function strip_matroska {
local codec_type
local idx
local title
local lang
local counter
if ${KEEP}; then
cp "${IN}" "${BACKUP}"
fi
mkvpropedit --tags all: "${IN}"
mkvpropedit -e info -d title "${IN}"
mkvpropedit -e info -d date "${IN}"
for ((idx = 0; idx < "${#STREAMS[@]}"; ++idx)); do
codec_type="${STREAMS[${idx}]}"
title="${TITLES[${idx}]}"
lang="${LANGUAGES[${idx}]}"
counter="${COUNTERS[${idx}]}"
case "${codec_type}" in
video)
if [[ -n "${title}" ]]; then
mkvpropedit -e track:v$((counter + 1)) --set name="${title}" "${IN}"
else
mkvpropedit -e track:v$((counter + 1)) --delete name "${IN}"
fi
;;
audio)
if [[ -n "${title}" ]]; then
mkvpropedit -e track:a$((counter + 1)) --set name="${title}" "${IN}"
else
mkvpropedit -e track:a$((counter + 1)) --delete name "${IN}"
fi
;;
subtitle)
if [[ -n "${title}" ]]; then
mkvpropedit -e track:s$((counter + 1)) --set name="${title}" "${IN}"
else
mkvpropedit -e track:s$((counter + 1)) --delete name "${IN}"
fi
;;
*) ;;
esac
done
}
function strip_image {
if ${KEEP}; then
cp "${IN}" "${BACKUP}"
fi
if command -v magick > /dev/null; then
magick "${IN}" -strip "${TMP}"
mv "${TMP}" "${IN}"
elif command -v convert > /dev/null; then
convert "${IN}" -strip "${TMP}"
mv "${TMP}" "${IN}"
else
show_error "ERROR: imagemagick not found. Exiting."
exit 3
fi
}
IN="${1}"
BACKUP="${IN}_$(date +%Y%m%d-%H%M%S).bak"
TMP="$(mktemp)"
trap 'rm -f "${TMP}" && exit' INT TERM ERR EXIT
STREAMS=()
COUNTERS=()
TITLES=()
LANGUAGES=()
KEEP=false
PROBESIZE=1G
ANALYZEDURATION=1G
OPTIONS=k
LONGOPTIONS=keep
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ "${#}" -ge 1 ]; do
case "${1}" in
-k | --keep)
KEEP=true
shift 1
;;
--)
shift
break
;;
*)
show_error "ERROR: invalid flag."
exit 3
;;
esac
done
if ! [ "${#}" -eq 1 ]; then
show_error "ERROR: invalid # of inputs. Exitng."
exit 3
fi
# Force UTF-8 locale, if possible.
LANG="$(locale -a | grep "utf-\?8" | head -n 1)"
if [ -n "${LANG}" ]; then
export LC_ALL="${LANG}"
else
show_warning "Failed to force UTF-8 locale."
fi
if ! [ -f "${IN}" ]; then
show_error "ERROR: file ${IN@Q} does not exist. Exiting."
exit 3
fi
parse_streams
TYPE="$(file -bi "${IN}")"
if [[ "${TYPE}" =~ matroska|webm ]]; then
if command -v mkvpropedit > /dev/null; then
strip_matroska
else
show_warning "WARNING: mkvtoolnix-cli not found. Defaulting to ffmpeg."
strip_generic "mkv"
fi
elif [[ "${TYPE}" =~ mp4|m4a|m4v|quicktime ]]; then
strip_generic "mp4"
elif [[ "${TYPE}" =~ x-msvideo ]]; then
strip_generic "avi"
elif [[ "${TYPE}" =~ image/jpeg ]]; then
strip_image
else
show_error "ERROR: file type ${TYPE@Q} unsupported. Exiting."
exit 3
fi
sync