                                        frames_dir + "\n"
                                        "'reformat': Save a copy of alignments "
                                        "file in a\n\tdifferent format. "
                                        "Specify a format with\n\tthe -fmt "
                                        "option.\n"
                                        "'remove': Remove deleted faces from "
                                        "an alignments\n\tfile. The original "
                                        "alignments file will be\n\tbacked up. "
                                        "A different file format for the\n\t"
                                        "alignments file can optionally be\n\t"
 
</s> remove                                       frames_dir + "\n"
 </s> add                                       frames_and_faces_dir + "\n" </s> remove                   "Exiting".format(self.alignments_format))
 </s> add                   "Exiting".format(self.src_format)) </s> remove             print("Alignments file exists at {}".format(self.alignments_file))
 </s> add             print("Alignments file exists at {}".format(self.file))
        return

    def get_source_format(self):
        """ Get the source alignments format """
        if self.file.lower() == "dfl":
            return "dfl"
        return os.path.splitext(self.file)[1].lower() </s> remove class Extract(object):
 </s> add class Reformat():
    """ Reformat Alignment file """
    def __init__(self, alignments, arguments):
        self.verbose = arguments.verbose
        self.alignments = alignments
        self.frames_dir = arguments.frames_dir
        if self.alignments.src_format == "dfl":
            self.frames = Frames(self.frames_dir,
                                 self.verbose).items
            self.faces = Faces(arguments.faces_dir,
                               self.verbose)

    def process(self):
        """ Run reformat """
        print("\n[REFORMAT ALIGNMENTS]")  # Tidy up cli output
        if self.alignments.src_format == "dfl":
            self.alignments.alignments = self.load_dfl()
            self.alignments.file = os.path.join(self.frames_dir,
                                                "alignments.json")
        self.alignments.save_alignments()

    def load_dfl(self):
        """ Load alignments from DeepFaceLab and format for Faceswap """
        alignments = dict()
        frames = {item[0]: item[1]
                  for item in self.frames.values()}
        for face in self.faces.file_list_sorted:
            if not self.validate_dfl(face, frames):
                continue

            fullpath = os.path.join(self.faces.folder, face[0] + face[1])
            dfl = self.get_dfl_alignment(fullpath)

            if not dfl:
                continue

            sourcefile = face[2] + frames[face[2]]
            self.convert_dfl_alignment(dfl, sourcefile, alignments)
        return alignments

    def validate_dfl(self, face, frames):
        """ Validate that current file is good for dfl extraction """
        if face[1] != ".png":
            if self.verbose:
                print("{} is not a png. Skipping".format(face[0] + face[1]))
            return False
        if face[2] not in frames.keys():
            if self.verbose:
                print("{} does not have a matching source frame for {} in "
                      "frames directory. Skipping".format(face[0] + face[1],
                                                          face[2]))
            return False
        return True

    @staticmethod
    def get_dfl_alignment(filename):
        """ Process the alignment of one face """
        with open(filename, "rb") as dfl:
            header = dfl.read(8)
            if header != b"\x89PNG\r\n\x1a\n":
                print("ERROR: No Valid PNG header: {}".format(filename))
                return
            while True:
                chunk_start = dfl.tell()
                chunk_hdr = dfl.read(8)
                if not chunk_hdr:
                    break
                chunk_length, chunk_name = struct.unpack("!I4s", chunk_hdr)
                dfl.seek(chunk_start, os.SEEK_SET)
                if chunk_name == b"fcWp":
                    chunk = dfl.read(chunk_length + 12)
                    return pickle.loads(chunk[8:-4])
                else:
                    dfl.seek(chunk_length+12, os.SEEK_CUR)
            print("ERROR: Couldn't find DFL alignments: {}".format(filename))

    @staticmethod
    def convert_dfl_alignment(dfl_alignments, sourcefile, alignments):
        """ Add DFL Alignments to alignments in Faceswap format """
        if not alignments.get(sourcefile, None):
            alignments[sourcefile] = list()

        left, top, right, bottom = dfl_alignments["source_rect"]
        alignment = {"r": 0,
                     "x": left,
                     "w": right - left,
                     "y": top,
                     "h": bottom - top,
                     "landmarksXY": dfl_alignments["source_landmarks"]}

        alignments[sourcefile].append(alignment)


class Extract(): </s> remove         if not os.path.isfile(self.alignments_file):
 </s> add         if os.path.split(self.file.lower())[1] == "dfl":
            self.file = "dfl"
        if self.file.lower() == "dfl":
            print("Using extracted pngs for alignments")
            return
        if not os.path.isfile(self.file):
                                      "option.\n\tAlignments can be "
                                      "converted from\n\tDeepFaceLab by "
                                      "specifing:\n\t    -a dfl\n\t    -fr "
                                      "<source frame folder>\n\t    -fc "
                                      "<source faces folder>\n"
                                        frames_dir + "\n"
                                        "'reformat': Save a copy of alignments "
                                        "file in a\n\tdifferent format. "
                                        "Specify a format with\n\tthe -fmt "
                                      "option.\n\tAlignments can be "
                                      "converted from\n\tDeepFaceLab by "
                                      "specifing:\n\t    -a dfl\n\t    -fr "
                                      "<source frame folder>\n\t    -fc "
                                      "<source faces folder>\n"
                                        "'remove': Remove deleted faces from "
                                        "an alignments\n\tfile. The original "
                                        "alignments file will be\n\tbacked up. "
                                        "A different file format for the\n\t"
                                        "alignments file can optionally be\n\t"
keep keep keep keep replace keep keep keep keep keep
 Alignments tool - Import from DeepFaceLab (#487)

* Import alignments from DeepFaceLab 
tools/cli.py