Picam Time Lapse Night Video

Introduction

For a while now I have been using picams to shoot the night sky in attempts to capture meteor events. I have been using raspistill at the full resolution of the particular sensor at max shutter time, which for the picam is 6 seconds. The problem has always been that to make a single 6 second frame the total cycle time is at least 40 seconds or so. Therefore the camera is only seeing less than 1/6 th of the night, thereby missing much of the action that might be taking place. So recently I found some users on the web using raspivid with a 6 second shutter speed and successfully shooting continuous night video. In this post I describe the command that optimized the settings for this purpose.

The Camera

I have several Raspberry Pi 3 Model B units with various picams and lenses. The particular one preferred is the camera module version 2 with the Sony IMX219 image sensor and equipped with a wide angle 160 degree field of view lens. This lens captures a much larger area of sky than a standard lens. The resolution of the sensor is 3280 × 2464 pixels.

RASPIVID Command

Much of the optimization that I found using the raspistill command for capturing images is applicable to the raspivid command. Below I show the complete command that I am currently using and subsequently describe the switches.

raspivid -h 720 -w 1280 -awb off -awbg 1.22,1.43 -rot 180 -ex night\
 -br 80 -co 100 -pf high -ISO 800 -g 1 -qp 10 -ss 6000000\
 -t 18000000 -b 25000000 -o /mnt/usb/outvideo.h264

Resolution

Generally speaking by using video instead of still images forces one to give up resolution. However, one does not need a resolution of 3280 x 2464. Perhaps then HD 1920 x 1080 should work. However, the way the video capture works at the sensor level sacrifices the field of view. Only the 1920 x 1080 pixels centered on the sensor with 3280 x 2464 pixels are used. To get the full width of the field of view one must step down the resolution to 1280 x 720. One can see in Figure 1 some screenshots that show this. See Sensor modes in Picam Release 1.13.

Figure 1

However, the resolution of 1280 x 720 was found adequate and capturing the full field of view potential of the lens outweighs by far the reduced resolution.

Automatic White Balance (-awb)

It was found that to successfully do night shots or video for long durations the ‘awb‘ algorithm needs to turned off and the gains preset. It appears the way the algorithm works is it needs enough color in the image to figure out how it should continually adjust the gains. But night photography has very little color and the algorithm goes awry. In the command ‘awb‘ is off and the switch ‘-awbg‘ presets the gains to those values shown (red and blue gains, respectively), which I obtained from someone on the internet who determined these values to be good. However, one might experiment to find better values.

Auto Exposure and ISO (-ex and -ISO)

During capture of a frame each pixel accumulates photons and the voltage produced is converted to a digital number with the analog-to-digital converter (ADC). An algorithm determines the gain settings of the ADC and the settings are optimized when one tells it what the conditions of the scene are. In this case it is ‘night‘. So is this not what ISO does? Apparently ISO is an algorithm that may be applying digital gain post-processing of the frame whereas the former applies analog gain adjustments during pixel capture. I stay confused about this but one needs to read Picamera Release 1.13, section 6.1.4 Sensor gain. These settings are not to be confused with exposure time, which is essentially shutter speed (the switch ‘-ss‘).

Contrast and Brightness

Contrast (‘-c 100‘) is set at maximum. Range is -100 to 100. Brighness(‘-br 80‘) is set to a high value. Range is from 0 to 100. These values were recommended by someone on the web doing night shots with the picam. They seem reasonable in that one wants good contrast between stars and the night sky while at the same time one does not want the scene to be too dark. Of course, one can experiment with these parameters as one sees fit.

Profile, Intra Refresh Period, Quantization Parameter, and Bit rate

Raspivid natively uses the h264 codec and all of these switches relate to this lossy video compression. Since we are shooting night sky video we don’t want to lose the pinpoints of light that are stars and settings need to be chosen to keep the video quality high.

The switch ‘-pf high‘ appears to be a way of telling the intended decoder that will ultimately by used for playback the requirements for this bitstream. The value ‘high’ is the most commonly used. See this Advanced Video Coding wiki for more description.

The switch ‘-g 1‘ is a parameter that is somewhat difficult to understand without looking into how video compression works and the definition of I-frames and P-frames (intra coded and predictive coded frames, respectively). I-frames have video compressed encoded pixels only within the frame, while P-frames encode only the changes relative to the preceding I-frame. A group of pictures (GoP) is an I-frame followed by a number of P-frames until a new I-frame is made. The value of the switch is the number of P-frames in this GoP. Therefore, the switch specifies only one P-frame occurs before a new I-frame is encoded. In this case if the video is recording at 30 frames per second then 1/30th of a second elapses before a new I-frame is made. A low number such as 1 means the resulting video will be larger in size.

The quantization parameter switch (‘-qp 10‘) is set at the minimum value and this results in higher quality video in that greater spatial detail in the image is retained. Together with a maximum bitrate of 25 Mbits/second (‘-b 25000000’) a high quality video results.

Shutter Speed and Video Length

The switch ‘-ss 6000000‘ straightforwardly defines the shutter time in microseconds and the value specified is the maximum permissible for the picam. The video time is the remaining switch and the value is video length in milliseconds. Therefore, ‘-t 18000000‘ represents 5 hours.

Putting the h264 Video in a mp4 Container

The resulting raw h264 encoded video will not play in many players. To solve this problem one must wrap the video in an mp4 container. ffmpeg does this with a simple command.

ffmpeg -i outvideo.h264 -c copy outvideo.mp4

Therefore, on the raspberry pi that is operating the picam it is easy to install ffmpeg. Try:

sudo apt-get install ffmpeg

Example Video

Here is a 35 second clip (representing about one and a quarter hours). If one looks carefully the resulting video is somewhat noisey. It is hard to see on Youtube, but more clearly seen with a video player. One can use rendering software such as Cyberlink’s Powerdirector to significantly remove the noise, which greatly improves scene.

Night Demo

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *