-
Notifications
You must be signed in to change notification settings - Fork 57
/
coverdump
executable file
·130 lines (108 loc) · 4.78 KB
/
coverdump
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
#!/usr/bin/python
# Audio Tools, a module and set of tools for manipulating audio data
# Copyright (C) 2007-2016 Brian Langenberger
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import os
import os.path
import audiotools
import audiotools.text as _
FILENAME_TYPES = ("front_cover", "back_cover", "leaflet", "media", "other")
if (__name__ == '__main__'):
import argparse
parser = argparse.ArgumentParser(description=_.DESCRIPTION_COVERDUMP)
parser.add_argument("--version",
action="version",
version=audiotools.VERSION_STR)
parser.add_argument("-V", "--verbose",
dest="verbosity",
choices=audiotools.VERBOSITY_LEVELS,
default=audiotools.DEFAULT_VERBOSITY,
help=_.OPT_VERBOSE)
parser.add_argument("-d", "--dir",
dest="dir",
default=".",
help=_.OPT_DIR_IMAGES)
parser.add_argument("-p", "--prefix",
action="store",
dest="prefix",
default="",
help=_.OPT_PREFIX)
parser.add_argument("filenames",
metavar="FILENAME",
nargs="+",
help=_.OPT_INPUT_FILENAME)
options = parser.parse_args()
msg = audiotools.Messenger(options.verbosity == "quiet")
audiofiles = audiotools.open_files(options.filenames,
sorted=False,
messenger=msg)
if len(audiofiles) != 1:
msg.error(_.ERR_1_FILE_REQUIRED)
sys.exit(1)
else:
audiofile = audiofiles[0]
input_filename = audiotools.Filename(audiofile.filename)
metadata = audiofile.get_metadata()
if metadata is not None:
# divide images by type (front cover, leaflet page, etc.)
image_types = {}
for image in metadata.images():
image_types.setdefault(image.type, []).append(image)
# build a set of (Image, Filename) tuples to be extracted
output_images = []
for type, images in image_types.items():
if len(images) != 1:
FILE_TEMPLATE = \
"{prefix}{filename}{filenum:02d}.{suffix}"
else:
FILE_TEMPLATE = \
"{prefix}{filename}.{suffix}"
for i, image in enumerate(images):
output_images.append(
(image,
audiotools.Filename(
os.path.join(
options.dir,
FILE_TEMPLATE.format(
prefix=options.prefix,
filename=FILENAME_TYPES[image.type],
filenum=i + 1,
suffix=image.suffix())))))
# ensure our input file isn't the same
# as any of the proposed files to extract
# (this sounds crazy,
# but there's no technical reason one's audio file
# can't be named "front_cover.jpg"
# even if it's not a JPEG
# so we have to be sure it's not going to be overwritten)
if (input_filename in
[output_filename for
(image, output_filename) in output_images]):
msg.error(_.ERR_OUTPUT_IS_INPUT.format(input_filename))
sys.exit(1)
# finally, write actual image data to disk if possible
for (image, output_filename) in output_images:
try:
audiotools.make_dirs(str(output_filename))
f = open(str(output_filename), "wb")
f.write(image.data)
f.close()
msg.info(_.LAB_ENCODE.format(source=input_filename,
destination=output_filename))
except IOError as e:
msg.error(_.ERR_ENCODING_ERROR.format(output_filename))
sys.exit(1)
except OSError as e:
msg.os_error(e)
sys.exit(1)