Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _determine_duration(self, fh, skip_tags=False):
# for spec, see https://xiph.org/flac/ogg_mapping.html
header_data = fh.read(4)
while len(header_data):
meta_header = struct.unpack('B3B', header_data)
block_type = meta_header[0] & 0x7f
is_last_block = meta_header[0] & 0x80
size = _bytes_to_int(meta_header[1:4])
# http://xiph.org/flac/format.html#metadata_block_streaminfo
if block_type == Flac.METADATA_STREAMINFO:
stream_info_header = fh.read(size)
if len(stream_info_header) < 34: # invalid streaminfo
return
header = struct.unpack('HH3s3s8B16s', stream_info_header)
# From the ciph documentation:
# py |
# ----------------------------------------------
# H | <16> The minimum block size (in samples)
# H | <16> The maximum block size (in samples)
# 3s | <24> The minimum frame size (in bytes)
# 3s | <24> The maximum frame size (in bytes)
# 8B | <20> Sample rate in Hz.
# | <3> (number of channels)-1.
# | <5> (bits per sample)-1.
# | <36> Total samples in stream.
# 16s| <128> MD5 signature
def get(cls, filename, tags=True, duration=True):
parser_class = None
size = os.path.getsize(filename)
if not size > 0:
return TinyTag(None, 0)
if cls == TinyTag:
"""choose which tag reader should be used by file extension"""
mapping = {
('.mp3',): ID3,
('.oga', '.ogg', '.opus'): Ogg,
('.wav'): Wave,
('.flac'): Flac,
}
for fileextension, tagclass in mapping.items():
if filename.lower().endswith(fileextension):
parser_class = tagclass
else:
# use class on which the method was invoked as parser
parser_class = cls
if parser_class is None:
raise LookupError('No tag reader found to support filetype! ')
with open(filename, 'rb') as af:
tag = parser_class(af, size)
tag.load(tags=tags, duration=duration)
return tag
# min_frm = _bytes_to_int(struct.unpack('3B', min_frm))
# max_frm = _bytes_to_int(struct.unpack('3B', max_frm))
# channels--. bits total samples
# |----- samplerate -----| |-||----| |---------~ ~----|
# 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
# #---4---# #---5---# #---6---# #---7---# #--8-~ ~-12-#
self.samplerate = _bytes_to_int(header[4:7]) >> 4
self.channels = ((header[6] >> 1) & 0x07) + 1
# bit_depth = ((header[6] & 1) << 4) + ((header[7] & 0xF0) >> 4)
# bit_depth = (bit_depth + 1)
total_sample_bytes = [(header[7] & 0x0F)] + list(header[8:12])
total_samples = _bytes_to_int(total_sample_bytes)
self.duration = float(total_samples) / self.samplerate
if self.duration > 0:
self.bitrate = self.filesize / self.duration * 8 / 1024
elif block_type == Flac.METADATA_VORBIS_COMMENT and not skip_tags:
oggtag = Ogg(fh, 0)
oggtag._parse_vorbis_comment(fh)
self.update(oggtag)
elif block_type >= 127:
return # invalid block type
else:
fh.seek(size, 1) # seek over this block
if is_last_block:
return
header_data = fh.read(4)
def _get_parser_for_filename(cls, filename, exception=False):
mapping = {
('.mp3',): ID3,
('.oga', '.ogg', '.opus'): Ogg,
('.wav',): Wave,
('.flac',): Flac,
('.wma',): Wma,
('.m4b', '.m4a', '.mp4'): MP4,
}
for fileextension, tagclass in mapping.items():
if filename.lower().endswith(fileextension):
return tagclass
if exception:
raise TinyTagException('No tag reader found to support filetype! ')