-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathedit-file-metadata
executable file
·147 lines (127 loc) · 4.05 KB
/
edit-file-metadata
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
#!/usr/bin/env oil
# A quick script on moving a file with embedded data.
# It can only detect certain files and write the metadata with a certain program.
# For the argument, it needs the path of the files to be moved.
# Dependencies:
# * Oil shell v0.8.8
# * vorbis-tools v1.4.2
# * exiftool v12.16
# * file 5.38
# * coreutils 8.32
shopt --set strict:all
var help_section = "A script for embedding metadata and renaming them files.
edit-file-metadata [options...] FILE
Options:
-h, --help Show the help section.
-a, --author [AUTHOR] Set the author.
-d, --date [DATE] Set the date.
-t, --title [TITLE] Set the title.
--skip Skip the prompt for missing metadata
and only change the needed metadata.
This only works if you've set any
of the metadata in the command line.
--json Return a JSON object on stdout.
--move Move the filename from the kebab-case
of the title.
"
var path = ''
var author = ''
var title = ''
var pub_date = ''
var skip = false
var json_data = false
var move = false
while test $len(ARGV) -gt 0 {
case $[ARGV[0]] {
-h|--help)
echo $help_section
exit 0
;;
-a|--author)
setvar author = ARGV[1]
shift 2
;;
-t|--title)
setvar title = ARGV[1]
shift 2
;;
-d|--date)
setvar pub_date = ARGV[1]
shift 2
;;
--skip)
setvar skip = true
shift
;;
--json)
setvar json_data = true
shift
;;
--move)
setvar move = true
shift
;;
*)
setvar path = ARGV[0]
shift
;;
}
}
proc kebab-case(word) {
# Convert into lower case.
setvar word = $(echo $word | tr '[:upper:]' '[:lower:]')
# What happens to this line:
# * Convert all whitespace and dashes into a single dash.
# * Remove all invalid characters (all that are not alphanumeric characters and dashes).
# * Remove leading and trailing dashes.
setvar word = $(echo $word | sed --regexp-extended --expression 's/\s+|-+/-/g' --expression 's/[^.a-z0-9-]//g' --expression 's/^-+|-+\$//g')
echo $word
}
proc prompt(string, :out, prefix = ">> ") {
>&2 printf "%s\\n%s" $string $prefix
read --line
setref out = $_line
}
proc file_parse(path, :out) {
var extension_regex = / '.' ![ '.' ]+ %end /
var file = {}
setvar file['dir'] = $(dirname $path)
setvar file['name'] = $(basename $path | sed --regexp-extended "s|(.+)${extension_regex}|\\1|")
setvar file['ext'] = $(basename $path | sed --regexp-extended "s|.+(${extension_regex})|\\1|")
setvar file['mime_type'] = $(file --mime-type --brief $path)
setref out = file
}
test -f $path || {
>&2 echo "${path} is not a regular file"
exit 1
}
# Prompt for the missing metadata (if it's configured to not skip, anyways).
if (not skip) {
test $title || prompt "What is the title?" :title
test $author || prompt "Who are the authors?" :author
test $pub_date || prompt "When this file published?" :pub_date
}
# Writing up the metadata time!
# Each file type has a different way of embedding metadata so it needs different tools.
# We'll use exiftool as a catch-all since it supports the most out of... 15 file types I considered.
case $(file --mime-type --brief $path) {
"audio/ogg")
vorbiscomment --write $path --tag "TITLE=${title}" --tag "ARTIST=${author}" --tag "DATE=${pub_date}" ||
opustags $path --in-place --delete-all --add "TITLE=${title}" --add "ARTIST=${author}" --add "DATE=${pub_date}"
;;
*)
exiftool -title="${title}" -author="${author}" -date="${pub_date}" ${path}
;;
}
file_parse $path :file_info
if (json_data) {
var metadata = {}
setvar metadata['file'] = file_info
setvar metadata['title'] = $title
setvar metadata['author'] = $author
setvar metadata['date'] = $pub_date
json write :metadata
}
if (move) {
mv $path "${file_info['dir']}/$(kebab-case $title)${file_info['ext']}"
}