【问题标题】:Concatenate 2 videos into 1 using Python使用 Python 将 2 个视频连接成 1 个视频
【发布时间】:2022-05-03 15:44:51
【问题描述】:

我想编写一个程序,使用 python (cv2) 中的 openCV 监视和跟踪 2 个不同视频中的对象。

我想将两个视频合并为一个视频,然后在该视频上运行一个程序来跟踪对象。

有人可以展示并解释合并它们的说明吗?

我的代码在这里不起作用。它在视频 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()

【问题讨论】:

  • 输入的一些例子?到目前为止,您尝试了什么?
  • 您想在第一个视频之后合并第二个视频还是同时合并两个视频 - 在屏幕的左侧和右侧?对于第一个版本,您可以使用ffmpeg 而不是opencv - 可能您应该像ffmpeg -i one.mp4 -i two.mp4 -o output.mp4 那样在一行中执行此操作(甚至有ffmpeg-python 可以从Python` 执行此操作)。为了合并显示器左右两侧的帧,您必须使用循环读取单个帧,创建新帧并将其保存在新文件中 - 类似于您在任何 opencv 教程中看到的单个视频

标签: python numpy opencv video-processing


【解决方案1】:

FFMPEG 不是我的解决方案...

我改用了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")

【讨论】:

  • 损坏了我的 mp4 文件。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    使用numpy.hstack() 可能更有效。

    import cv2
    import numpy as np
     
    INPUT_FILE1 = 'im.avi'
    INPUT_FILE2 = 'bg.avi'
    OUTPUT_FILE = 'merge.avi'
     
    reader1 = cv2.VideoCapture(INPUT_FILE1)
    reader2 = cv2.VideoCapture(INPUT_FILE2)
    width = int(reader1.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(reader1.get(cv2.CAP_PROP_FRAME_HEIGHT))
    writer = cv2.VideoWriter(OUTPUT_FILE,
                  cv2.VideoWriter_fourcc('I', '4', '2', '0'),
                  30, # fps
                  (width, height//2)) # resolution
     
    print(reader1.isOpened())
    print(reader2.isOpened())
    have_more_frame = True
    c = 0
    while have_more_frame:
        have_more_frame, frame1 = reader1.read()
        _, frame2 = reader2.read()
        frame1 = cv2.resize(frame1, (width//2, height//2))
        frame2 = cv2.resize(frame2, (width//2, height//2))
        img = np.hstack((frame1, frame2))
        cv2.waitKey(1)
        writer.write(img)
        c += 1
        print(str(c) + ' is ok')
     
     
    writer.release()
    reader.release()
    cv2.destroyAllWindows()
    

    【讨论】:

      【解决方案4】:
          import cv2
          import numpy as np
      
          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()
      

      【讨论】:

      • 对这个答案进行一些解释会很好,虽然很好,但只是代码。
      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多