import re
import json


def get_bam_library_info(bam):
    """Get the library info from a BAM's comment lines.
    Args:
      bam (pysam.AlignmentFile): BAM file
    Returns:
      list of dicts
    """
    comments = bam.header['CO']
    libraries = []
    for comment in comments:
        m = re.match(r'^library_info:(.+)$', comment)
        if m:
            libraries.append(json.loads(m.group(1)))
    return libraries