Skip to content

Instantly share code, notes, and snippets.

@nkint
Created January 23, 2014 10:15
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nkint/8576156 to your computer and use it in GitHub Desktop.
Save nkint/8576156 to your computer and use it in GitHub Desktop.
opencv and python: concatenate video
import numpy as np
import cv2
import os
# this two lines are for loading the videos.
# in this case the video are named as: cut1.mp4, cut2.mp4, ..., cut15.mp4
videofiles = [n for n in os.listdir('.') if n[0]=='c' and n[-4:]=='.mp4']
videofiles = sorted(videofiles, key=lambda item: int( item.partition('.')[0][3:]))
video_index = 0
cap = cv2.VideoCapture(videofiles[0])
# video resolution: 1624x1234 px
out = cv2.VideoWriter("video.avi",
cv2.cv.CV_FOURCC('F','M','P', '4'),
15, (1624, 1234), 1)
while(cap.isOpened()):
ret, frame = cap.read()
if frame is None:
print "end of video " + str(video_index) + " .. next one now"
video_index += 1
if video_index >= len(videofiles):
break
cap = cv2.VideoCapture(videofiles[ video_index ])
ret, frame = cap.read()
cv2.imshow('frame',frame)
out.write(frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()
print "end."
@jordybayo
Copy link

i update the code for opencv4 2020 that is it :files should be like cut1.avi or cut2.avi if you choose to use AVI format

import cv2
import os

# this two lines are for loading the videos.
# in this case the video are named as: cut1.mp4, cut2.mp4, ..., cut15.mp4
# videofiles = [n for n in os.listdir('.') if n[0]=='c' and n[-4:]=='.mp4']
# videofiles = sorted(videofiles, key=lambda item: int( item.partition('.')[0][3:]))


videofiles = [n for n in os.listdir('.') if n[0]=='c' and n[-4:]=='.avi']
videofiles = sorted(videofiles, key=lambda item: int( item.partition('.')[0][3:]))

video_index = 0
cap = cv2.VideoCapture(videofiles[0])

# video resolution: 1624x1234 px
# out = cv2.VideoWriter("video.avi", 
#                       cv2.cv.CV_FOURCC('F','M','P', '4'), 
#                       15, (1624, 1234), 1)

# fourcc = cv2.VideoWriter_fourcc(*'MP4V')
# out = cv2.VideoWriter('cutout.mp4', fourcc, 20, (640, 480))

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('cutout.avi', fourcc, 20.0, (640, 480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if frame is None:
        print ("end of video " + str(video_index) + " .. next one now")
        video_index += 1
        if video_index >= len(videofiles):
            break
        cap = cv2.VideoCapture(videofiles[ video_index ])
        ret, frame = cap.read()
    cv2.imshow('frame',frame)
    out.write(frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
out.release()
cv2.destroyAllWindows()

print ("end.")

@nkint
Copy link
Author

nkint commented Mar 10, 2020

💣 great!!

@dahuachun
Copy link

Help me a lot. Thanks!

@hualili
Copy link

hualili commented Mar 3, 2021

thanks for sharing.

@Adesoji1
Copy link

Adesoji1 commented Apr 2, 2022

Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment