Quickly Detect Gaps in Dashcam Footage
Dashcams save their footage in multiple progressive files, but there's a common flaw that goes largely unnoticed: there can be a gap in footage between each file. This lost footage can be a few seconds long, and you never know when some emergency event will happen (the entire point of a dashcam always recording). It would be terrible if the event happens during one of those recording gaps.
"Eh, the files look fine to me..."
Unfortunately, it's hard to notice that it's happening unless you happen to be watching some footage and notice that the next file doesn't start where the previous file left off.
Some file managers display the "date modified" timestamp without seconds, so it's impossible to tell at a glance. Even if it did have seconds, scanning through the list and judging what the expected seconds would be for each file sounds tedious at best.
"I checked a few by hand, it seems like my camera doesn't have the issue."
This can be a "sometimes" problem. I have personally experienced missing footage and discovered that it correlates with high-activity scenes. When a lot is happening on camera, the bitrate of the recording is higher. In this situation, the dashcam and/or memory card don't always keep up well enough to start the next recording promptly. And the joke's on us: an emergency event probably qualifies as a high-activity scene...
Let's make a tool to automatically flag these gaps in recordings. That way we can troubleshoot and attempt to fix it before we regret missing footage when it matters most!
Approach
We don't need to parse much data from each video to figure this out. By using the video's timestamp as well as the video duration, we know when the next video's timestamp is expected to be.
Known Limitation
Since the timestamp only has a precision down to the second, we won't be able to detect gaps smaller than a second. We have to accept this limitation. This also means the precision at which we can calculate the gap is limited. A recording can start at, say, 12:00:00.678 but have a timestamp of 12:00:00. Our calculated gap would not be able to factor in the 678ms.
But that's fine since the real goal is to know that gaps have occurred rather than know the exact gap size down to the millisecond.
Let's write it!
But first a quick note, I am assuming that the camera has named the files in some sequential order that follows an alphanumeric sort. So, the tool will simply alpha-sort (using a priority queue because it's faster) the file paths and parse them in that order. os.walk()
might already return the paths in the order we want, but I will still sort it to be safe.
Each file is then compared to the previous file and the gap between them is calculated.
Here it is on GitHub!
And this is the tool's output:
$ python3 detect.py -d "/tank1/cameras/A119S"
Detected 6408ms gap between ".../2017_0114_141750_008.MP4" and ".../2017_0114_141805_009.MP4"
Detected 3520ms gap between ".../2017_0114_141805_009.MP4" and ".../2017_0114_141816_010.MP4"
Detected 1000ms gap between ".../2017_0116_121056_017.MP4" and ".../2017_0116_121557_018.MP4"
Detected 1000ms gap between ".../2017_0205_153459_307.MP4" and ".../2017_0205_153959_308.MP4"
Detected 1000ms gap between ".../2017_0226_175522_708.MP4" and ".../2017_0226_180023_709.MP4"
Detected 1000ms gap between ".../2017_0402_204918_285.MP4" and ".../2017_0402_205419_286.MP4"
Detected 9528ms gap between ".../2017_0708_104906_511.MP4" and ".../2017_0708_104917_512.MP4"
Detected 1000ms gap between ".../2017_0708_110435_516.MP4" and ".../2017_0708_110935_517.MP4"
Detected 1000ms gap between ".../2017_0716_184607_697.MP4" and ".../2017_0716_185108_698.MP4"
Detected 1000ms gap between ".../2017_0803_201801_997.MP4" and ".../2017_0803_202302_998.MP4"
Looks like it works! We have gaps though. What can be done?
Fixing your dashcam
Just some general tips.
Lower your recording quality
In other words, use a lower bitrate. It might help to lower your recording quality so in a high-activity scene the raised bitrate is still within the capabilities of your device/storage.
Use a constant bitrate (CBR)
This means high-activity scenes will not cause the bitrate to increase.
Reformat your memory card
It's possible for prolonged use to bog down the filesystem on the card, where its metadata/access tables are not as efficiently organized (or something. I'm not an expert). A reformat would give the filesystem a fresh start. I know some dashcams even have an automated reminder to reformat the card periodically.
Get a faster memory card
This is assuming the bottleneck is the card and not the dashcam.
Get a better dashcam
If all else fails. ๐