Differences between revisions 2 and 5 (spanning 3 versions)
Revision 2 as of 2012-11-09 09:24:47
Size: 2426
Editor: hih-kn-2301-01
Comment:
Revision 5 as of 2017-08-03 08:18:47
Size: 3039
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
 * Notes on CPrimitiveVideo: How to create an appropriate video file with Matlab == Notes on CPrimitiveVideo: How to create an appropriate video file with Matlab ==
Line 17: Line 17:
{{{  {{{
Line 27: Line 27:
}}}  }}}
Line 29: Line 29:
{{{  {{{
Line 40: Line 40:
}}}  }}}
Line 44: Line 44:
== 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);
 }}}

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:

  1. 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);
  2. 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);

nrec: PrimitivesExplanation (last edited 2017-08-03 08:18:47 by FriedemannBunjes)