[英]Concatenate 2 videos into 1 using Python
I want to write a program that monitors and tracks objects in 2 different videos using openCV in python (cv2).我想编写一个程序,使用 python (cv2) 中的 openCV 监视和跟踪 2 个不同视频中的对象。
I would like to Merge the two videos into 1 video then run a program on that video to track objects.我想将两个视频合并为一个视频,然后在该视频上运行一个程序来跟踪对象。
Could someone please show and explain the instructions behind merging them?有人可以展示并解释合并它们的说明吗?
My code here doesn't Work.我的代码在这里不起作用。 It launches video 2 after the first frame of video 1它在视频 1 的第一帧之后启动视频 2
import cv2
capture = cv2.VideoCapture('p1a_tetris_1.mp4') #tell open cv to use the following video file as input
while capture.isOpened():
ret, frame = capture.read() #capture each frame from the video .
#ret is a boolean to indicate if the
if ret == True :
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # apply gray frame to current frame
cv2.imshow('video Part 1', grayFrame) # shows video in grascale
else :
capture = cv2.VideoCapture('p1a_tetris_2.mp4')
while capture.isOpened():
try:
ret, frame = capture.read()
print(ret)
if ret == True :
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # apply gray frame to current frame
cv2.imshow('Video Part 2', grayFrame) # shows video in grascale
if cv2.waitKey(1) == 27:
break
else :
break
except :
print("error occured")
capture.release() cv2.destroyAllWindows() capture.release() cv2.destroyAllWindows()
FFMPEG was not my solution... FFMPEG 不是我的解决方案...
I used moviepy instead (much simpler btw)我改用moviepy(顺便说一句更简单)
from moviepy.editor import VideoFileClip, concatenate_videoclips
clip_1 = VideoFileClip("p1b_tetris_1.mp4")
clip_2 = VideoFileClip("p1b_tetris_2.mp4")
final_clip = concatenate_videoclips([clip_1,clip_2])
final_clip.write_videofile("final.mp4")
moviepy
has mostly yielded corrupted files for me so here is a just as fast approach with cv2
: moviepy
主要为我生成了损坏的文件,所以这里有一个与cv2
一样快的方法:
# A list of the paths of your videos
videos = ["v1.mp4", "v2.mp4"]
# Create a new video
video = cv2.VideoWriter("new_video.mp4", cv2.VideoWriter_fourcc(*"MPEG"), fps, resolution)
# Write all the frames sequentially to the new video
for v in videos:
curr_v = cv2.VideoCapture(v)
while curr_v.isOpened():
r, frame = curr_v.read() # Get return value and curr frame of curr video
if not r:
break
video.write(frame) # Write the frame
video.release() # Save the video
def merge_video(video1, video2):
camera = cv2.VideoCapture(video1)
cv2.namedWindow("detection", cv2.WINDOW_AUTOSIZE)
sz = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fourcc = cv2.VideoWriter_fourcc(*'mpeg')
vout = cv2.VideoWriter()
vout.open(os.path.join("output.mp4"), fourcc, 20, sz, True)
while True:
res, frame = camera.read()
if not res:
break
cv2.imshow("detection", frame)
# Save the video frame by frame
vout.write(frame)
if cv2.waitKey(110) & 0xff == 27:
break
camera = cv2.VideoCapture(video2)
cv2.namedWindow("detection", cv2.WINDOW_AUTOSIZE)
while True:
res, frame = camera.read()
if not res:
break
cv2.imshow("detection", frame)
# Save the video frame by frame
vout.write(frame)
if cv2.waitKey(110) & 0xff == 27:
break
vout.release()
camera.release()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.