-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprint-fontstring
More file actions
executable file
·205 lines (185 loc) · 5.82 KB
/
print-fontstring
File metadata and controls
executable file
·205 lines (185 loc) · 5.82 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
#!/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 -euo pipefail
ROOT="$(dirname "${0}")"
source "${ROOT}"/globals
# Generate an ffmpeg format string to add all the fonts in a directory to a
# video container. String will specify the stream index numbers.
function make_string_v1 {
local fontstring=""
local fonttype
local count=0
for fontfile in "${1}"/*; do
fonttype="$(file "${fontfile}" |
sed -n "s|^${fontfile}: \([A-Za-z]\+\) [F|f]ont .*|\1|p")"
if [[ "${fonttype}" = "OpenType" ]]; then
fontstring+="-attach \"${fontfile}\" -metadata:s:t:${count} mimetype=application/vnd.ms-opentype "
count=$((count + 1))
elif [[ "${fonttype}" = "TrueType" ]]; then
fontstring+="-attach \"${fontfile}\" -metadata:s:t:${count} mimetype=application/x-truetype-font "
count=$((count + 1))
fi
done
echo "${fontstring}"
}
# Generate an ffmpeg format string to add all the fonts in a directory to a
# video container.
function make_string_v2 {
local fontstring=""
local fonttype
for fontfile in "${1}"/*; do
fonttype="$(file "${fontfile}" |
sed -n "s|^${fontfile}: \([A-Za-z]\+\) [F|f]ont .*|\1|p")"
if [[ "${fonttype}" = "OpenType" ]]; then
fontstring+="-attach \"${fontfile}\" -metadata:s:t mimetype=application/vnd.ms-opentype "
elif [[ "${fonttype}" = "TrueType" ]]; then
fontstring+="-attach \"${fontfile}\" -metadata:s:t mimetype=application/x-truetype-font "
fi
done
echo "${fontstring}"
}
# Generate an ffmpeg format string to add any the fonts in a directory to a
# video container if the font isn't already found it it. String will specify
# the stream index numbers.
#
# FIXME: incremting counts from 0 will result in a string that removes fonts in
# the container and adds those that aren't, which isn't useful.
function make_string_v3 {
local data
local fontstring=""
local fonttype
local fontpath
local fontfile
local count=0
data="$(ffprobe file:"${1}" 2>&1)"
for fontpath in "${2}"/*; do
fontfile="$(basename "${fontpath}")"
fontname="${fontfile%.*}"
fontname="${fontname/-/\.\*}"
fonttype="$(file "${fontpath}" |
sed -n "s|^${fontpath}: \([A-Za-z]\+\) [F|f]ont .*|\1|p")"
if ! [[ "${data}" =~ ${fontname} ]]; then
if [[ "${fonttype}" = "OpenType" ]]; then
fontstring+="-attach \"${fontpath}\" -metadata:s:t:${count} mimetype=application/vnd.ms-opentype "
count=$((count + 1))
elif [[ "${fonttype}" = "TrueType" ]]; then
fontstring+="-attach \"${fontpath}\" -metadata:s:t:${count} mimetype=application/x-truetype-font "
count=$((count + 1))
fi
fi
done
echo "${fontstring}"
}
# Generate an ffmpeg format string to add any the fonts in a directory to a
# video container if the font isn't already found it it. String will specify
# the stream index numbers.
function make_string_v4 {
local data
local fontstring=""
local fonttype
local fontpath
local fontfile
data="$(ffprobe file:"${1}" 2>&1)"
for fontpath in "${2}"/*; do
fontfile="$(basename "${fontpath}")"
fontname="${fontfile%.*}"
fontname="${fontname/-/\.\*}"
fonttype="$(file "${fontpath}" |
sed -n "s|^${fontpath}: \([A-Za-z]\+\) [F|f]ont .*|\1|p")"
if ! [[ "${data}" =~ ${fontname} ]]; then
if [[ "${fonttype}" = "OpenType" ]]; then
fontstring+="-attach \"${fontpath}\" -metadata:s:t mimetype=application/vnd.ms-opentype "
elif [[ "${fonttype}" = "TrueType" ]]; then
fontstring+="-attach \"${fontpath}\" -metadata:s:t mimetype=application/x-truetype-font "
fi
fi
done
echo "${fontstring}"
}
# Generate an mkvpropedit format string to add any the fonts in a directory to a
# video container.
function make_string_mkv {
local fontstring=""
local fontpath
for fontpath in "${1}"/*; do
fontstring="${fontstring} --add-attachment ${fontpath@Q}"
done
echo "${fontstring}"
}
CONTAINER=
OPTIONS=i:d:fc:
LONGOPTIONS=input:,font_dir:,force,container:
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-i | --input)
INPUT="${2}"
shift 2
;;
-d | --font_dir)
FONTDIR="${2}"
shift 2
;;
-f | --force_override)
FORCE=true
shift 1
;;
-c | --container)
CONTAINER=mkv
shift 2
;;
--)
shift
break
;;
*)
show_error "What was that?"
exit 3
;;
esac
done
if ! [[ -v FONTDIR ]]; then
show_error "ERROR: font directory missing. Exiting."
exit 3
fi
if ! [[ -v FORCE ]]; then
FORCE=false
fi
if [[ -v INPUT ]]; then
if [[ -f "${INPUT}" ]]; then
if ${FORCE}; then
show_warning "Why search input fonts when they are to be overridden?"
# make_string_v3 "${INPUT}" "${FONTDIR}"
else
make_string_v4 "${INPUT}" "${FONTDIR}"
fi
else
show_error "ERROR: input ${INPUT@Q} is not a file. Exiting."
fi
else
if [[ "${CONTAINER}" = mkv ]]; then
make_string_mkv "${FONTDIR}"
else
if ${FORCE}; then
make_string_v1 "${FONTDIR}"
else
make_string_v2 "${FONTDIR}"
fi
fi
fi