From 560b0999ff951b42d92482bb0753d512dfcfa520 Mon Sep 17 00:00:00 2001 From: Peter Date: Sun, 2 Aug 2020 17:01:43 +0100 Subject: [PATCH] Amend ubvinfo callout to prefer the ubnt_ubvinfo shipped with Unifi Protect where possible. --- README.txt | 29 +++++++++++++++++++++-------- src/main/java/Remux.java | 7 ++++++- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/README.txt b/README.txt index ce85e79..8df8f04 100644 --- a/README.txt +++ b/README.txt @@ -1,15 +1,26 @@ OVERVIEW ======== -This is a Java tool that extracts an H.264 video bitstream from Ubiquiti's proprietary Unifi Protect .ubv container. +This is a Java tool that extracts an H.264 video bitstream from Ubiquiti's proprietary Unifi Protect .ubv container. Native binaries are available for ARM64 and AMD64 Linux systems. + +QUICK START FOR CLOUD KEY GEN2 +============================== + +1. Go to the "releases" page at https://github.com/petergeneric/unifi-protect-remux/releases and download the latest remux-arm64 binary. +2. Upload this to your Cloud Key, leaving in the home folder +3. Navigate to where your .ubv video is located (/srv/unifi-protect/video) +4. Run ~/remux-amd64 with a single argument, the .ubv file you want to extract from (e.g. ~/remux-amd64 2020/08/01/XXXXXXXXXXXX_0_rotating_1596300058863.ubv) +5. Once the tool completes, you'll find a series of .h264 files in the same directory as the .ubv input +6. Transfer these to your machine. The .h264 files are raw video bitstreams. It will be easier if you remux them into an .MP4 wrapper using FFmpeg +7. Install FFmpeg for your platform (https://ffmpeg.zeranoe.com/builds/ for Windows, apt install ffmpeg for Linux, brew install ffmpeg for MacOS using HomeBrew or https://evermeet.cx/ffmpeg/ otherwise) +8. Use the following command to remux each .h264 into an MP4: ffmpeg -i example.h264 -vcodec copy example.mp4 LIMITATIONS =========== -1. Currently this tool only works with single partition files. In the future it will be expanded to work with multiple partitions, by creating multiple .mp4 files -2. Currently only the video stream is extracted. The underlying can be used to extract the audio bitstream too, but currently the tool does not do this. The resulting AAC and H.264 bitstreams can be muxed together into an MP4 trivially. -3. The tool expects a UNIX system (tested on Linux and MacOS, BSD should work too). The code will run just fine under Windows, but the provided scripts expect UNIX. +1. Currently only the video stream is extracted. The underlying can be used to extract the audio bitstream too, but currently the tool does not do this. Raise an issue if there's interest in this functionality. +2. The tool expects a UNIX system (tested on Linux and MacOS, BSD should work too). The code will run just fine under Windows, it's just untested. FINDING SOURCE MEDIA ==================== @@ -28,12 +39,14 @@ Build Native Binary You can build a native binary using GraalVM's native-image tool. Instructions on how to install the dependencies are at: -https://www.graalvm.org/docs/reference-manual/native-image/ +https://www.graalvm.org/docs/reference-manual/native-image/ (the installation is as simple as: unpack latest graal CE, run "gu install native-image") -N.B. for Ubuntu you'll also need the following packages: +N.B. for Ubuntu you'll also need the following packages (N.B. libz-dev may also be zlib1g-dev): apt install build-essential libz-dev -Then simply run "make native-image" to invoke Maven and then run the resulting .jar through Graal +Make graal your default JVM (export JAVA_HOME=/path/to/graal), put it on the path (export PATH=$PATH:/path/to/graal/bin) + +Then simply run "make native-image", which will run a Maven build and generate a "remux" binary via Graal Install FFmpeg -------------- @@ -50,7 +63,7 @@ RUNNING Entirely locally ---------------- -If you have a version of ubnt_ubvinfo that runs on your system (and it's on your PATH), you can run "remux.sh some.ubv" +If you have a version of ubnt_ubvinfo that runs on your system (and it's on your PATH), you can simply run "remux somefile.ubv" Run ubvinfo remotely and remux locally -------------------------------------- diff --git a/src/main/java/Remux.java b/src/main/java/Remux.java index 8a4bb67..e84e848 100644 --- a/src/main/java/Remux.java +++ b/src/main/java/Remux.java @@ -62,7 +62,12 @@ private static Stream ubvinfo(final File inputFile) throws IOException, { tempFile = File.createTempFile("ubv", ".txt"); - ProcessBuilder pb = new ProcessBuilder("ubnt_ubvinfo", "-t", "7", "-P", "-f", inputFile.getAbsolutePath()); + final File cloudkeyBinaryLocation = new File("/usr/share/unifi-protect/app/node_modules/.bin/ubnt_ubvinfo"); + + // If possible, use the ubnt_ubvinfo under /usr/share. If not present, assume ubnt_ubvinfo is on PATH + final String binary = cloudkeyBinaryLocation.exists() ? cloudkeyBinaryLocation.getPath() : "ubnt_ubvinfo"; + + ProcessBuilder pb = new ProcessBuilder(binary, "-t", "7", "-P", "-f", inputFile.getAbsolutePath()); pb.redirectError(new File("/dev/null")); // Discard error pb.redirectOutput(tempFile); Process process = pb.start();