forked from tinytag/tinytag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinytag.py
1601 lines (1477 loc) · 75.2 KB
/
tinytag.py
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# tinytag - an audio meta info reader
# Copyright (c) 2014-2023 Tom Wallroth
# Copyright (c) 2021-2023 Mat (mathiascode)
#
# Sources on GitHub:
# http://github.com/devsnd/tinytag/
# MIT License
# Copyright (c) 2014-2023 Tom Wallroth, Mat (mathiascode)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import division, print_function
from collections import OrderedDict, defaultdict
try:
from collections.abc import MutableMapping
except ImportError:
from collections import MutableMapping
from functools import reduce
from io import BytesIO
import base64
import codecs
import io
import json
import operator
import os
import re
import struct
import sys
DEBUG = os.environ.get('DEBUG', False) # some of the parsers can print debug info
class TinyTagException(LookupError): # inherit LookupError for backwards compat
pass
def _read(fh, nbytes): # helper function to check if we haven't reached EOF
b = fh.read(nbytes)
if len(b) < nbytes:
raise TinyTagException('Unexpected end of file')
return b
def stderr(*args):
sys.stderr.write('%s\n' % ' '.join(repr(arg) for arg in args))
sys.stderr.flush()
def _bytes_to_int_le(b):
fmt = {1: '<B', 2: '<H', 4: '<I', 8: '<Q'}.get(len(b))
return struct.unpack(fmt, b)[0] if fmt is not None else 0
def _bytes_to_int(b):
return reduce(lambda accu, elem: (accu << 8) + elem, b, 0)
class TinyTag(object):
SUPPORTED_FILE_EXTENSIONS = [
'.mp1', '.mp2', '.mp3',
'.oga', '.ogg', '.opus', '.spx',
'.wav', '.flac', '.wma',
'.m4b', '.m4a', '.m4r', '.m4v', '.mp4', '.aax', '.aaxc',
'.aiff', '.aifc', '.aif', '.afc',
'.ape'
]
_file_extension_mapping = None
_magic_bytes_mapping = None
def __init__(self, filehandler, filesize, ignore_errors=False):
# This is required for compatibility between python2 and python3
# in python2 there is a difference between `str` and `unicode`
# whereas in python3 everything every string is `unicode` by default and
# the type `unicode` is deprecated
if type(filehandler).__name__ in ('str', 'unicode'):
raise Exception('Use `TinyTag.get(filepath)` instead of `TinyTag(filepath)`')
self._filehandler = filehandler
self._filename = None # for debugging purposes
self._default_encoding = None # allow override for some file formats
self.filesize = filesize
self.album = None
self.albumartist = None
self.artist = None
self.audio_offset = None
self.bitrate = None
self.channels = None
self.comment = None
self.composer = None
self.disc = None
self.disc_total = None
self.duration = None
self.extra = defaultdict(lambda: None)
self.genre = None
self.samplerate = None
self.bitdepth = None
self.title = None
self.track = None
self.track_total = None
self.year = None
self._parse_tags = True
self._parse_duration = True
self._load_image = False
self._image_data = None
self._ignore_errors = ignore_errors
def as_dict(self):
return {k: v for k, v in self.__dict__.items() if not k.startswith('_')}
@classmethod
def is_supported(cls, filename):
return cls._get_parser_for_filename(filename) is not None
def get_image(self):
return self._image_data
@classmethod
def _get_parser_for_filename(cls, filename):
if cls._file_extension_mapping is None:
cls._file_extension_mapping = {
(b'.mp1', b'.mp2', b'.mp3'): ID3,
(b'.oga', b'.ogg', b'.opus', b'.spx'): Ogg,
(b'.wav',): Wave,
(b'.flac',): Flac,
(b'.wma',): Wma,
(b'.m4b', b'.m4a', b'.m4r', b'.m4v', b'.mp4', b'.aax', b'.aaxc'): MP4,
(b'.aiff', b'.aifc', b'.aif', b'.afc'): Aiff,
(b'.ape',): APE,
}
if not isinstance(filename, bytes): # convert filename to binary
try:
filename = filename.encode('ASCII', errors='ignore')
except AttributeError:
filename = bytes(filename) # pathlib
filename = filename.lower()
for ext, tagclass in cls._file_extension_mapping.items():
if filename.endswith(ext):
return tagclass
@classmethod
def _get_parser_for_file_handle(cls, fh):
# https://en.wikipedia.org/wiki/List_of_file_signatures
if cls._magic_bytes_mapping is None:
cls._magic_bytes_mapping = {
b'^ID3': ID3,
b'^\xff\xfb': ID3,
b'^OggS.........................FLAC': Ogg,
b'^OggS........................Opus': Ogg,
b'^OggS........................Speex': Ogg,
b'^OggS.........................vorbis': Ogg,
b'^RIFF....WAVE': Wave,
b'^fLaC': Flac,
b'^\x30\x26\xB2\x75\x8E\x66\xCF\x11\xA6\xD9\x00\xAA\x00\x62\xCE\x6C': Wma,
b'....ftypM4A': MP4, # https://www.file-recovery.com/m4a-signature-format.htm
b'....ftypaax': MP4, # Audible proprietary M4A container
b'....ftypaaxc': MP4, # Audible proprietary M4A container
b'\xff\xf1': MP4, # https://www.garykessler.net/library/file_sigs.html
b'^FORM....AIFF': Aiff,
b'^FORM....AIFC': Aiff,
b'^MAC ': APE,
}
header = fh.peek(max(len(sig) for sig in cls._magic_bytes_mapping))
for magic, parser in cls._magic_bytes_mapping.items():
if re.match(magic, header):
return parser
@classmethod
def get_parser_class(cls, filename=None, filehandle=None):
if cls != TinyTag: # if `get` is invoked on TinyTag, find parser by ext
return cls # otherwise use the class on which `get` was invoked
if filename:
parser_class = cls._get_parser_for_filename(filename)
if parser_class is not None:
return parser_class
# try determining the file type by magic byte header
if filehandle:
parser_class = cls._get_parser_for_file_handle(filehandle)
if parser_class is not None:
return parser_class
raise TinyTagException('No tag reader found to support filetype! ')
@classmethod
def get(cls, filename=None, tags=True, duration=True, image=False,
ignore_errors=False, encoding=None, file_obj=None):
should_open_file = (file_obj is None)
if should_open_file:
file_obj = io.open(filename, 'rb')
elif isinstance(file_obj, io.BytesIO):
file_obj = io.BufferedReader(file_obj) # buffered reader to support peeking
try:
file_obj.seek(0, os.SEEK_END)
filesize = file_obj.tell()
file_obj.seek(0)
if filesize <= 0:
return TinyTag(None, filesize)
parser_class = cls.get_parser_class(filename, file_obj)
tag = parser_class(file_obj, filesize, ignore_errors=ignore_errors)
tag._filename = filename
tag._default_encoding = encoding
tag.load(tags=tags, duration=duration, image=image)
tag.extra = dict(tag.extra) # turn default dict into dict so that it can throw KeyError
return tag
finally:
if should_open_file:
file_obj.close()
def __str__(self):
return json.dumps(OrderedDict(sorted(self.as_dict().items())))
def __repr__(self):
return str(self)
def load(self, tags, duration, image=False):
self._parse_tags = tags
self._parse_duration = duration
self._load_image = image
if tags:
self._parse_tag(self._filehandler)
if duration:
if tags: # rewind file if the tags were already parsed
self._filehandler.seek(0)
self._determine_duration(self._filehandler)
def _set_field(self, fieldname, value, overwrite=True):
"""convenience function to set fields of the tinytag by name"""
write_dest = self # write into the TinyTag by default
get_func = getattr
set_func = setattr
is_extra = fieldname.startswith('extra.') # but if it's marked as extra field
if is_extra:
fieldname = fieldname[6:]
write_dest = self.extra # write into the extra field instead
get_func = operator.getitem
set_func = operator.setitem
if get_func(write_dest, fieldname): # do not overwrite existing data
return
if DEBUG:
stderr('Setting field "%s" to "%s"' % (fieldname, value))
if fieldname == 'genre':
genre_id = 255
if value.isdigit(): # funky: id3v1 genre hidden in a id3v2 field
genre_id = int(value)
else: # funkier: the TCO may contain genres in parens, e.g. '(13)'
if value[:1] == '(' and value[-1:] == ')' and value[1:-1].isdigit():
genre_id = int(value[1:-1])
if 0 <= genre_id < len(ID3.ID3V1_GENRES):
value = ID3.ID3V1_GENRES[genre_id]
if fieldname in ("track", "disc", "track_total", "disc_total"):
# Converting to string for type consistency
value = str(value)
mapping = [(fieldname, value)]
if fieldname in ("track", "disc"):
if type(value).__name__ in ('str', 'unicode') and '/' in value:
value, total = value.split('/')[:2]
mapping = [(fieldname, str(value)), ("%s_total" % fieldname, str(total))]
for k, v in mapping:
if overwrite or not get_func(write_dest, k):
set_func(write_dest, k, v)
def _determine_duration(self, fh):
raise NotImplementedError()
def _parse_tag(self, fh):
raise NotImplementedError()
def update(self, other, all_fields=False):
# update the values of this tag with the values from another tag
if all_fields:
self.__dict__.update(other.__dict__)
return
for key in ['track', 'track_total', 'title', 'artist',
'album', 'albumartist', 'year', 'duration',
'genre', 'disc', 'disc_total', 'comment', 'composer',
'_image_data']:
if not getattr(self, key) and getattr(other, key):
setattr(self, key, getattr(other, key))
if other.extra:
self.extra.update(other.extra)
@staticmethod
def _unpad(s):
# strings in mp3 and asf *may* be terminated with a zero byte at the end
return s.strip('\x00')
class MP4(TinyTag):
# https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html
# https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html
class Parser:
# https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/Metadata/Metadata.html#//apple_ref/doc/uid/TP40000939-CH1-SW34
ATOM_DECODER_BY_TYPE = {
0: lambda x: x, # 'reserved',
1: lambda x: codecs.decode(x, 'utf-8', 'replace'), # UTF-8
2: lambda x: codecs.decode(x, 'utf-16', 'replace'), # UTF-16
3: lambda x: codecs.decode(x, 's/jis', 'replace'), # S/JIS
# 16: duration in millis
13: lambda x: x, # JPEG
14: lambda x: x, # PNG
21: lambda x: struct.unpack('>b', x)[0], # BE Signed int
22: lambda x: struct.unpack('>B', x)[0], # BE Unsigned int
23: lambda x: struct.unpack('>f', x)[0], # BE Float32
24: lambda x: struct.unpack('>d', x)[0], # BE Float64
# 27: lambda x: x, # BMP
# 28: lambda x: x, # QuickTime Metadata atom
65: lambda x: struct.unpack('b', x)[0], # 8-bit Signed int
66: lambda x: struct.unpack('>h', x)[0], # BE 16-bit Signed int
67: lambda x: struct.unpack('>i', x)[0], # BE 32-bit Signed int
74: lambda x: struct.unpack('>q', x)[0], # BE 64-bit Signed int
75: lambda x: struct.unpack('B', x)[0], # 8-bit Unsigned int
76: lambda x: struct.unpack('>H', x)[0], # BE 16-bit Unsigned int
77: lambda x: struct.unpack('>I', x)[0], # BE 32-bit Unsigned int
78: lambda x: struct.unpack('>Q', x)[0], # BE 64-bit Unsigned int
}
@classmethod
def make_data_atom_parser(cls, fieldname):
def parse_data_atom(data_atom):
data_type = struct.unpack('>I', data_atom[:4])[0]
conversion = cls.ATOM_DECODER_BY_TYPE.get(data_type)
if conversion is None:
stderr('Cannot convert data type: %s' % data_type)
return {} # don't know how to convert data atom
# skip header & null-bytes, convert rest
return {fieldname: conversion(data_atom[8:])}
return parse_data_atom
@classmethod
def make_number_parser(cls, fieldname1, fieldname2):
def _(data_atom):
number_data = data_atom[8:14]
numbers = struct.unpack('>HHH', number_data)
# for some reason the first number is always irrelevant.
return {fieldname1: numbers[1], fieldname2: numbers[2]}
return _
@classmethod
def parse_id3v1_genre(cls, data_atom):
# dunno why the genre is offset by -1 but that's how mutagen does it
idx = struct.unpack('>H', data_atom[8:])[0] - 1
if idx < len(ID3.ID3V1_GENRES):
return {'genre': ID3.ID3V1_GENRES[idx]}
return {'genre': None}
@classmethod
def read_extended_descriptor(cls, esds_atom):
for i in range(4):
if esds_atom.read(1) != b'\x80':
break
@classmethod
def parse_audio_sample_entry_mp4a(cls, data):
# this atom also contains the esds atom:
# https://ffmpeg.org/doxygen/0.6/mov_8c-source.html
# http://xhelmboyx.tripod.com/formats/mp4-layout.txt
# http://sasperger.tistory.com/103
datafh = BytesIO(data)
datafh.seek(16, os.SEEK_CUR) # jump over version and flags
channels = struct.unpack('>H', datafh.read(2))[0]
datafh.seek(2, os.SEEK_CUR) # jump over bit_depth
datafh.seek(2, os.SEEK_CUR) # jump over QT compr id & pkt size
sr = struct.unpack('>I', datafh.read(4))[0]
# ES Description Atom
esds_atom_size = struct.unpack('>I', data[28:32])[0]
esds_atom = BytesIO(data[36:36 + esds_atom_size])
esds_atom.seek(5, os.SEEK_CUR) # jump over version, flags and tag
# ES Descriptor
cls.read_extended_descriptor(esds_atom)
esds_atom.seek(4, os.SEEK_CUR) # jump over ES id, flags and tag
# Decoder Config Descriptor
cls.read_extended_descriptor(esds_atom)
esds_atom.seek(9, os.SEEK_CUR)
avg_br = struct.unpack('>I', esds_atom.read(4))[0] / 1000 # kbit/s
return {'channels': channels, 'samplerate': sr, 'bitrate': avg_br}
@classmethod
def parse_audio_sample_entry_alac(cls, data):
# https://github.com/macosforge/alac/blob/master/ALACMagicCookieDescription.txt
alac_atom_size = struct.unpack('>I', data[28:32])[0]
alac_atom = BytesIO(data[36:36 + alac_atom_size])
alac_atom.seek(9, os.SEEK_CUR)
bitdepth = struct.unpack('b', alac_atom.read(1))[0]
alac_atom.seek(3, os.SEEK_CUR)
channels = struct.unpack('b', alac_atom.read(1))[0]
alac_atom.seek(6, os.SEEK_CUR)
avg_br = struct.unpack('>I', alac_atom.read(4))[0] / 1000 # kbit/s
sr = struct.unpack('>I', alac_atom.read(4))[0]
return {'channels': channels, 'samplerate': sr, 'bitrate': avg_br, 'bitdepth': bitdepth}
@classmethod
def parse_mvhd(cls, data):
# http://stackoverflow.com/a/3639993/1191373
walker = BytesIO(data)
version = struct.unpack('b', walker.read(1))[0]
walker.seek(3, os.SEEK_CUR) # jump over flags
if version == 0: # uses 32 bit integers for timestamps
walker.seek(8, os.SEEK_CUR) # jump over create & mod times
time_scale = struct.unpack('>I', walker.read(4))[0]
duration = struct.unpack('>I', walker.read(4))[0]
else: # version == 1: # uses 64 bit integers for timestamps
walker.seek(16, os.SEEK_CUR) # jump over create & mod times
time_scale = struct.unpack('>I', walker.read(4))[0]
duration = struct.unpack('>q', walker.read(8))[0]
return {'duration': duration / time_scale}
@classmethod
def debug_atom(cls, data):
stderr(data) # use this function to inspect atoms in an atom tree
return {}
# The parser tree: Each key is an atom name which is traversed if existing.
# Leaves of the parser tree are callables which receive the atom data.
# callables return {fieldname: value} which is updates the TinyTag.
META_DATA_TREE = {b'moov': {b'udta': {b'meta': {b'ilst': {
# see: http://atomicparsley.sourceforge.net/mpeg-4files.html
# and: https://metacpan.org/dist/Image-ExifTool/source/lib/Image/ExifTool/QuickTime.pm#L3093
b'\xa9ART': {b'data': Parser.make_data_atom_parser('artist')},
b'\xa9alb': {b'data': Parser.make_data_atom_parser('album')},
b'\xa9cmt': {b'data': Parser.make_data_atom_parser('comment')},
# need test-data for this
# b'cpil': {b'data': Parser.make_data_atom_parser('extra.compilation')},
b'\xa9day': {b'data': Parser.make_data_atom_parser('year')},
b'\xa9des': {b'data': Parser.make_data_atom_parser('extra.description')},
b'\xa9gen': {b'data': Parser.make_data_atom_parser('genre')},
b'\xa9lyr': {b'data': Parser.make_data_atom_parser('extra.lyrics')},
b'\xa9mvn': {b'data': Parser.make_data_atom_parser('movement')},
b'\xa9nam': {b'data': Parser.make_data_atom_parser('title')},
b'\xa9wrt': {b'data': Parser.make_data_atom_parser('composer')},
b'aART': {b'data': Parser.make_data_atom_parser('albumartist')},
b'cprt': {b'data': Parser.make_data_atom_parser('extra.copyright')},
b'desc': {b'data': Parser.make_data_atom_parser('extra.description')},
b'disk': {b'data': Parser.make_number_parser('disc', 'disc_total')},
b'gnre': {b'data': Parser.parse_id3v1_genre},
b'trkn': {b'data': Parser.make_number_parser('track', 'track_total')},
# need test-data for this
# b'tmpo': {b'data': Parser.make_data_atom_parser('extra.bmp')},
}}}}}
# see: https://developer.apple.com/library/mac/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html
AUDIO_DATA_TREE = {
b'moov': {
b'mvhd': Parser.parse_mvhd,
b'trak': {b'mdia': {b"minf": {b"stbl": {b"stsd": {
b'mp4a': Parser.parse_audio_sample_entry_mp4a,
b'alac': Parser.parse_audio_sample_entry_alac
}}}}}
}
}
IMAGE_DATA_TREE = {b'moov': {b'udta': {b'meta': {b'ilst': {
b'covr': {b'data': Parser.make_data_atom_parser('_image_data')},
}}}}}
VERSIONED_ATOMS = {b'meta', b'stsd'} # those have an extra 4 byte header
FLAGGED_ATOMS = {b'stsd'} # these also have an extra 4 byte header
def _determine_duration(self, fh):
self._traverse_atoms(fh, path=self.AUDIO_DATA_TREE)
def _parse_tag(self, fh):
self._traverse_atoms(fh, path=self.META_DATA_TREE)
if self._load_image: # A bit inefficient, we rewind the file
self._filehandler.seek(0) # to parse it again for the image
self._traverse_atoms(fh, path=self.IMAGE_DATA_TREE)
def _traverse_atoms(self, fh, path, stop_pos=None, curr_path=None):
header_size = 8
atom_header = fh.read(header_size)
while len(atom_header) == header_size:
atom_size = struct.unpack('>I', atom_header[:4])[0] - header_size
atom_type = atom_header[4:]
if curr_path is None: # keep track how we traversed in the tree
curr_path = [atom_type]
if atom_size <= 0: # empty atom, jump to next one
atom_header = fh.read(header_size)
continue
if DEBUG:
stderr('%s pos: %d atom: %s len: %d' %
(' ' * 4 * len(curr_path), fh.tell() - header_size, atom_type,
atom_size + header_size))
if atom_type in self.VERSIONED_ATOMS: # jump atom version for now
fh.seek(4, os.SEEK_CUR)
if atom_type in self.FLAGGED_ATOMS: # jump atom flags for now
fh.seek(4, os.SEEK_CUR)
sub_path = path.get(atom_type, None)
# if the path leaf is a dict, traverse deeper into the tree:
if issubclass(type(sub_path), MutableMapping):
atom_end_pos = fh.tell() + atom_size
self._traverse_atoms(fh, path=sub_path, stop_pos=atom_end_pos,
curr_path=curr_path + [atom_type])
# if the path-leaf is a callable, call it on the atom data
elif callable(sub_path):
for fieldname, value in sub_path(fh.read(atom_size)).items():
if DEBUG:
stderr(' ' * 4 * len(curr_path), 'FIELD: ', fieldname)
if fieldname:
self._set_field(fieldname, value)
# if no action was specified using dict or callable, jump over atom
else:
fh.seek(atom_size, os.SEEK_CUR)
# check if we have reached the end of this branch:
if stop_pos and fh.tell() >= stop_pos:
return # return to parent (next parent node in tree)
atom_header = fh.read(header_size) # read next atom
class ID3(TinyTag):
FRAME_ID_TO_FIELD = { # Mapping from Frame ID to a field of the TinyTag
'COMM': 'comment', 'COM': 'comment',
'TRCK': 'track', 'TRK': 'track',
'TYER': 'year', 'TYE': 'year', 'TDRC': 'year',
'TALB': 'album', 'TAL': 'album',
'TPE1': 'artist', 'TP1': 'artist',
'TIT2': 'title', 'TT2': 'title',
'TCON': 'genre', 'TCO': 'genre',
'TPOS': 'disc',
'TPE2': 'albumartist', 'TCOM': 'composer',
'WXXX': 'extra.url',
'TSRC': 'extra.isrc',
'TXXX': 'extra.text',
'TKEY': 'extra.initial_key',
'USLT': 'extra.lyrics',
'TCOP': 'extra.copyright', 'TCR': 'extra.copyright',
}
IMAGE_FRAME_IDS = {'APIC', 'PIC'}
PARSABLE_FRAME_IDS = set(FRAME_ID_TO_FIELD.keys()).union(IMAGE_FRAME_IDS)
_MAX_ESTIMATION_SEC = 30
_CBR_DETECTION_FRAME_COUNT = 5
_USE_XING_HEADER = True # much faster, but can be deactivated for testing
ID3V1_GENRES = [
'Blues', 'Classic Rock', 'Country', 'Dance', 'Disco',
'Funk', 'Grunge', 'Hip-Hop', 'Jazz', 'Metal', 'New Age', 'Oldies',
'Other', 'Pop', 'R&B', 'Rap', 'Reggae', 'Rock', 'Techno', 'Industrial',
'Alternative', 'Ska', 'Death Metal', 'Pranks', 'Soundtrack',
'Euro-Techno', 'Ambient', 'Trip-Hop', 'Vocal', 'Jazz+Funk', 'Fusion',
'Trance', 'Classical', 'Instrumental', 'Acid', 'House', 'Game',
'Sound Clip', 'Gospel', 'Noise', 'AlternRock', 'Bass', 'Soul', 'Punk',
'Space', 'Meditative', 'Instrumental Pop', 'Instrumental Rock',
'Ethnic', 'Gothic', 'Darkwave', 'Techno-Industrial', 'Electronic',
'Pop-Folk', 'Eurodance', 'Dream', 'Southern Rock', 'Comedy', 'Cult',
'Gangsta', 'Top 40', 'Christian Rap', 'Pop/Funk', 'Jungle',
'Native American', 'Cabaret', 'New Wave', 'Psychadelic', 'Rave',
'Showtunes', 'Trailer', 'Lo-Fi', 'Tribal', 'Acid Punk', 'Acid Jazz',
'Polka', 'Retro', 'Musical', 'Rock & Roll', 'Hard Rock',
# Wimamp Extended Genres
'Folk', 'Folk-Rock', 'National Folk', 'Swing', 'Fast Fusion', 'Bebob',
'Latin', 'Revival', 'Celtic', 'Bluegrass', 'Avantgarde', 'Gothic Rock',
'Progressive Rock', 'Psychedelic Rock', 'Symphonic Rock', 'Slow Rock',
'Big Band', 'Chorus', 'Easy Listening', 'Acoustic', 'Humour', 'Speech',
'Chanson', 'Opera', 'Chamber Music', 'Sonata', 'Symphony', 'Booty Bass',
'Primus', 'Porn Groove', 'Satire', 'Slow Jam', 'Club', 'Tango', 'Samba',
'Folklore', 'Ballad', 'Power Ballad', 'Rhythmic Soul', 'Freestyle',
'Duet', 'Punk Rock', 'Drum Solo', 'A capella', 'Euro-House',
'Dance Hall', 'Goa', 'Drum & Bass',
# according to https://de.wikipedia.org/wiki/Liste_der_ID3v1-Genres:
'Club-House', 'Hardcore Techno', 'Terror', 'Indie', 'BritPop',
'', # don't use ethnic slur ("Negerpunk", WTF!)
'Polsk Punk', 'Beat', 'Christian Gangsta Rap', 'Heavy Metal',
'Black Metal', 'Contemporary Christian', 'Christian Rock',
# WinAmp 1.91
'Merengue', 'Salsa', 'Thrash Metal', 'Anime', 'Jpop', 'Synthpop',
# WinAmp 5.6
'Abstract', 'Art Rock', 'Baroque', 'Bhangra', 'Big Beat', 'Breakbeat',
'Chillout', 'Downtempo', 'Dub', 'EBM', 'Eclectic', 'Electro',
'Electroclash', 'Emo', 'Experimental', 'Garage', 'Illbient',
'Industro-Goth', 'Jam Band', 'Krautrock', 'Leftfield', 'Lounge',
'Math Rock', 'New Romantic', 'Nu-Breakz', 'Post-Punk', 'Post-Rock',
'Psytrance', 'Shoegaze', 'Space Rock', 'Trop Rock', 'World Music',
'Neoclassical', 'Audiobook', 'Audio Theatre', 'Neue Deutsche Welle',
'Podcast', 'Indie Rock', 'G-Funk', 'Dubstep', 'Garage Rock', 'Psybient',
]
def __init__(self, filehandler, filesize, *args, **kwargs):
TinyTag.__init__(self, filehandler, filesize, *args, **kwargs)
# save position after the ID3 tag for duration measurement speedup
self._bytepos_after_id3v2 = None
@classmethod
def set_estimation_precision(cls, estimation_in_seconds):
cls._MAX_ESTIMATION_SEC = estimation_in_seconds
# see this page for the magic values used in mp3:
# http://www.mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm
samplerates = [
[11025, 12000, 8000], # MPEG 2.5
[], # reserved
[22050, 24000, 16000], # MPEG 2
[44100, 48000, 32000], # MPEG 1
]
v1l1 = [0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0]
v1l2 = [0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0]
v1l3 = [0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0]
v2l1 = [0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0]
v2l2 = [0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0]
v2l3 = v2l2
bitrate_by_version_by_layer = [
[None, v2l3, v2l2, v2l1], # MPEG Version 2.5 # note that the layers go
None, # reserved # from 3 to 1 by design.
[None, v2l3, v2l2, v2l1], # MPEG Version 2 # the first layer id is
[None, v1l3, v1l2, v1l1], # MPEG Version 1 # reserved
]
samples_per_frame = 1152 # the default frame size for mp3
channels_per_channel_mode = [
2, # 00 Stereo
2, # 01 Joint stereo (Stereo)
2, # 10 Dual channel (2 mono channels)
1, # 11 Single channel (Mono)
]
@staticmethod
def _parse_xing_header(fh):
# see: http://www.mp3-tech.org/programmer/sources/vbrheadersdk.zip
fh.seek(4, os.SEEK_CUR) # read over Xing header
header_flags = struct.unpack('>i', fh.read(4))[0]
frames = byte_count = toc = vbr_scale = None
if header_flags & 1: # FRAMES FLAG
frames = struct.unpack('>i', fh.read(4))[0]
if header_flags & 2: # BYTES FLAG
byte_count = struct.unpack('>i', fh.read(4))[0]
if header_flags & 4: # TOC FLAG
toc = [struct.unpack('>i', fh.read(4))[0] for _ in range(25)] # 100 bytes
if header_flags & 8: # VBR SCALE FLAG
vbr_scale = struct.unpack('>i', fh.read(4))[0]
return frames, byte_count, toc, vbr_scale
def _determine_duration(self, fh):
# if tag reading was disabled, find start position of audio data
if self._bytepos_after_id3v2 is None:
self._parse_id3v2_header(fh)
max_estimation_frames = (ID3._MAX_ESTIMATION_SEC * 44100) // ID3.samples_per_frame
frame_size_accu = 0
header_bytes = 4
frames = 0 # count frames for determining mp3 duration
bitrate_accu = 0 # add up bitrates to find average bitrate to detect
last_bitrates = [] # CBR mp3s (multiple frames with same bitrates)
# seek to first position after id3 tag (speedup for large header)
fh.seek(self._bytepos_after_id3v2)
while True:
# reading through garbage until 11 '1' sync-bits are found
b = fh.peek(4)
if len(b) < 4:
if frames:
self.bitrate = bitrate_accu / frames
break # EOF
sync, conf, bitrate_freq, rest = struct.unpack('BBBB', b[0:4])
br_id = (bitrate_freq >> 4) & 0x0F # biterate id
sr_id = (bitrate_freq >> 2) & 0x03 # sample rate id
padding = 1 if bitrate_freq & 0x02 > 0 else 0
mpeg_id = (conf >> 3) & 0x03
layer_id = (conf >> 1) & 0x03
channel_mode = (rest >> 6) & 0x03
# check for eleven 1s, validate bitrate and sample rate
if (not b[:2] > b'\xFF\xE0' or br_id > 14 or br_id == 0 or sr_id == 3
or layer_id == 0 or mpeg_id == 1): # noqa
idx = b.find(b'\xFF', 1) # invalid frame, find next sync header
if idx == -1:
idx = len(b) # not found: jump over the current peek buffer
fh.seek(max(idx, 1), os.SEEK_CUR)
continue
try:
self.channels = self.channels_per_channel_mode[channel_mode]
frame_bitrate = ID3.bitrate_by_version_by_layer[mpeg_id][layer_id][br_id]
self.samplerate = ID3.samplerates[mpeg_id][sr_id]
except (IndexError, TypeError):
raise TinyTagException('mp3 parsing failed')
# There might be a xing header in the first frame that contains
# all the info we need, otherwise parse multiple frames to find the
# accurate average bitrate
if frames == 0 and ID3._USE_XING_HEADER:
xing_header_offset = b.find(b'Xing')
if xing_header_offset != -1:
fh.seek(xing_header_offset, os.SEEK_CUR)
xframes, byte_count, toc, vbr_scale = ID3._parse_xing_header(fh)
if xframes and xframes != 0 and byte_count:
# MPEG-2 Audio Layer III uses 576 samples per frame
samples_per_frame = 576 if mpeg_id <= 2 else ID3.samples_per_frame
self.duration = xframes * samples_per_frame / float(self.samplerate)
# self.duration = (xframes * ID3.samples_per_frame / self.samplerate
# / self.channels) # noqa
self.bitrate = byte_count * 8 / self.duration / 1000
self.audio_offset = fh.tell()
return
continue
frames += 1 # it's most probably an mp3 frame
bitrate_accu += frame_bitrate
if frames == 1:
self.audio_offset = fh.tell()
if frames <= ID3._CBR_DETECTION_FRAME_COUNT:
last_bitrates.append(frame_bitrate)
fh.seek(4, os.SEEK_CUR) # jump over peeked bytes
frame_length = (144000 * frame_bitrate) // self.samplerate + padding
frame_size_accu += frame_length
# if bitrate does not change over time its probably CBR
is_cbr = (frames == ID3._CBR_DETECTION_FRAME_COUNT and len(set(last_bitrates)) == 1)
if frames == max_estimation_frames or is_cbr:
# try to estimate duration
fh.seek(-128, 2) # jump to last byte (leaving out id3v1 tag)
audio_stream_size = fh.tell() - self.audio_offset
est_frame_count = audio_stream_size / (frame_size_accu / frames)
samples = est_frame_count * ID3.samples_per_frame
self.duration = samples / self.samplerate
self.bitrate = bitrate_accu / frames
return
if frame_length > 1: # jump over current frame body
fh.seek(frame_length - header_bytes, os.SEEK_CUR)
if self.samplerate:
self.duration = frames * ID3.samples_per_frame / self.samplerate
def _parse_tag(self, fh):
self._parse_id3v2(fh)
attrs = ['track', 'track_total', 'title', 'artist', 'album', 'albumartist', 'year', 'genre']
has_all_tags = all(getattr(self, attr) for attr in attrs)
if not has_all_tags and self.filesize > 128:
fh.seek(-128, os.SEEK_END) # try parsing id3v1 in last 128 bytes
self._parse_id3v1(fh)
def _parse_id3v2_header(self, fh):
size, extended, major = 0, None, None
# for info on the specs, see: http://id3.org/Developer%20Information
header = struct.unpack('3sBBB4B', _read(fh, 10))
tag = codecs.decode(header[0], 'ISO-8859-1')
# check if there is an ID3v2 tag at the beginning of the file
if tag == 'ID3':
major, rev = header[1:3]
if DEBUG:
stderr('Found id3 v2.%s' % major)
# unsync = (header[3] & 0x80) > 0
extended = (header[3] & 0x40) > 0
# experimental = (header[3] & 0x20) > 0
# footer = (header[3] & 0x10) > 0
size = self._calc_size(header[4:8], 7)
self._bytepos_after_id3v2 = size
return size, extended, major
def _parse_id3v2(self, fh):
size, extended, major = self._parse_id3v2_header(fh)
if size:
end_pos = fh.tell() + size
parsed_size = 0
if extended: # just read over the extended header.
size_bytes = struct.unpack('4B', _read(fh, 6)[0:4])
extd_size = self._calc_size(size_bytes, 7)
fh.seek(extd_size - 6, os.SEEK_CUR) # jump over extended_header
while parsed_size < size:
frame_size = self._parse_frame(fh, id3version=major)
if frame_size == 0:
break
parsed_size += frame_size
fh.seek(end_pos, os.SEEK_SET)
def _parse_id3v1(self, fh):
if fh.read(3) == b'TAG': # check if this is an ID3 v1 tag
def asciidecode(x):
return self._unpad(codecs.decode(x, self._default_encoding or 'latin1'))
fields = fh.read(30 + 30 + 30 + 4 + 30 + 1)
self._set_field('title', asciidecode(fields[:30]), overwrite=False)
self._set_field('artist', asciidecode(fields[30:60]), overwrite=False)
self._set_field('album', asciidecode(fields[60:90]), overwrite=False)
self._set_field('year', asciidecode(fields[90:94]), overwrite=False)
comment = fields[94:124]
if b'\x00\x00' < comment[-2:] < b'\x01\x00':
self._set_field('track', str(ord(comment[-1:])), overwrite=False)
comment = comment[:-2]
self._set_field('comment', asciidecode(comment), overwrite=False)
genre_id = ord(fields[124:125])
if genre_id < len(ID3.ID3V1_GENRES):
self._set_field('genre', ID3.ID3V1_GENRES[genre_id], overwrite=False)
@staticmethod
def index_utf16(s, search):
for i in range(0, len(s), len(search)):
if s[i:i + len(search)] == search:
return i
return -1
def _parse_frame(self, fh, id3version=False):
# ID3v2.2 especially ugly. see: http://id3.org/id3v2-00
frame_header_size = 6 if id3version == 2 else 10
frame_size_bytes = 3 if id3version == 2 else 4
binformat = '3s3B' if id3version == 2 else '4s4B2B'
bits_per_byte = 7 if id3version == 4 else 8 # only id3v2.4 is synchsafe
frame_header_data = fh.read(frame_header_size)
if len(frame_header_data) != frame_header_size:
return 0
frame = struct.unpack(binformat, frame_header_data)
frame_id = self._decode_string(frame[0])
frame_size = self._calc_size(frame[1:1 + frame_size_bytes], bits_per_byte)
if DEBUG:
stderr('Found id3 Frame %s at %d-%d of %d' %
(frame_id, fh.tell(), fh.tell() + frame_size, self.filesize))
if frame_size > 0:
# flags = frame[1+frame_size_bytes:] # dont care about flags.
if frame_id not in ID3.PARSABLE_FRAME_IDS: # jump over unparsable frames
fh.seek(frame_size, os.SEEK_CUR)
return frame_size
content = fh.read(frame_size)
fieldname = ID3.FRAME_ID_TO_FIELD.get(frame_id)
if fieldname:
language = fieldname in ("comment", "extra.lyrics")
self._set_field(fieldname, self._decode_string(content, language))
elif frame_id in self.IMAGE_FRAME_IDS and self._load_image:
# See section 4.14: http://id3.org/id3v2.4.0-frames
encoding = content[0:1]
if frame_id == 'PIC': # ID3 v2.2:
desc_start_pos = 1 + 3 + 1 # skip encoding (1), imgformat (3), pictype(1)
else: # ID3 v2.3+
desc_start_pos = content.index(b'\x00', 1) + 1 + 1 # skip mimetype, pictype(1)
# latin1 and utf-8 are 1 byte
termination = b'\x00' if encoding in (b'\x00', b'\x03') else b'\x00\x00'
desc_length = ID3.index_utf16(content[desc_start_pos:], termination)
desc_end_pos = desc_start_pos + desc_length + len(termination)
self._image_data = content[desc_end_pos:]
return frame_size
return 0
def _decode_string(self, bytestr, language=False):
default_encoding = 'ISO-8859-1'
if self._default_encoding:
default_encoding = self._default_encoding
try: # it's not my fault, this is the spec.
first_byte = bytestr[:1]
if first_byte == b'\x00': # ISO-8859-1
bytestr = bytestr[1:]
encoding = default_encoding
elif first_byte == b'\x01': # UTF-16 with BOM
bytestr = bytestr[1:]
# remove language (but leave BOM)
if language:
if bytestr[3:5] in (b'\xfe\xff', b'\xff\xfe'):
bytestr = bytestr[3:]
if bytestr[:3].isalpha() and bytestr[3:4] == b'\x00':
bytestr = bytestr[4:] # remove language
if bytestr[:1] == b'\x00':
bytestr = bytestr[1:] # strip optional additional null byte
# read byte order mark to determine endianness
encoding = 'UTF-16be' if bytestr[0:2] == b'\xfe\xff' else 'UTF-16le'
# strip the bom if it exists
if bytestr[:2] in (b'\xfe\xff', b'\xff\xfe'):
bytestr = bytestr[2:] if len(bytestr) % 2 == 0 else bytestr[2:-1]
# remove ADDITIONAL EXTRA BOM :facepalm:
if bytestr[:4] == b'\x00\x00\xff\xfe':
bytestr = bytestr[4:]
elif first_byte == b'\x02': # UTF-16LE
# strip optional null byte, if byte count uneven
bytestr = bytestr[1:-1] if len(bytestr) % 2 == 0 else bytestr[1:]
encoding = 'UTF-16le'
elif first_byte == b'\x03': # UTF-8
bytestr = bytestr[1:]
encoding = 'UTF-8'
else:
bytestr = bytestr
encoding = default_encoding # wild guess
if language and bytestr[:3].isalpha() and bytestr[3:4] == b'\x00':
bytestr = bytestr[4:] # remove language
errors = 'ignore' if self._ignore_errors else 'strict'
return self._unpad(codecs.decode(bytestr, encoding, errors))
except UnicodeDecodeError:
raise TinyTagException('Error decoding ID3 Tag!')
def _calc_size(self, bytestr, bits_per_byte):
# length of some mp3 header fields is described by 7 or 8-bit-bytes
return reduce(lambda accu, elem: (accu << bits_per_byte) + elem, bytestr, 0)
class Ogg(TinyTag):
def __init__(self, filehandler, filesize, *args, **kwargs):
TinyTag.__init__(self, filehandler, filesize, *args, **kwargs)
self._tags_parsed = False
self._max_samplenum = 0 # maximum sample position ever read
def _determine_duration(self, fh):
max_page_size = 65536 # https://xiph.org/ogg/doc/libogg/ogg_page.html
if not self._tags_parsed:
self._parse_tag(fh) # determine sample rate
fh.seek(0) # and rewind to start
if self.duration is not None or not self.samplerate:
return # either ogg flac or invalid file
if self.filesize > max_page_size:
fh.seek(-max_page_size, 2) # go to last possible page position
while True:
b = fh.peek(4)
if len(b) == 0:
return # EOF
if b[:4] == b'OggS': # look for an ogg header
for _ in self._parse_pages(fh):
pass # parse all remaining pages
self.duration = self._max_samplenum / self.samplerate
else:
idx = b.find(b'OggS') # try to find header in peeked data
seekpos = idx if idx != -1 else len(b) - 3
fh.seek(max(seekpos, 1), os.SEEK_CUR)
def _parse_tag(self, fh):
page_start_pos = fh.tell() # set audio_offset later if its audio data
check_flac_second_packet = False
check_speex_second_packet = False
for packet in self._parse_pages(fh):
walker = BytesIO(packet)
if packet[0:7] == b"\x01vorbis":
if self._parse_duration:
(channels, self.samplerate, max_bitrate, bitrate,
min_bitrate) = struct.unpack("<B4i", packet[11:28])
if not self.audio_offset:
self.bitrate = bitrate / 1000
self.audio_offset = page_start_pos
elif packet[0:7] == b"\x03vorbis":
if self._parse_tags:
walker.seek(7, os.SEEK_CUR) # jump over header name
self._parse_vorbis_comment(walker)
elif packet[0:8] == b'OpusHead':
if self._parse_duration: # parse opus header
# https://www.videolan.org/developers/vlc/modules/codec/opus_header.c
# https://mf4.xiph.org/jenkins/view/opus/job/opusfile-unix/ws/doc/html/structOpusHead.html
walker.seek(8, os.SEEK_CUR) # jump over header name
(version, ch, _, sr, _, _) = struct.unpack("<BBHIHB", walker.read(11))
if (version & 0xF0) == 0: # only major version 0 supported
self.channels = ch
self.samplerate = 48000 # internally opus always uses 48khz
elif packet[0:8] == b'OpusTags':
if self._parse_tags: # parse opus metadata:
walker.seek(8, os.SEEK_CUR) # jump over header name
self._parse_vorbis_comment(walker)
elif packet[0:5] == b'\x7fFLAC':
# https://xiph.org/flac/ogg_mapping.html
walker.seek(9, os.SEEK_CUR) # jump over header name, version and number of headers
flactag = Flac(io.BufferedReader(walker), self.filesize)
flactag.load(tags=self._parse_tags, duration=self._parse_duration,
image=self._load_image)
self.update(flactag, all_fields=True)
check_flac_second_packet = True
elif check_flac_second_packet:
# second packet contains FLAC metadata block
if self._parse_tags:
meta_header = struct.unpack('B3B', walker.read(4))
block_type = meta_header[0] & 0x7f
if block_type == Flac.METADATA_VORBIS_COMMENT:
self._parse_vorbis_comment(walker)
check_flac_second_packet = False
elif packet[0:8] == b'Speex ':
# https://speex.org/docs/manual/speex-manual/node8.html
if self._parse_duration:
walker.seek(36, os.SEEK_CUR) # jump over header name and irrelevant fields
(self.samplerate, _, _, self.channels,
self.bitrate) = struct.unpack("<5i", walker.read(20))
check_speex_second_packet = True
elif check_speex_second_packet:
if self._parse_tags:
length = struct.unpack('I', walker.read(4))[0] # starts with a comment string
comment = codecs.decode(walker.read(length), 'UTF-8')
self._set_field('comment', comment)
self._parse_vorbis_comment(walker, contains_vendor=False) # other tags
check_speex_second_packet = False
else:
if DEBUG:
stderr('Unsupported Ogg page type: ', packet[:16])
break
page_start_pos = fh.tell()
self._tags_parsed = True
def _parse_vorbis_comment(self, fh, contains_vendor=True):
# for the spec, see: http://xiph.org/vorbis/doc/v-comment.html
# discnumber tag based on: https://en.wikipedia.org/wiki/Vorbis_comment
# https://sno.phy.queensu.ca/~phil/exiftool/TagNames/Vorbis.html
comment_type_to_attr_mapping = {
'album': 'album',
'albumartist': 'albumartist',
'title': 'title',
'artist': 'artist',
'author': 'artist',