I would like to convert this to code, so that I can extract and process the frames being recorded.

Following is what I came up with. This code is not fully working - I see image on preview screen and then everything freezes. The output encoded video file is not generated. It seems that some queue/buffer is getting choked. Please help with any potential issue in my code...