import os
import cv2
import imageio
import numpy as np
from PIL import Image
import sys
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
def concatenate_frames_horizontally(video1_path, video2_path, output_path):
# Create readers for the input videos
reader1 = imageio.get_reader(video1_path)
reader2 = imageio.get_reader(video2_path)
# Get the minimum number of frames between the two videos
# num_frames = min(len(reader1), len(reader2))-1
# Get the frame rate from the first video
fps = reader1.get_meta_data()['fps']
# Create a writer object for the output video
writer = imageio.get_writer(output_path, fps=fps)
for frame_index in range(num_frames):
# Read frames from each video
frame1 = reader1.get_data(frame_index)
frame2 = reader2.get_data(frame_index)
# Check if the frames have the same height
if frame1.shape[0] != frame2.shape[0]:
# 获取帧的高度和宽度
height1, width1 = frame1.shape[:2]
height2, width2 = frame2.shape[:2]
# 计算帧的面积
area1 = height1 * width1
area2 = height2 * width2
# 确定较小的帧
if area1 <= area2:
target_height, target_width = height1, width1
frame_to_resize = frame2
else:
target_height, target_width = height2, width2
frame_to_resize = frame1
# resized_frame = frame_to_resize.resize((target_width, target_height), Image.ANTIALIAS)
# 缩放帧以匹配较小的帧的尺寸
resized_frame = cv2.resize(frame_to_resize, (target_width, target_height))
# 确定保留的帧
if area1 <= area2:
frame1 = frame1
frame2 = resized_frame
else:
frame1 = resized_frame
frame2 = frame2
# raise ValueError("Frames have different heights, cannot concatenate horizontally")
# Concatenate frames horizontally
concatenated_frame = np.hstack((frame1, frame2))
# Append the concatenated frame to the writer
writer.append_data(concatenated_frame)
# Close readers and writer
reader1.close()
reader2.close()
writer.close()
def process_directories(dir1, dir2, output_dir):
os.makedirs(output_dir,exist_ok=True)
video_files = [f for f in os.listdir(dir1) if f.endswith('.mp4')]
video_files.sort()
# video_files=video_files[:video_len]
for video_file in video_files:
video1_path = os.path.join(dir1, video_file)
video2_path = os.path.join(dir2, video_file)
output_path = os.path.join(output_dir, video_file)
if os.path.exists(video2_path):
concatenate_frames_horizontally(video1_path, video2_path, output_path)
print(f"Processed and saved: {output_path}")
else:
print(f"File {video_file} not found in {dir2}")
if __name__ == "__main__":
num_frames=12
video_len=1000
dir1 = '/lpai/volumes/ad-op-ga/vis/256x448_liauto2000_zero_promt/epoch8_train2000_val3000_fv_step3_4' # Directory containing the first set of videos
dir1 = '/lpai/volumes/ad-op-ga/vis/256x448_liauto3000_zero_promt/epoch13_val_1000_fv_step4_4' # Directory containing the second set of videos
dir1 = '/lpai/volumes/ad-op-ga/code/vis/256x448_liauto100_zero_promt/pose_LW433B128N1025825_o' # Directory containing the second set of videos
dir2 = '/lpai/volumes/ad-op-ga/vis/256x448_liauto3000_zero_promt/epoch16_val_3000_fv_step4_4' # Directory containing the second set of videos
dir2 = '/lpai/volumes/ad-op-ga/code/vis/256x448_liauto100_zero_promt/pose_LW433B128N1025825' # Directory containing the second set of videos
# dir1 = '/lpai/volumes/ad-op-ga/code/inference/inference3/output1/0' # Directory containing the second set of videos
# dir2 = '/lpai/volumes/ad-op-ga/code/inference/inference3/output_clip3000/0' # Directory containing the second set of videos
dir1="/lpai/volumes/ad-op-ga/code/vis/256x448_liauto100_zero_promt/pose_1706251380"
dir2="/lpai/volumes/ad-op-ga/code/vis/256x448_liauto100_zero_promt/pose_1706251380_512"
output_dir = 'out_pinjie_13_16' # Directory to save the concatenated videos
output_dir = 'out_pinjie_8_16' # Directory to save the concatenated videos
output_dir = 'out_pinjie_1706251380' # Directory to save the concatenated videos
process_directories(dir1, dir2, output_dir)