You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB
Python

#!/usr/bin/env python
from __future__ import print_function
import cv2, os, sys, time
from argparse import ArgumentParser
p = ArgumentParser("")
p.add_argument("--video", type=int, default=0, help="video, default: 0")
p.add_argument("--output", default=None, help="path to save movie, default: None (show live)")
p.add_argument("--width", type=int, default=640, help="pre-detect resize width")
p.add_argument("--height", type=int, default=480, help="pre-detect resize height")
p.add_argument("--fourcc", default="XVID", help="MJPG,mp4v,XVID")
p.add_argument("--framerate", type=float, default=25, help="output frame rate")
p.add_argument("--show", default=False, action="store_true")
p.add_argument("--time", type=float, default=None)
args = p.parse_args()
fourcc = None
cam = cv2.VideoCapture(args.video)
cam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, args.width)
cam.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, args.height)
if args.output:
try:
fourcc = cv2.cv.CV_FOURCC(*args.fourcc)
except AttributeError:
fourcc = cv2.VideoWriter_fourcc(*args.fourcc)
out = cv2.VideoWriter()
out.open(args.output, fourcc, args.framerate, (args.width, args.height))
else:
out = None
try:
if args.time != None:
start = time.time()
while True:
ret, frame = cam.read()
if out != None:
out.write(frame)
if args.show:
cv2.imshow('display', frame)
if cv2.waitKey(5) & 0xFF == ord('q'):
break
if args.time != None:
elapsed = time.time() - start
if (elapsed >= args.time):
break
except KeyboardInterrupt:
pass
print ("\nCleaning up...")
if out:
out.release()
if args.show:
cv2.destroyAllWindows()