Last active
August 29, 2015 14:16
-
-
Save celiacintas/e286fe06ed0d3084e5b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
import cv2 | |
import subprocess | |
import os | |
from time import gmtime, strftime | |
path = 'record_{}'.format(strftime("%Y-%m-%d%H:%M:%S", gmtime())) | |
os.makedirs(path) | |
# command = 'ffmpeg -f x11grab -s 1366x768 -r 12 -i $DISPLAY -f pulse -i default -c:v libx264 -b:v 400k -s 1366x768 -strict -2 {}/pantalla.avi'.format(path) | |
command = 'ffmpeg -f avfoundation -r 12 -i 1:0 -c:v libx264 -b:v 400k {}/pantalla.avi'.format(path) | |
def grab_config(capture): | |
""" | |
config for | |
""" | |
w = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH )) | |
h = int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) | |
# video recorder | |
fourcc = cv2.cv.CV_FOURCC(*'mp4v') # para mac mp4v | |
out = cv2.VideoWriter('{}/cam.avi'.format(path),fourcc, 12.0, (w,h)) | |
return out | |
def video_config(FILENAME): | |
"""Initialize video capture, pass filename by | |
param jic that remove var and pass by argv""" | |
cap = cv2.VideoCapture(FILENAME) | |
while not cap.isOpened(): | |
cap = cv2.VideoCapture(FILENAME) | |
cv2.waitKey(1000) | |
print "Wait for the header" | |
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) | |
print cap.get(cv2.cv.CV_CAP_PROP_FPS) | |
return cap, pos_frame | |
def main(): | |
p = subprocess.Popen(command, shell=True) | |
#output = p.communicate()[0] | |
cap, pos_frame = video_config(0) | |
video_writer = grab_config(cap) | |
while True: | |
flag, frame = cap.read() | |
if flag: | |
video_writer.write(frame) | |
#cv2.imshow('frame',frame) | |
pos_frame = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) | |
else: | |
# The next frame is not ready, so we try to read it again | |
cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, pos_frame-1) | |
print "frame is not ready" | |
cv2.waitKey(1000) | |
if cv2.waitKey(10) == 27: | |
Open = False | |
cap.release() | |
video_writer.release() | |
p.kill() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment