I want to write a video file using two videos side-by-side, but at the end the output file is not playable in any video player. This is my code:
from __future__ import print_function
import numpy as np
import argparse
import cv2
import tkinter as tk
from tkinter import filedialog
import os
import os.path
ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", type=str, default="sample.avi",
help="path to output video file")
ap.add_argument("-f", "--fps", type=int, default=100.0,
help="FPS of output video")
ap.add_argument("-c", "--codec", type=str, default="MJPG",
help="codec of output video")
args = vars(ap.parse_args())
root = tk.Tk()
root.withdraw()
source_video = filedialog.askopenfilename(title="Select file")
sign = filedialog.askopenfilename(title="Select file")
print("[INFO] Capturing video...")
cap1 = cv2.VideoCapture(source_video)
cap2 = cv2.VideoCapture(sign)
fourcc = cv2.VideoWriter_fourcc(*args["codec"])
writer = None
(h, w) = (None, None)
zeros = None
i = 0
try:
while cap1.isOpened():
ret, frame1 = cap1.read()
if ret:
frame1 = cv2.resize(frame1, (0, 0), fx=0.5, fy=0.5)
(h, w) = frame1.shape[:2]
ret, frame2 = cap2.read()
if ret:
frame2 = cv2.resize(frame2, (w, h))
else:
break
if writer is None:
writer = cv2.VideoWriter(args["output"], fourcc, args["fps"],
(h, w*2), True)
zeros = np.zeros((h, w*2), dtype="uint8")
output = np.zeros((h, w*2, 3), dtype="uint8")
if frame2 is None:
output[0:h, 0:w] = frame1
writer.write(output)
else:
output[0:h, 0:w] = frame1
output[0:h, w:w * 2] = frame2
writer.write(output)
cv2.imshow('output', output)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
writer.release()
cv2.destroyAllWindows()
except Exception as e:
print(str(e))
I don't get any error all the things going well but when I try to play output video file from the directory it will not play. I also check file size which is about 16KB. I don't know where the problem exist. please help me. I'm using: Windows 10 64-bit Python3.7 Pycharm as IDE
The frameSize
parameter for VideoWriter
is (width, height), which is somewhat hidden in the docs (e.g., here). So in your code, it should be
writer = cv2.VideoWriter(args["output"], fourcc, args["fps"], (w*2, h), True)
After this fix, your code produced working videos for me.
I noticed that your code "learns" the frame size from the input video but not the frame rate, but I assume this is intentional.