def encode_synchsafe_int(i, per_byte):
    """Encode synchsafe integers for ID3v2 tags."""
    if i > 2 ** (per_byte * 4) - 1:
        raise ValueError(
            f'{i} is too large to be represented by a synchsafe integer with {per_byte} bits per byte.'
            )
    v = 0
    mask = 127
    value = i
    while mask ^ 2147483647 > 0:
        v = value & ~mask
        v = v << 8 - per_byte
        v = v | value & mask
        mask = (mask + 1 << 8) - 1
        value = v
    return value.to_bytes(4, byteorder='big')
