Python实现视频mp4垂直和水平拼接

 更新时间:2025年02月23日 08:53:55   作者:AI算法网奇  
这篇文章主要为大家详细介绍了如何使用Python实现视频mp4垂直和水平拼接功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

视频mp4垂直拼接 水平拼接

pinjie_v.py

import imageio
import numpy as np
import os
import cv2
 
def pinjie_v(dir1,dir2,out_dir):
 
    os.makedirs(out_dir, exist_ok=True)
    # 获取目录下的所有视频文件
    video_files_1 = [f for f in os.listdir(dir1) if f.endswith('.mp4')]
    video_files_2 = [f for f in os.listdir(dir2) if f.endswith('.mp4')]
 
    # 确保两个目录下的视频文件是同名的
    common_files = set(video_files_1).intersection(video_files_2)
 
    # 如果没有同名视频,退出
    if not common_files:
        print("没有同名的视频文件。")
        exit()
 
    for video_name in common_files:
        print(f"处理视频: {video_name}")
 
        # if "user-4fd103ee-38d4-43c5-bb2a-f496d2fe065e" not in video_name:
        #     continue
        # 打开视频文件
        video_path_1 = os.path.join(dir1, video_name)
        video_path_2 = os.path.join(dir2, video_name)
 
        reader1 = imageio.get_reader(video_path_1)
        reader2 = imageio.get_reader(video_path_2)
 
        # 获取视频信息(假设两个视频有相同帧数)
        fps = reader1.get_meta_data()['fps']
        num_frames = min(reader1.count_frames(), reader2.count_frames())
 
        # 创建输出文件
        output_path = os.path.join(out_dir, f"v_{video_name}")
        # writer = imageio.get_writer(output_path, fps=fps)
        if os.path.exists(output_path):
            continue
        outs = []
        # 逐帧处理
        for i in range(num_frames):
            frame1 = reader1.get_data(i)
            frame2 = reader2.get_data(i)
 
            # 获取帧的高度和宽度
            height1, width1, _ = frame1.shape
            height2, width2, _ = frame2.shape
            if height1 > width1:
                if height1 != height2:
                    y_scale = height1 / height2
                    frame2 = cv2.resize(frame2, (int(width2 * y_scale), height1), interpolation=cv2.INTER_AREA)
            elif height1 <= width1:
                if width1 != width2:
                    x_scale = width1 / width2
                    frame2 = cv2.resize(frame2, (width1, int(height2 * x_scale)), interpolation=cv2.INTER_AREA)
 
            if height1 > width1:
                frame = np.hstack([frame1, frame2])
            else:
                frame = np.vstack([frame1, frame2])
 
            outs.append(frame)
        try:
            imageio.mimsave(f'{output_path}', outs, fps=fps, macro_block_size=None)
        except Exception as e:
            print(e)
        # writer.close()
        print(f"视频 {video_name} 拼接完成,保存在 {output_path}")
 
if __name__ == '__main__':
 
    # 设置目录路径
    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)

方法补充

下面小编为大家整理了Python中视频拼接的示例代码,希望对大家有所帮助

#!/user/bin/env python
# coding=utf-8
"""
@project : csdn
@author  : 剑客阿良_ALiang
@file   : concat_video.py
@ide    : PyCharm
@time   : 2021-12-23 15:23:16
"""
  
from ffmpy import FFmpeg
import os
import uuid
import subprocess
  
  
# 视频拼接
def 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 = os.path.join(
        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(ff.cmd)
    ff.run()
    os.remove(_tmp_config)
    return _result_path
  
  
# 构造拼接所需临时文件
def make_tmp_concat_config(video_list: list, output_dir: str):
    _tmp_concat_config_path = os.path.join(output_dir, '{}.txt'.format(uuid.uuid1().hex))
    with open(_tmp_concat_config_path, mode='w', encoding='utf-8') as f:
        f.writelines(list(map(lambda x: 'file {}\n'.format(x), video_list)))
    return _tmp_concat_config_path
  
  
# 校验每个视频的格式
def check_format(video_list: list):
    _video_format = ''
    for x in video_list:
        _ext = os.path.splitext(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
  
  
# 校验每个视频的fps
def 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
  
  
# 获取视频fps
def get_video_fps(video_path: str):
    ext = os.path.splitext(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 = subprocess.Popen(
        ffprobe_cmd.format(video_path),
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        shell=True)
    out, err = p.communicate()
    print("subprocess 执行结果: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'))

到此这篇关于Python实现视频mp4垂直和水平拼接的文章就介绍到这了,更多相关Python视频拼接内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详解Python常用的魔法方法

    详解Python常用的魔法方法

    在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”,中文称『魔术方法』,例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中有相应描述,今天给大家整理了本篇文章,需要的朋友可以参考下
    2021-06-06
  • python扫描proxy并获取可用代理ip的实例

    python扫描proxy并获取可用代理ip的实例

    下面小编就为大家带来一篇python扫描proxy并获取可用代理ip的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-08-08
  • Python实现抓取页面上链接的简单爬虫分享

    Python实现抓取页面上链接的简单爬虫分享

    这篇文章主要介绍了Python实现抓取页面上链接的简单爬虫分享,本文使用了一个开源模块requests实现需求,需要的朋友可以参考下
    2015-01-01
  • TensorFlow tf.nn.conv2d_transpose是怎样实现反卷积的

    TensorFlow tf.nn.conv2d_transpose是怎样实现反卷积的

    这篇文章主要介绍了TensorFlow tf.nn.conv2d_transpose是怎样实现反卷积的,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • 理想高通滤波实现Python opencv示例

    理想高通滤波实现Python opencv示例

    今天小编就为大家分享一篇关于理想高通滤波实现Python opencv示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • Python sep参数使用方法详解

    Python sep参数使用方法详解

    这篇文章主要介绍了Python sep参数使用方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • Python pip安装模块提示错误解决方案

    Python pip安装模块提示错误解决方案

    这篇文章主要介绍了Python pip安装模块提示错误解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • Python批量解压&压缩文件夹的示例代码

    Python批量解压&压缩文件夹的示例代码

    这篇文章主要介绍了利用Python实现批量解压&压缩文件夹的示例代码,文中的实现步骤讲解详细,感兴趣的小伙伴快跟随小编一起动手试一试
    2022-04-04
  • Python编程中如何捕获警告ps不是捕获异常

    Python编程中如何捕获警告ps不是捕获异常

    这篇文章主要为大家介绍了如何使用Python捕获警告,注意:不是捕获异常哦,有需要的朋友可以借鉴参考下,希望能够有所帮助祝大家多多进步
    2021-10-10
  • Python的jsonpath库使用方法实例

    Python的jsonpath库使用方法实例

    这篇文章主要介绍了Python的jsonpath库使用方法实例,接口返回的jsonn数据,需要取值后断言,一般我们是使用jsonpath来提取接口返回的数据 ,JsonPath是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,,需要的朋友可以参考下
    2023-08-08

最新评论