import cv2 from pathlib import Path import tempfile import os if __name__ == "__main__": # file list flist = [ "B-bigger", "B-bigger2", "B-forward-open", "B-good", "B-NO", "B-open", "L-bad", "L-forward-open", "L-good", "L-handle", "L-little", "L-number-1", "L-number-2", "L-number-3", "L-OK", "L-up-open", "L-up-point2", "R-bad", "R-forward-open", "R-good", "R-handle", "R-little", "R-number-1", "R-number-2", "R-number-3", "R-OK", "R-up-open", "R-up-point2", ] flist = [ "XBG-2-mix", "XBG-3-mix", "XBG-4-mix" ] path_data = Path("/mfs/lsen/DATA/Actor6/bg/motion-gesture-fitting2") path_save = Path("/mfs/lsen/DATA/Actor6/bg/motion-gesture-fitting2-shift") path_save.mkdir(parents=True, exist_ok=True) for ifname in flist: ipath_video = path_data.joinpath(ifname + ".mp4") if not ipath_video.exists(): print(f"{ifname} not existing .... -> {ipath_video}") continue print(f"processing .... {ipath_video}") # load frames vidcap = cv2.VideoCapture(str(ipath_video)) N = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT)) video_width = int(vidcap.get(cv2.CAP_PROP_FRAME_WIDTH)) video_height = int(vidcap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # data_load = np.zeros((N, video_height, video_width, 3), dtype = np.uint8) # create save video ipath_video_save = path_save.joinpath(ifname + ".mp4") file_tmp = tempfile.NamedTemporaryFile(suffix='.mp4') writer = cv2.VideoWriter(str(file_tmp.name), cv2.VideoWriter_fourcc(*'mp4v'), 50, (video_width, video_height), True) # writer = cv2.VideoWriter(str(ipath_video_save), cv2.VideoWriter_fourcc(*'avc1'), 50, (video_width, video_height), True) # print(frames) for i in range(N): success, frame = vidcap.read() if not success: print(f"frame_{i}: can not read frame!") continue # make shift # (H, W) --> (H+3, W+43) | (H+3, W+31) # (H+3, W+43) frame[3:, 31:] = frame[:-3, :-31] # add to writer writer.write(frame) # write video writer.release() # convert to h264 print('save video') cmd = f"ffmpeg -y -i {str(file_tmp.name)} -vcodec libx264 {str(ipath_video_save)}" os.system(cmd)