-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcoverart
More file actions
executable file
·131 lines (112 loc) · 2.78 KB
/
coverart
File metadata and controls
executable file
·131 lines (112 loc) · 2.78 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
#!/bin/bash
# SPDX-FileCopyrightText: 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 print_usage() {
show_header "Usage: coverart"
show_listitem "\
coverart add -i|--input <audio file> -c|--cover <image>
coverart rm -i|--input <audio file>
Pass '-k' to keep a backup of the original file."
}
function get_format() {
ffprobe -v error -show_format file:"${INPUT}" |
sed -n "s/^format_name=\(.*\)/\1/p"
}
function add_cover() {
ffmpeg -i file:"${INPUT}" -i file:"${COVER}" \
-map_metadata 0 -map_chapters 0 -map 0 -map 1:0 \
-c copy -f "$(get_format)" -y "${TMP}"
if "${KEEP}"; then
mv "${INPUT}" "${INPUT}_$(date +%Y%m%d-%H%M%S).bak"
fi
mv "${TMP}" "${INPUT}"
sync
}
function rm_cover() {
ffmpeg -i file:"${INPUT}" \
-map_metadata 0 -map_chapters 0 -map 0:a \
-c:a copy -vn -f "$(get_format)" -y "${TMP}"
if "${KEEP}"; then
mv "${INPUT}" "${INPUT}_$(date +%Y%m%d-%H%M%S).bak"
fi
mv "${TMP}" "${INPUT}"
sync
}
KEEP=false
OPTIONS=hc:i:k
LONGOPTIONS=help,cover:,input:,keep
PARSED=$(getopt -o "${OPTIONS}" --long "${LONGOPTIONS}" -n "${0}" -- "${@}")
eval set -- "${PARSED}"
while [ ${#} -ge 1 ]; do
case "${1}" in
-c | --cover)
COVER="${2}"
shift 2
;;
-i | --input)
INPUT="${2}"
shift 2
;;
-k | --keep)
KEEP=true
shift
;;
-h | --help)
print_usage
exit
;;
--)
shift
break
;;
*)
show_error "Error"
exit 3
;;
esac
done
MODE="${1:-}"
if [ "${MODE}" = add ]; then
COVER="${COVER:-cover.jpg}"
if ! [[ -f "${COVER}" ]]; then
show_error "ERROR: cover art not found. Exiting."
exit 3
fi
fi
if ! [ -f "${INPUT:-}" ]; then
show_error "ERROR: input file not found. Exiting."
print_usage
exit 3
fi
TMP="$(mktemp)"
trap 'rm -f "${TMP}"' INT TERM EXIT ERR
case "${MODE}" in
add)
add_cover
;;
rm | remove)
rm_cover
;;
*)
show_error "ERROR: no run mode ('add' or 'rm') specified. Exiting."
exit 3
;;
esac