Odds are if you have a piece of software such as MATLAB, you did not compensate MathWorks® for their product unless you are currently engaged in a mutually beneficial relationship with a large organization, you are insane, or you are filthy rich.
With that in mind, the topic of the day is how to make the “most” of your incredibly expensive software that you did not pay for.
Do you remember when televisions weren’t widescreen, flatscreen, oversized computer monitors? I sure don’t. But my great-great grandfather’s diary mentions that in the dark ages, television channels weren’t broadcast digitally. I personally find this hard to believe.
However, apparently there existed something called “static” that would occur when the electromagnetic radiation emitted by the television outweighed the meaningful signal being received by the antenna. The end result of this was a seemingly random pattern of flickering black and white. For some people it was quite annoying, but for some people it was actually interesting to watch. With digital broadcasting, when the signal is lost, the image blocks up and stutters, but the effect isn’t anywhere as interesting as it is when analogue is involved.
So, while there are various ways to generate a static noise pattern with video editing software such as Sony Vegas, using video editing software to create video would make too much sense for my purposes. Also, Sony Vegas is not RIDICULOUSLY EXPENSIVE enough.
Anyway, on to the actual interesting part. There are two ways to actually accomplish this, but they are basically the same: the only real difference is the number of files generated and which program actually does the video authoring.
The basic theory is this: use a random number generator to generate a matrix of values that can be interpreted as an image. If we take the simplistic view that an image is simply a 3 dimensional matrix of the type mxnx3, where m is the height of the desired image, n is the width and the 3 allows each pixel to have an R,G,B value, this makes figuring out the parameters easier. Because we want greyscale static, the R G and B values will be identical for each pixel: this conveniently saves us a fair amount of computation because we only really need to compute one matrix for each image rather than three.
Pointless exposition aside, (I just wanted to make myself look smart, forgive me if I failed), we’ll start with the messy method.
%start a for loop, designed for 30 seconds of video at ~24fps
for i = 1:24*30
%make a random matrix, in this case it will be 720x480 (wxh)
randmat = rand(480,720,'double');
%make it 3D, and greyscale
Y = cat(3,randmat,randmat,randmat);
%set up a variable so each image will have a different name, e.g. static0001.bmp
imout = sprintf('static%04d.bmp',i);
%write the image to the active directory in matlab
imwrite(Y,imout,'bmp');
end
WordPress does a pretty grand job of derping up any formatting whatsoever, and I haven’t found a way around that without plugins, which I can’t use.
This is the messy method because it will generate 740 independently named bitmap images to your active directory (or more if you increase the length of the loop). The reason for exporting them with this naming system is simple: so we don’t go insane trying to import them into avisynth, which is the next step. The avisynth script is a whopping one line long:
ImageReader("drive:\path\to\MATLAB\static%04d.bmp",start=1,end=740,fps=24/1.001)
This tells ImageReader to look for a pattern that follows the exact same pattern we used to name the bitmaps, which is pretty convenient. This will load each image as an individual frame and set the framerate to 24/1.001 fps, progressive ntsc. Now all that’s left is to encode. Simply add converttoYV12() and throw it at x264. I recommend crf0 because not encoding it losslessly would be silly. Or if you care about decoding speed, you could encode it to an uncompressed rgb avi with virtualdub pretty easily.
The less messy method is second for a couple of reasons: 1) it requires the latest version of MATLAB (r2010b) and 2) it has a strange issue that I am too stupid to fix.
%declare the video object and compression type
video = VideoWriter('static.avi','Uncompressed AVI');
% Set the frame rate.
video.FrameRate = 24/1.001;
%open for writing
open(video);
%set up the loop
for i = 1:24*30
%make a random matrix
randmat = rand(480,720,'double');
%make it three dimensional, greyscale
Y = cat(3,randmat,randmat,randmat);
%write matrix as a video frame
derp = im2frame(Y);
%write frame to video file
writeVideo(video,derp);
end
As you can see, the contents of the for loop don’t change much at all. The VideoWriter class requires MATLAB r2010b or later, and the error in the script is that for some reason or another, it does not finish writing the video file until you close MATLAB. It throws the following warning:
Warning: Direct access of structure fields returned by a function call (e.g.,
call to static) is not allowed. See MATLAB 7.10 Release Notes, "Subscripting Into Function Return Values" for details.
??? Attempt to reference field of non-structure array.
Since the output seems fine and I can’t really see why this warning shows up (it does on the first method as well). Perhaps someone more acquainted with MATLAB would be able to figure it out, but since the output is fine, I don’t really care that much. This method is faster since you don’t have images out the wazoo, and the clip is put together on the fly. Abrupt ending.