Ask Your Question

Revision history [back]

Videowriter M_Alloc error python 2.7

Hi, I'm trying to extract frames from a video file, and use those frames to write a new video. I managed to extract the frames, save them as *.npy files, and rewrite them again. The following extracts the frames:

cap = cv2.VideoCapture('./' + file_name)
currentFrame = 0

width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)

print width, height, fps

info = open(file_out_path+'info.txt', 'w')
info.write("{0} {1} {2}".format(int(width), int(height), fps))
info.close()

while (True):
    ret, frame = cap.read()
    if not ret:
        break
    if flag:
        frame = cv2.resize(frame, (int(width),int(height)), interpolation=cv2.INTER_AREA)

    np.save(file_out_path + FRAME_OUTPUT + '{0}'.format(currentFrame), frame, False, False)

    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        print 'interrupted'
        print 'frame saved up till break'
        break

    currentFrame += 1

cap.release()
cv2.destroyAllWindows()

which works fine for both 480 * 360 (360p), 1280 * 720 (720p) videos. But when I use the frames to write:

frame_count = len(os.listdir(file_out_path)) - 1
currentFrame = 0

info = open(file_out_path+'/info.txt','r')
info_string = info.readline()
info_list = info_string.split()
info.close()

fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter(FILE_OUTPUT, fourcc, float(info_list[2]), (int(info_list[0]), int(info_list[1])),1)
print int(info_list[0]), int(info_list[1]), float(info_list[2])

while (currentFrame < frame_count):
    frame = ''
    frame = np.load(file_out_path + FRAME_OUTPUT + '{0}.npy'.format(currentFrame), frame, False, False)

    out.write(cv2.flip(frame,1))
    cv2.imshow('frame', frame)
    cv2.imshow('target_frame', cv2.flip(frame, 1))
    if cv2.waitKey(1) & 0xFF == ord('q'):
        print 'interrupted'
        print 'output saved up till break'
        break

    currentFrame += 1

out.release()
cv2.destroyAllWindows()

It works for 360p videos, but not for 720p. Width, height, fps are saved as a text file within the same directory where the frames are located(created before extracting frames), which are accessed to set the parameters of videowriter, so the size aren't altered from the original video. It gives an error message saying:

M_Alloc: error allocing 1094080

I can only assume it's because 720p videos are maybe just to big to write(it works when the frames are resized to a smaller amount), but I couldn't find any other problems about not being able to write 720p videos like I do. Any help would be appreciated.

Thank you!

P.S. Hitting 'q' stops the process and no errors occur, but for 720p videos it doesn't yeild any results at all(empty file), whereas for 360p videos it writes up till the frames were accessed.