How to read values out of .avi file for mathematical analysis?

I have a sensor that digitally outputs several hundred readings every time it increments, which is on the order of a hundred times a second. The software that comes with it saves the result in a .avi file. This format is usually used for video, but for this information a video is a nonsensical representation.

I wish I could convert the .avi file into ASCII text that gives a number for the “brightness” value for each “pixel” frame by frame in the video.

I’ve found many programs that create “ASCII art” out of such videos, so the pixels wind up looking like different characters. Let me be gracious and just say this is a disappointing option given what I am trying to do.

I want to read a text file with my math or stats package and do stuff to it.

BTW if I had to read simple integers or floats in binary that’d be OK too, but I’m not going to try to work through compression algorithm expansion and all that sort of thing.

Anybody know anything that might do me?
BTW, as to why in the world they would have chosen the .avi file format to store numerical datasets that have nothing to do with imaging, I suspect a small company on a limited budget found something that would work. But I don’t really know.

Thanks!!

Take a look into something like OpenCV.

“Reading an AVI” is somewhat broad, becuase an AVI is a “container”, and doesn’t say too much about the actual underlying format. You need a codec to actually read the data in the container, OpenCV generally can figure this out for you automatically given an AVI file.

Is the “video” greyscale or are there 3 channels (RGB) every frame? Either way, OpenCV should let you get arrays of the data at each frame and then you can convert it yourself.

How automated does this need to be? There are of course programmatic ways of doing what you want, but they’re pretty complicated–the APIs for handling AVI files are non-trivial in my experience.

If you’re ok with using external programs, you can use these steps:

  • Get VirtualDub. Load your AVI file and go to Export->Image Sequence. Save the sequence somewhere in whichever format you wish.
  • Get IrfanView. Use the Batch Conversion option to convert the files to PPM format. Use the format options to select ASCII encoding.

You will get files as output that look like this:
P3

Created by IrfanView

1632 1224
255
231 202 158 227 198 154 225 198 155 225 198 155 224 197 154
224 197 154 225 197 157 225 197 157 225 197 157 224 196 156
226 199 156 229 202 159 231 202 160 228 199 157 226 197 153

The two numbers just below the comment are your width/height. Not sure about the next line, but subsequent lines are just the RGB values in integer form. I assume you can parse things from here.

I mean, this’ll probably do it fine in just a few lines:


import cv2
import numpy as np
import csv

cap = cv2.VideoCapture('test.avi')
with open('outfile.csv', 'wb') as outfile:
    writer = csv.writer(outfile, delimiter=',')
    
    while cap.isOpened():    
        ret,frame = cap.read()
        
        if not ret:
            break
        
        grayscale = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        
        grayscale = np.ndarray.flatten(grayscale)
        writer.writerow(grayscale)
    

cap.release()


Prints each frame as a comma-delimited line in a CSV file. It’ll probably take a while but that’s mostly because videos are large. The grayscale conversion is because you mentioned “brightness”, you may not want to do that or do something subtly different with the RGB values.

Edit: This is OpenCV and Numpy in python 2.7

I really want to warn you that this will balloon the file size. I’m testing it on a random 500 MB AVI I had lying around and the text CSV file is over 1.5GB and not done. You are converting it into a raw format.

That’s pretty neat. I didn’t realize that OpenCV made things that easy when it comes to video handling. Of course, having to use Python is a non-starter, but it appears the standard bindings are C++ :).

How much expansion you’ll get depends on the encoding. Most video files are going to use some MPEG-4 variant, which is liable to be compressed 50:1 or more. But if this is output from a sensor, maybe it’s uncompressed or losslessly compressed–in which case the expansion (after grayscale conversion) might only be 2:1.

Ahah!! I have used IrfanView for years, for still image files, and of course I tried it here. It did open the “video” and display it as a long skinny rectangle with various gray shadings that fluctuated for as long as it took to play. But I keep getting error messages when I try to export .ppm files. I’ve also tried converting the “video” to a bunch of separate images that I could use the batch conversion on, but nothing seems to happen. I mean I click OK (or whatever) and nothing flickers, no error messages, and no files appear.

I think IrfanView has the codec it needs to read this file, because it displays a fluctuating image similar to Windows media Player and others. What else can be wrong?

Hmm, it looks like IrfanView does support frame extraction (it was buried in a different menu than I was expecting). I was able to get it to work–I loaded up a video, then Options->Extract all frames, and I got a big directory of BMP files. From there you can use Batch Conversion to convert to PPM.

You’re saying that you can’t even get the frame extraction working, right? Did you use Options->Extract all frames? If that doesn’t work, you might try VirtualDub. It has much better support for videos.

You could also use FFMPEG to output to BMP (which is trivial to read):
https://trac.ffmpeg.org/wiki/Create%20a%20thumbnail%20image%20every%20X%20seconds%20of%20the%20video

Exactly. And I hunted around (I think it was in their help system but maybe it was online) for explanations why, and what I was turning up was mostly about codec failures, which I don’t think can be the explanation because IrfanView can display the “video”.

Maybe I’ll try the others…

I mean, mine definitely works if you can get numpy and OpenCV installed. And pretty much any stats program can read a CSV file. Mine also has the advantage of keeping all the data from a single “video” in a single file.

Since this avi file isn’t a real video, and as noted above, avi’s are containers for subordinate video “chunks” that can be in any of dozens of different encodings …

How sure is the OP that the chunk of data in the container used a standard encoding?

Unless this video “plays” without error in an ordinary vid player (even if the visual output is gibberish) we don’t know for sure that any conventional codec can make sense out of the file segment created by this instrument of yours.

I have no evidence about whether the encoding is standard. Since it is not really video, not really image data at all, the whole basic idea itself of using a video file format is already pretty nonstandard. It makes a certain kind of sense, because many times a second we have to write a few hundred or thousand values into the file, the values are integers about a byte long (I think), we do this dozens of times a second, and the pace is fixed. From the computing point of view this data stream has a lot of features in common with video. But clearly there was some out of the box thinking at work.

And, yes, it “plays” in several players, and the way it looks is more or less what I might expect.