wiki:

Capture

/

Lightning


Version 1 (modified by burek, 6 years ago) (diff)

--

The problem

This is a funny tutorial that describes one of the ways you can capture a lightning, during storms. The problem with a lightning capturing is that it happens at a random time interval and lasts for a brief moment of time, so it is very tricky to capture it easily. The usual way is to record the sky continuously for a longer time period and later to edit the recording and cut the pieces where the lightning happened or using some hi-tech expensive cameras, that can do all this for you.

The ideal solution

But let's do this in a cheap and easy way. What we need is to capture the video of the sky continuously and to save a video only when a lightning happens. We can accomplish this by using "buffered recording". Consider the following example.

  1. One ffmpeg instance captures the camera's input and buffers the output for, say 20 seconds.
  2. We can start another ffmpeg instance, whenever we see the lightning bolt, which will take the output of the first ffmpeg and record it to a file.

Let's translate this into familiar ffmpeg commands. First let's start the first ffmpeg instance, which will constantly keep capturing the camera's output and buffer it for some time:

ffmpeg -f v4l2 -i /dev/video0 -vcodec rawvideo -delay 20 -f mpegts udp://127.0.0.1:1234

and when we see the lightning, we'll just type something like this and press the Enter key:

ffmpeg -f mpegts -i udp://127.0.0.1:1234 -vcodec libx264 -t 30 output.flv

Now, this is all great, except FFmpeg doesn't have (yet) a "-delay <seconds>" option, which will buffer and delay the output for specified amount of seconds. I hope this will be implemented soon, but until then, we can use another tool, which can do what we need.

If you are interested in a discussion about the "-delay" option in ffmpeg, please read this ticket.

The actual solution

The tool is a modified version of Samplicator which "Sends copies of (UDP) datagrams to multiple receivers". The modified version of this tool has a feature added that allows Samplicator to delay its output UDP stream for some time.

Shortly, it works like this. We start the first instance of ffmpeg, capture the camera input and forward the video output to a local udp port number 1234:

ffmpeg -f v4l2 -i /dev/video0 -vcodec rawvideo -f mpegts udp://127.0.0.1:1234

Then we start a modified Samplicator, to buffer that udp stream for 20 seconds:

samplicate -p 1234 -z 20 127.0.0.1/5678

and finally, we will prepare the following command and when we see the lightning, we'll just press the Enter key:

ffmpeg -f mpegts -i udp://127.0.0.1:5678 -vcodec libx264 -t 30 output.flv

This way we will start recording our video somewhere a couple of seconds in the past (depending on how much time it takes ffmpeg to start up and start recording the video), but since we delay the original for 20 seconds, it should be enough. Also, we record for the next 30 seconds (-t 30) which should capture the moment when the lightning happened.

Download

The source code of the modified Samplicator tool can be found here: http://ffmpeg.gusari.org/uploads/samplicator-1.3.6-z.tar.gz

Keep in mind it is in a pure beta stage, so it might not even work on your platform at all, but the author of the original Samplicator was contacted and provided with all the changes, so hopefully we can soon expect that those will be merged into the original tool and tested for bugs. Until that happens (and if it happens at all), you might use that modified tool under the same license that is provided for the original tool, nothing more, nothing less.

Happy lightning hunting :)