最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)視頻mp4垂直和水平拼接

 更新時間:2025年02月23日 08:53:55   作者:AI算法網奇  
這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)視頻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)
 
        # 獲取視頻信息(假設兩個視頻有相同幀數(shù))
        fps = reader1.get_meta_data()['fps']
        num_frames = min(reader1.count_frames(), reader2.count_frames())
 
        # 創(chuàng)建輸出文件
        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 執(zhí)行結果: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實現(xiàn)視頻mp4垂直和水平拼接的文章就介紹到這了,更多相關Python視頻拼接內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 用Python配平化學方程式的方法

    用Python配平化學方程式的方法

    在本篇文章中小編給大家整理的是關于用Python配平化學方程式的方法以及相關注意知識點,需要的朋友們參考學習下。
    2019-07-07
  • 在Pytorch中計算自己模型的FLOPs方式

    在Pytorch中計算自己模型的FLOPs方式

    今天小編就為大家分享一篇在Pytorch中計算自己模型的FLOPs方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • TensorFlow實現(xiàn)批量歸一化操作的示例

    TensorFlow實現(xiàn)批量歸一化操作的示例

    這篇文章主要介紹了TensorFlow實現(xiàn)批量歸一化操作的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • 使用Python和OpenCV庫實現(xiàn)實時顏色識別系統(tǒng)

    使用Python和OpenCV庫實現(xiàn)實時顏色識別系統(tǒng)

    這篇文章主要介紹了使用Python和OpenCV庫實現(xiàn)的實時顏色識別系統(tǒng),這個系統(tǒng)能夠通過攝像頭捕捉視頻流,并在視頻中指定區(qū)域內識別主要顏色(紅、黃、綠、藍),這種技術在機器人視覺、自動化檢測和交互式應用中有著廣泛的應用前景,需要的朋友可以參考下
    2025-06-06
  • python與php實現(xiàn)分割文件代碼

    python與php實現(xiàn)分割文件代碼

    本文給大家分享的是兩個分別使用python和php實現(xiàn)的將文件分割成小文件的代碼,非常的實用有需要的小伙伴可以參考下
    2017-03-03
  • python3中替換python2中cmp函數(shù)的實現(xiàn)

    python3中替換python2中cmp函數(shù)的實現(xiàn)

    這篇文章主要介紹了python3替換python2中cmp函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-08-08
  • Python docutils文檔編譯過程方法解析

    Python docutils文檔編譯過程方法解析

    這篇文章主要介紹了Python docutils文檔編譯過程方法解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • python3.5 + PyQt5 +Eric6 實現(xiàn)的一個計算器代碼

    python3.5 + PyQt5 +Eric6 實現(xiàn)的一個計算器代碼

    這篇文章主要介紹了python3.5 + PyQt5 +Eric6 實現(xiàn)的一個計算器代碼,在windows7 32位系統(tǒng)可以完美運行 計算器,有興趣的可以了解一下。
    2017-03-03
  • Python實現(xiàn)NLP的完整流程介紹

    Python實現(xiàn)NLP的完整流程介紹

    這篇文章主要為大家詳細介紹了Python實現(xiàn)NLP的完整流程,文中的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-01-01
  • 使用uv管理Python項目的詳細說明

    使用uv管理Python項目的詳細說明

    uv是Astral推出的Rust驅動Python項目管理工具,支持安裝、初始化、依賴管理與虛擬環(huán)境配置,這篇文章主要介紹了使用uv管理Python項目的詳細說明,需要的朋友可以參考下
    2025-06-06

最新評論

泰顺县| 固阳县| 盈江县| 亚东县| 衡山县| 若尔盖县| 商都县| 福安市| 洞头县| 武强县| 津市市| 崇阳县| 东乌珠穆沁旗| 鱼台县| 肇州县| 申扎县| 新郑市| 定西市| 长沙县| 郴州市| 克什克腾旗| 锡林浩特市| 威远县| 凤翔县| 平谷区| 五寨县| 彭水| 涿鹿县| 衡山县| 长垣县| 德兴市| 沂源县| 湘西| 青冈县| 固原市| 临沭县| 繁昌县| 容城县| 和林格尔县| 收藏| 茌平县|