<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def movie(args):
 """ %prog movie input.bed scaffolds.fasta chr1 Visualize history of scaffold OO. The history is contained within the tourfile, generated by path(). For each historical scaffold OO, the program plots a separate PDF file. The plots can be combined to show the progression as a little animation. The third argument limits the plotting to a specific pseudomolecule, for example `chr1`. """

    p = OptionParser(movie.__doc__)
    p.add_option("--gapsize", default=100, type="int",
                 help="Insert gaps of size between scaffolds")
    add_allmaps_plot_options(p)
    opts, args = p.parse_args(args)

    if len(args) != 3:
        sys.exit(not p.print_help())

    inputbed, scaffoldsfasta, seqid = args
    gapsize = opts.gapsize
    pf = inputbed.rsplit(".", 1)[0]
    agpfile = pf + ".chr.agp"
    tourfile = pf + ".tour"

    fp = open(tourfile)
    sizes = Sizes(scaffoldsfasta).mapping
    ffmpeg = "ffmpeg"
    mkdir(ffmpeg)
    score = cur_score = None
    i = 1
    for header, block in read_block(fp, ">"):
        s, tag, label = header[1:].split()
        if s != seqid:
            continue
        tour = block[0].split()
        tour = [(x[:-1], x[-1]) for x in tour]
        if label.startswith("GA"):
            cur_score = label.split("-")[-1]
            if cur_score == score:
                i += 1
                continue
            score = cur_score

        image_name = ".".join((seqid, "{0:04d}".format(i), label, "pdf"))
        if need_update(tourfile, image_name):
            fwagp = must_open(agpfile, "w")
            order_to_agp(seqid, tour, sizes, fwagp, gapsize=gapsize,
                         gaptype="map")
            fwagp.close()
            logging.debug("{0} written to `{1}`".format(header, agpfile))
            build([inputbed, scaffoldsfasta, "--cleanup"])
            pdf_name = plot([inputbed, seqid, "--title={0}".format(label)])
            sh("mv {0} {1}".format(pdf_name, image_name))
        if label in ("INIT", "FLIP", "TSP", "FINAL"):
            for j in xrange(5):  # Delay for 5 frames
                image_delay = image_name.rsplit(".", 1)[0] + \
                              ".d{0}.pdf".format(j)
                sh("cp {0} {1}/{2}".format(image_name, ffmpeg, image_delay))
        else:
            sh("cp {0} {1}/".format(image_name, ffmpeg))
        i += 1

    make_movie(ffmpeg, pf)