예제 #1
0
    def downscale_frames(self,
                         subdir,
                         max_size,
                         ext,
                         align=16,
                         full_subdir="color_full"):
        full_dir = pjoin(self.path, full_subdir)
        down_dir = pjoin(self.path, subdir)

        mkdir_ifnotexists(down_dir)

        if self.check_frames(down_dir, ext):
            # Frames are already extracted and checked OK.
            return

        for i in range(self.frame_count):
            full_file = "%s/frame_%06d.png" % (full_dir, i)
            down_file = ("%s/frame_%06d." + ext) % (down_dir, i)
            suppress_messages = (i > 0)
            image = image_io.load_image(full_file,
                                        max_size=max_size,
                                        align=align,
                                        suppress_messages=suppress_messages)
            image = image[..., ::-1]  # Channel swizzle

            if ext == "raw":
                image_io.save_raw_float32_image(down_file, image)
            else:
                cv2.imwrite(down_file, image * 255)

        self.check_frames(down_dir, ext)
예제 #2
0
    def extract_pts(self):
        if self.check_extracted_pts():
            # frames.txt exists and checked OK.
            return

        if not os.path.exists(self.video_file):
            sys.exit("ERROR: input video file '%s' not found.",
                     self.video_file)  # 如果视频文件没有找到报错

        # Get width and height
        tmp_file = tempfile.mktemp(".png")
        cmd = "%s -i %s -vframes 1 %s" % (ffmpeg, self.video_file, tmp_file
                                          )  # 此命令的作用为输出视频第1帧
        print(cmd)
        res = os.popen(
            cmd).read()  # 执行命令行 并获得屏幕输出 ffmpeg -i 视频文件路径 -vframes 1 临时文件
        image = image_io.load_image(tmp_file)
        height = image.shape[0]  # 获得视频的高
        width = image.shape[1]  #  获得视频的宽度
        os.remove(tmp_file)
        if os.path.exists(tmp_file):  # 检查临时文件是否存在
            sys.exit("ERROR: unable to remove '%s'" % tmp_file)  # 报告无法删除临时文件

        # Get PTS
        def parse_line(line, token):
            if line[:len(token)] != token:
                sys.exit("ERROR: record is malformed, expected to find '%s'." %
                         token)
            return line[len(token):]

        ffprobe_cmd = "%s %s -select_streams v:0 -show_frames" % (
            ffprobe,
            self.video_file,
        )  # ffprobe 视频地址 -select_streams v:0 -show_frames
        cmd = ffprobe_cmd + " | grep pkt_pts_time"
        print(cmd)
        res = os.popen(cmd).read()
        pts = []
        for line in res.splitlines():
            pts.append(parse_line(
                line, "pkt_pts_time="))  # 获得PTS(Presentation Time Stamp)
            # https: // zhuanlan.zhihu.com / p / 34162672
        self.frame_count = len(pts)
        print("%d frames detected." % self.frame_count)

        pts_file = "%s/frames.txt" % self.path
        with open(pts_file, "w") as file:  # 将得到的pts写入文件
            file.write("%d\n" % len(pts))
            file.write("%s\n" % width)
            file.write("%s\n" % height)
            for t in pts:
                file.write("%s\n" % t)

        self.check_extracted_pts()  #检查frames.txt 文件是否正确
예제 #3
0
    def extract_pts(self):
        if self.check_extracted_pts():
            # frames.txt exists and checked OK.
            return
        # 检查是否存在文件
        if not os.path.exists(self.video_file):
            sys.exit("ERROR: input video file '%s' not found.",
                     self.video_file)

        # Get width and height
        tmp_file = tempfile.mktemp(".png")
        cmd = "%s -i %s -vframes 1 %s" % (ffmpeg, self.video_file, tmp_file)
        print(cmd)
        res = os.popen(cmd).read()
        image = image_io.load_image(tmp_file)
        height = image.shape[0]
        width = image.shape[1]
        os.remove(tmp_file)  # 删除中间文件
        if os.path.exists(tmp_file):
            sys.exit("ERROR: unable to remove '%s'" % tmp_file)

        # Get PTS
        def parse_line(line, token):
            if line[:len(token)] != token:
                sys.exit("ERROR: record is malformed, expected to find '%s'." %
                         token)
            return line[len(token):]

        # 查询视频帧以及关键时间点
        ffprobe_cmd = "%s %s -select_streams v:0 -show_frames" % (
            ffprobe,
            self.video_file,
        )
        cmd = ffprobe_cmd + " | grep pkt_pts_time"
        print(cmd)
        res = os.popen(cmd).read()
        pts = []
        for line in res.splitlines():
            pts.append(parse_line(line, "pkt_pts_time="))
        self.frame_count = len(pts)
        print("%d frames detected." % self.frame_count)
        # 设置帧输出文件
        pts_file = "%s/frames.txt" % self.path
        with open(pts_file, "w") as file:
            file.write("%d\n" % len(pts))
            file.write("%s\n" % width)
            file.write("%s\n" % height)
            for t in pts:
                file.write("%s\n" % t)
        # 再次确认文件
        self.check_extracted_pts()