Video mp4 vertical splicing Horizontal splicing
pinjie_v.py
import imageio import numpy as np import os import cv2 def pinjie_v(dir1,dir2,out_dir): (out_dir, exist_ok=True) # Get all video files in the directory video_files_1 = [f for f in (dir1) if ('.mp4')] video_files_2 = [f for f in (dir2) if ('.mp4')] # Make sure that the video files in both directories are of the same name common_files = set(video_files_1).intersection(video_files_2) # If there is no video of the same name, exit if not common_files: print("No video file with the same name.") exit() for video_name in common_files: print(f"Processing videos: {video_name}") # if "user-4fd103ee-38d4-43c5-bb2a-f496d2fe065e" not in video_name: # continue # Open the video file video_path_1 = (dir1, video_name) video_path_2 = (dir2, video_name) reader1 = imageio.get_reader(video_path_1) reader2 = imageio.get_reader(video_path_2) # Get video information (assuming that two videos have the same frame number) fps = reader1.get_meta_data()['fps'] num_frames = min(reader1.count_frames(), reader2.count_frames()) # Create output file output_path = (out_dir, f"v_{video_name}") # writer = imageio.get_writer(output_path, fps=fps) if (output_path): continue outs = [] # Frame-by-frame processing for i in range(num_frames): frame1 = reader1.get_data(i) frame2 = reader2.get_data(i) # Get the height and width of the frame height1, width1, _ = height2, width2, _ = if height1 > width1: if height1 != height2: y_scale = height1 / height2 frame2 = (frame2, (int(width2 * y_scale), height1), interpolation=cv2.INTER_AREA) elif height1 <= width1: if width1 != width2: x_scale = width1 / width2 frame2 = (frame2, (width1, int(height2 * x_scale)), interpolation=cv2.INTER_AREA) if height1 > width1: frame = ([frame1, frame2]) else: frame = ([frame1, frame2]) (frame) try: (f'{output_path}', outs, fps=fps, macro_block_size=None) except Exception as e: print(e) # () print(f"video {video_name} Splicing is completed,Save in {output_path}") if __name__ == '__main__': # Set directory path dir1 = r'E:\project\smpl\render_blender\linux\hmr_res' dir2 = r'E:\project\smpl\render_blender\linux\hmr2_res' dir1 = r'E:\project\smpl\render_blender\linux\val_out_depth_any_color' dir2 = r'E:\project\smpl\render_blender\linux\val_out_video' dir1 = r'E:\project\smpl\render_blender\linux\val_out_depth_any_color' dir2 = r'E:\project\smpl\render_blender\linux\val_out_video' dir1=r'E:\project\smpl\render_blender\linux\test_lbg_o' dir2 =r'E:\project\smpl\render_blender\linux\test_lbg6' out_dir = 'track_diff' pinjie_v(dir1,dir2,out_dir)
Method supplement
#!/user/bin/env python # coding=utf-8 """ @project : csdn @author: Swordsman Aliang_ALiang @file : concat_video.py @ide : PyCharm @time: 2021-12-23 15:23:16 """ from ffmpy import FFmpeg import os import uuid import subprocess # Video stitchingdef concat(video_list: list, output_dir: str): if len(video_list) == 0: raise Exception('video_list can not empty') _ext = check_format(video_list) _fps = check_fps(video_list) _result_path = ( output_dir, '{}{}'.format( uuid.uuid1().hex, _ext)) _tmp_config = make_tmp_concat_config(video_list, output_dir) ff = FFmpeg(inputs={'{}'.format(_tmp_config): '-f concat -safe 0 -y'}, outputs={ _result_path: '-c copy'}) print() () (_tmp_config) return _result_path # Construct temporary files required for splicingdef make_tmp_concat_config(video_list: list, output_dir: str): _tmp_concat_config_path = (output_dir, '{}.txt'.format(uuid.uuid1().hex)) with open(_tmp_concat_config_path, mode='w', encoding='utf-8') as f: (list(map(lambda x: 'file {}\n'.format(x), video_list))) return _tmp_concat_config_path # Verify the format of each videodef check_format(video_list: list): _video_format = '' for x in video_list: _ext = (x)[-1] if _video_format == '' and _ext != '': _video_format = _ext continue if _video_format != '' and _ext == _video_format: continue if _video_format != '' and _ext != _video_format: raise Exception('Inconsistent video format') return _video_format # Verify the fps of each videodef check_fps(video_list: list): _video_fps = 0 for x in video_list: _fps = get_video_fps(x) if _video_fps == 0 and _fps: _video_fps = _fps continue if _video_fps != 0 and _fps == _video_fps: continue if _video_fps != '' and _fps != _video_fps: raise Exception('Inconsistent video fps') if _video_fps == 0: raise Exception('video fps error') return _video_fps # Get video fpsdef get_video_fps(video_path: str): ext = (video_path)[-1] if ext != '.mp4' and ext != '.avi' and ext != '.flv': raise Exception('format not support') ffprobe_cmd = 'ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate {}' p = ( ffprobe_cmd.format(video_path), stdout=, stderr=, shell=True) out, err = () print("subprocess Execution results:out:{} err:{}".format(out, err)) fps_info = str(out, 'utf-8').strip() if fps_info: if fps_info.find("/") > 0: video_fps_str = fps_info.split('/', 1) fps_result = int(int(video_fps_str[0]) / int(video_fps_str[1])) else: fps_result = int(fps_info) else: raise Exception('get fps error') return fps_result if __name__ == '__main__': print(concat(['D:/tmp/100.mp4', 'D:/tmp/101.mp4'], 'C:/Users/huyi/Desktop'))
This is the article about Python's vertical and horizontal splicing of video mp4. For more related Python video splicing content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!