def read_stream(stream, output, prebuffer, chunk_size=8192):
    """Reads data from stream and then writes it to the output."""
    is_player = isinstance(output, PlayerOutput)
    is_http = isinstance(output, HTTPServer)
    is_fifo = is_player and output.namedpipe
    show_progress = isinstance(output, FileOutput) and output.fd is not stdout

    stream_iterator = chain(
        [prebuffer],
        iter(partial(stream.read, chunk_size), b"")
    )
    if show_progress:
        stream_iterator = progress(stream_iterator,
                                   prefix=os.path.basename(args.output))

    try:
        for data in stream_iterator:
            # We need to check if the player process still exists when
            # using named pipes on Windows since the named pipe is not
            # automatically closed by the player.
            if is_win32 and is_fifo:
                output.player.poll()

                if output.player.returncode is not None:
                    console.logger.info("Player closed")
                    break

            try:
                output.write(data)
            except IOError as err:
                if is_player and err.errno in ACCEPTABLE_ERRNO:
                    console.logger.info("Player closed")
                elif is_http and err.errno in ACCEPTABLE_ERRNO:
                    console.logger.info("HTTP connection closed")
                else:
                    console.logger.error("Error when writing to output: {0}", err)

                break
    except IOError as err:
        console.logger.error("Error when reading from stream: {0}", err)

    stream.close()
    console.logger.info("Stream ended")
Reads data from stream and then writes it to the output.
src/livestreamer_cli/main.py