Python實(shí)現(xiàn)多視頻畫(huà)面拼接
利用Python實(shí)現(xiàn)多視頻畫(huà)面拼接
第一行為原始視頻,第二行為分割處理后的視頻,拼接實(shí)現(xiàn)效果如下:
完整代碼
import cv2 import numpy as np import os # 設(shè)置原始視頻文件夾路徑. input_folder = "C:/Users/Administrator/Desktop/output" # 替換為你的輸入文件夾路徑. output_file = 'C:/Users/Administrator/Desktop/combined_video.mp4' # 輸出文件名. # 指定視頻的順序. video_indices = [1, 2, 3] # 假設(shè)你的視頻編號(hào)從1到3. # 收集視頻文件名. original_videos = [] segmentation_videos = [] for index in video_indices: original_videos.append(os.path.join(input_folder, f'resized_test_video{index}.mp4')) segmentation_videos.append(os.path.join(input_folder, f'resized_output_video{index}.mp4')) # 確保視頻數(shù)量匹配. if len(original_videos) != len(segmentation_videos): print("原始視頻和分割視頻數(shù)量不匹配!") exit() # 初始化VideoCapture對(duì)象. caps = [cv2.VideoCapture(v) for v in original_videos + segmentation_videos] # 獲取視頻幀的寬高(確保所有視頻具有相同分辨率). frame_width = int(caps[0].get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(caps[0].get(cv2.CAP_PROP_FRAME_HEIGHT)) # 設(shè)置輸出視頻的參數(shù). output_width = frame_width * 3 output_height = frame_height * 2 fps = caps[0].get(cv2.CAP_PROP_FPS) fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 創(chuàng)建 VideoWriter 對(duì)象. out = cv2.VideoWriter(output_file, fourcc, fps, (output_width, output_height)) while True: frames = [] # 讀取所有視頻的當(dāng)前幀. for cap in caps: ret, frame = cap.read() if not ret: frames.append(None) # 添加空幀以保持一致性. else: # Resize frame if needed frame = cv2.resize(frame, (frame_width, frame_height)) frames.append(frame) # 檢查是否所有視頻都已經(jīng)結(jié)束. if all(frame is None for frame in frames): break # 創(chuàng)建一個(gè)大畫(huà)面,只合并有效幀. valid_frames = [frame for frame in frames if frame is not None] # 檢查是否有足夠的有效幀進(jìn)行合并. if len(valid_frames) == 6: top_row = np.hstack((valid_frames[0], valid_frames[1], valid_frames[2])) bottom_row = np.hstack((valid_frames[3], valid_frames[4], valid_frames[5])) combined_frame = np.vstack((top_row, bottom_row)) # 寫(xiě)入合成幀到輸出文件. out.write(combined_frame) else: print("當(dāng)前幀無(wú)效,無(wú)法進(jìn)行合并。") # 釋放資源. for cap in caps: cap.release() out.release() print('視頻合并完成!輸出文件:', output_file)
# 實(shí)現(xiàn)效果. from IPython.display import Video Video(filename='./輸出效果/combined_video.mp4', width=450, height=330)
處理效果如下
到此這篇關(guān)于Python實(shí)現(xiàn)多視頻畫(huà)面拼接的文章就介紹到這了,更多相關(guān)Python視頻畫(huà)面拼接內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python基于itchat模塊實(shí)現(xiàn)微信防撤回
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)微信防撤回,基于itchat模塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Win7下搭建python開(kāi)發(fā)環(huán)境圖文教程(安裝Python、pip、解釋器)
這篇文章主要為大家分享了Win7下搭建python開(kāi)發(fā)環(huán)境圖文教程,本文主要介紹了安裝Python、pip、解釋器的詳細(xì)步驟,感興趣的小伙伴們可以參考一下2016-05-05Python中根據(jù)時(shí)間自動(dòng)創(chuàng)建文件夾的代碼實(shí)現(xiàn)
這篇文章主要介紹了Python中根據(jù)時(shí)間自動(dòng)創(chuàng)建文件夾的代碼實(shí)現(xiàn),這樣的話(huà)給工作帶來(lái)極大的便利,方便桌面文件按時(shí)間存放,具體實(shí)例代碼跟隨小編一起看看吧2021-10-10如何利用Anaconda配置簡(jiǎn)單的Python環(huán)境
這篇文章主要為大家詳細(xì)介紹了如何利用Anaconda配置簡(jiǎn)單的Python環(huán)境,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06