Documentation of Primitives
Notes on CPrimitiveVideo: How to create an appropriate video file with Matlab
On Linux machines, Matlab seems to be capable of two compression methods: no compression or with "Motion JPEG" compression. The advantage of making videos with no compression is that nrec is not capable of handling a Motion JPEG codec, whereas the advantage of making movies with the Motion JPEG codec is that they are more widely playable in standard media players, and they also take up only a small fraction of the amount of space that uncompressed movies take. Uncompressed movies, moreover, sometimes come out "distorted" (e.g., tilted and with improper colors), but this problem seems to be caused by adjusting the figure size with a line like the following: set(gcf,'Position',[50 50 1050 800]);
If one simply leaves the figure size alone, this problem goes away. If one absolutely must change the figure size, then doing so on a Windows or Mac machine sometimes works.
Here is some example code for generating uncompressed or compressed movies:
- Uncompressed:
aviobj = avifile('filename.avi','compression','None','fps',30); for frame_num=1:num_frames figHandle=figure; image(myImage); %this line should be replaced by one which plots the actual desired frame F=getframe(figHandle); aviobj = addframe(aviobj,F); close all end aviobj = close(aviobj);
- Compressed:
aviobj = VideoWriter('filename.avi','Motion JPEG AVI'); open(aviobj) for frame_num=1:num_frames figHandle=figure; image(myImage); %this line should be replaced by one which plots the actual desired frame F=getframe(figHandle); writeVideo(aviobj,F); close all end close(aviobj);
An additional issue is that even if nrec is able to play the movies, it might cut off the movie too soon. This problem seems to be caused by "rounding down" the movie length to the nearest whole second. For example, if the movie is 2.5 seconds, then nrec seems to only play the first two seconds. The easy solution is to simply add enough black frames to the end to bring the video length to the next whole second. For example, if the movie is 2.5 seconds long and the frame rate is 30 frames/sec, then we can add 15 black frames to the end of the movie to make it an even 3 seconds long.
Notes on CPrimitiveVideo: How to convert a movie file:
%% Big files filenameIn=uigetfile('*.mp4','Choose mp4 video file'); filenameOut=strrep(filenameIn,'.mp4','_nrec.avi'); videoIn = VideoReader(filenameIn); videoOut = VideoWriter(filenameOut); open(videoOut) cnt = 0; while hasFrame(videoIn) cnt=cnt+1; frameIn = readFrame(videoIn); frameIn = imresize( frameIn, [600 800], 'cubic'); image(frameIn); title(sprintf('processing frame %d\r\n',cnt)) drawnow writeVideo(videoOut,frameIn); %pause end close(videoOut); close(videoIn);