Stitching together UniFi Video Footage

I recently wanted a video of a large amount (lets say 24 hours worth) of footage from my Ubiquiti UniFi camera. If you're doing 24/7 recording, the NVR interface chunks videos into 10 minute segments. You can check multiple videos to download at one time, but that's still 144 files to go through, and the web interface chokes on this anway.

However, thanks to the power of linux (WSL to the rescue once again), this is a pretty easy problem to solve. There are a few hurdles that get in our way though.

Step 1) Enable SSH

Head to your web interface. Not the one on port 7443, but the main landing page on port 80. In the upper right, you'll see the little settings gears. Enter your device setup password. This won't be the same as your NVR password, and the username is likely ubnt unless you have already changed some things here.

Once logged in, click on the Configuration tab and enable the SSH Server check box at the bottom of this screen. Save your configuration.

Step 2) Drag down the files

We now have access to our NVR's file system, you can SSH in with the same configuration credentials we used above. We can go find our video files, which live in the following directory: /srv/unifi-video/videos/<GUID>/<YEAR>/<MONTH>/<DAY>
These video segments are saved in 5 second chunks, so there is a ton of them (roughly 17,280 per day, but ls -l | wc -l only gave me 14,402 for some reason...). Presumably this is so no large amount of video is ever lost should the system crash, but it makes things a bit more annoying. We're going to drag down all of these for post-processing with the following:
scp [email protected]:/srv/unifi-video/videos/6c12c19d-72ad-49eb-a241-f3e6bfee6ede/2018/07/10/* .

Remember that videos are saved in UTC by default, so adjust your directory name for the day you want, and of course substitute your own IP and GUID. I would highly suggest being in an empty directory as the aformentioned metric ton of files will be dumped here.

Step 2a) Go have a beer

This is going to take a while.

Step 3) Create a list

We're going to use a list of files as our input to ffmpeg. A quick one-liner can generate this list for us: for f in *.mp4; do echo "file '$PWD/$f'" >> mylist.txt; done

Step 4) Merge the list

Another quick one liner: ffmpeg -f concat -safe 0 -i mylist.txt -c copy -fflags +genpts merged.mp4
This one will take a while as well. Please don't ask me what all the flags do. I speant a reasonable amount of time figuring out what works correctly, and the answers are spread across a dozen Stack Overflow pages.

Step 5) Profit

You should now have one giant file with 24 hours of video. VLC works wonders, and remember that ] is the shortcut key for speed up (up to 31.5x), hitting + brings us back to 1x speed real quick, spacebar pauses, and e steps forward 1 frame at a time.