def fix_character_encoding(input_file, output_file):
    with open(input_file, 'rb') as f_in:
        with open(output_file, 'wb') as f_out:
            for line in f_in:
                # Try to decode with both latin_1 and utf-8
                decoded = [try_decode(line, c) for c in codec_options]

                decoded = [d for d in decoded if d is not None]

                if len(decoded) == 0:
                    # Hopefully at least one codec worked
                    logger.info('Could not decode: %s' % line)
                    sys.exit(1)
                else:
                    # If more than one, choose the codec that gives the best
                    # length
                    chosen_string = shortest_string(decoded)

                    # Write result as ascii, with non-ascii characters escaped
                    f_out.write(chosen_string.encode('utf-8'))