Example #1
0
def reread_f(size):
    global F, f
    pos = f.tell()
    f.seek(F.offset)
    if DEBUG: print "reread_f: now at", f.tell()
    F_offset = F.offset
    F_file = F.file
    F = StringIO(f.read(F.len + size + 10))
    F.file = F_file
    F.offset = F_offset
    f.seek(pos)
Example #2
0
def mp3format(file, warn=1, max_skip=100000, offset=0):
    global F, f, xing, id3v2, warnings, start_offset

    warnings = warn
    start_offset = offset

    f = xing = id3v2 = None
    if DEBUG:
        print "examining", file, "warnings=%i" % warnings, "offset=%i" % start_offset, "max_skip=%i" % max_skip

    # XXX we're using StringIO because it's faster - transitionally.
    if type(file) == types.StringType:
        f = open(file, "r")
    elif hasattr(file, 'seek'):
        f = file
    else:
        print "unknown object:", file
        return None
    f.seek(start_offset)
    search_in = f.read(max_skip)
    F = StringIO(search_in)
    F.file = file
    f.seek(start_offset)
    F.offset = start_offset

    x = syncronize(what=4)

    if not x:
        if warnings:
            print "Warning: no MP3 header found in the first", F.tell(
            ), "bytes of file\n       : \"" + file + "\"."
        return {}

    # we're quite sure we have a valid header now
    if warnings > 1:
        print "Warning: MP3 header found at offset", f.tell() - 4

    # fill in convenience stuff
    x = extend_frameinfo(x)

    # the xing header is normally embedded in the first valid frame
    # but sometimes it appears earlier, I don't know why. Use the 1st.
    if not xing:
        find_xing(x['framesize'])

    if xing:
        x.update(xing)
        if x.has_key('x_frames'):
            x['length_in_samples'] = x['x_frames'] * x['samples_per_frame']
            x['length'] = float(x['length_in_samples']) / x['sfreq']
            # should use the real file size instead of x_bytes.
            if x.has_key("x_bytes"):
                x['bitrate'] = int((x['x_bytes'] * 8.0 / x['length']) / 100 +
                                   0.5)
                x['bitrate'] = x['bitrate'] / 10.0
        else:
            print "strange xing=" + ` xing `
    else:
        x['length'] = stat(file)[ST_SIZE] * 8.0 / (x['bitrate'] * 1000)
    if id3v2:
        x['id3v2'] = id3v2

    if DEBUG > 1: print x
    return x