拼接视频笔记

imageio

opencv

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)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值