ImageWriter and ffmpeg for file size reduction

1,434 views
Skip to first unread message

Richard Warren

unread,
May 31, 2017, 11:18:26 AM5/31/17
to Bonsai Users
Hello,

Thank you so much for Bonsai! It has drastically improved my experimental workflow.

I am collecting video at 250fps (color, ~420x420 pixels, PointGrey Grasshopper camera) for ~1 hour per session. Using VideoWriter to create .avi files results in file sizes of ~10GB per session, which is a bit cumbersome. I would like to use ImageWriter and ffmpeg as described here to perform online video conversion, ideally in a manner that minimally affects the quality of the video but makes the file sizes more manageable. My questions are:
  1. Can you recommend ffmpeg settings for file size reduction that minimally affect quality?
  2. My understanding is that some video compression algorithms utilize across-frame information, such as motion. Does the ImageWriter method perform compression on a frame by frame basis, thus missing the opportunity to utilize across-frame information? Or does it collect a 'buffer' of frames that are compressed in chunks? In other words, if I word to use ffmpeg from the command line after acquisition, using the same settings as in Bonsai, would the compression algorithm perform better, or differently?
Thanks for your help!
Rick

Gonçalo Lopes

unread,
May 31, 2017, 8:44:32 PM5/31/17
to Richard Warren, Bonsai Users
Hi Rick, welcome to the forums and great to hear you find Bonsai helpful!

To answer your questions:

1) It should very much be possible to bring down the video size to almost any size that you want. The tradeoff, as you realize, will be quality. Also, what "quality" means can really depend on what the final purpose of the video is. Will you do further image processing on the video? Is it only for human eyes to process/classify? What kind of compression artifacts are acceptable?

In any case, one of the first knobs to turn on ffmpeg besides picking the codec is the output bitrate. This basically specifies how many bits per second are produced by the compression algorithm, and hence is the most direct determiner of file size.

ffmpeg has a small collection of examples here:

It can be tricky to decide which parameters are best beforehand without experimentation. I recommend trying a couple of codecs and playing with either the constant rate factor (-crf) or with a variable bitrate encoding (-vb). In terms of codecs we found either mpeg4 (CPU) or nvenc (GPU - requires NVIDIA) to be the fastest for live streaming.

You can calibrate these parameters by recording videos with a fixed duration (e.g. 1 min) at different parameters and check their quality vs file size.

2) ImageWriter simply streams the raw data to ffmpeg as if it were a file. From that perspective, ffmpeg doesn't really know where the frames come from and in principle there should be no difference over encoding from a video (except you always have to reencode the stream, since it contains raw frames). My understanding is that ffmpeg will buffer frames itself in order to compute keyframes, etc, depending on the codec.

The main difference is that if the encoding process cannot keep up with the image acquisition, you will end up growing a large buffer in RAM, which may end up blowing your process for longer recordings.

Hope this helps.

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/02c055cd-3687-4e86-9877-b7d51cc325ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Warren

unread,
Jun 1, 2017, 3:44:24 PM6/1/17
to Bonsai Users
Thanks so much for the quick and helpful reply.
  1. Adjusting the bitrate worked quite well. Using mpeg4 at 20M brings the files down to a reasonable size.
  2. Great, I feel more comfortable doing the compression on-line knowing that ffmpeg can utilize frame-frame information.
I appreciate the help.

Richard Warren

unread,
Nov 11, 2017, 3:47:21 PM11/11/17
to Bonsai Users
Hi,

I recently switched from color to grayscale video and am using ffmpeg for online video compression. I noticed that when using h264 or mpeg4 codecs and specifying the pixel format as gray I get an error saying that the pixel format gray is not supported for either of these codecs. To resolve the issue I am using yuv420p pixel format, but I'd like to find a codec that supports grayscale outputs in the interest of reducing file size. Are you aware of any codec / pixel format combinations that support grayscale encoding?

Here is my command to ffmpeg (currently using  '-pix_fmt yuv420p' because '-pix_fmt gray' throws an error):
  • 'ffmpeg -f rawvideo -vcodec rawvideo -y -s 416x456 -r 250 -pix_fmt gray -i \\.\pipe\video -vcodec mpeg4 -vf format=gray -pix_fmt yuv420p -vb 2M -filter:v "crop=386:162:18:23" E:\\bonsaiVid\\runTop.mp4'
Thanks!
Rick

Gonçalo Lopes

unread,
Nov 14, 2017, 8:00:36 PM11/14/17
to Richard Warren, Bonsai Users
Hi Rick,

Yeah, I have been looking myself for a good codec with grayscale compression support and have come up mostly empty. There's apparently not a lot of interest in encoding grayscale videos. Another hard format is 16-bit grayscale, which is useful for 10- and 12-bit video streams and maybe even for kinect depth video.

Compression is a tricky beast. I found mention of LJPG and other JPG encoding techniques, but overall they seem to be worse for compression than just using h264 or other more common MPEG formats. Let me know if you find anything else.


--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.

Richard Warren

unread,
Nov 15, 2017, 11:45:01 AM11/15/17
to Bonsai Users
Thank you for the reply. Yes, I've also come up empty regarding ffmpeg options for grayscale encoding. The closest I found was this thread suggesting jpegls, but this appears to be lossless. The lack of options is surprising give what you'd expect to be high demand for grayscale compression in surveillance footage. Anyways, I'll let you know I come acrossanything. Thanks as always for the help.

Best,
Rick
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages