Skip to content

Instantly share code, notes, and snippets.

@harakka
Last active January 23, 2020 20:48
Show Gist options
  • Save harakka/9ad4936d051f3b9848f8d79c39c86e89 to your computer and use it in GitHub Desktop.
Save harakka/9ad4936d051f3b9848f8d79c39c86e89 to your computer and use it in GitHub Desktop.
Recompresses motioneye-generated mp4 files after creation event to save space
#!/bin/sh
# MIT License
# Copyright (c) 2019 Antti Riikonen
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# What does this script do?
# --------------------------
# This script is meant to run by motioneye in a post file write event. It runs
# the created file through ffmpeg, if the file is of type mp4. For other files
# the script exits. This is useful because motioneye triggers the event for
# all files, including snapshots.
# It tries to do some sanity checking but don't assume it is safe and secure.
# If you care about security, at least make sure you're not running motioneye
# as root, which is the default.
# Depends on ffmpeg for encoding, should not depend on bashisms.
debug_print() {
if [ "$DEBUG_MODE" -eq 1 ]; then echo "$1"
fi
}
DEBUG_MODE=0
TEMPNEW=$(mktemp).mp4 || exit 1
TEMPOLD=$(mktemp) || exit 1
TARGETFILE="$*"
debug_print "Recompress: recompress script for motioneye file end event starting."
if [ ! -f "$TARGETFILE" ]; then
if [ -f "$TARGETFILE.mp4" ]; then
TARGETFILE="$TARGETFILE.mp4"
debug_print "Recompress: target is missing .mp4 extension, adding."
else
debug_print "Recompress: error: missing $TARGETFILE, with and without .mp4." >&2
exit 1
fi
fi
FILE_OUTPUT=$(file -ib "$TARGETFILE")
case $FILE_OUTPUT in
*mp4*)
debug_print "Recompress: $FILE_OUTPUT detected.";;
*)
debug_print "Recompress: $TARGETFILE type is $FILE_OUTPUT, exiting."
exit 0;;
esac
# This part tries to handle file creation cleanly, but it is
# probably vulnerable against usual temp file attacks.
debug_print "Recompress starting on target: $TARGETFILE, temp files: $TEMPNEW, $TEMPOLD"
FFMPEG_OUTPUT=$(/usr/bin/ffmpeg -loglevel error -i "$TARGETFILE" "$TEMPNEW" 2>&1) || { echo "Recompress error at recoding stage, ffmpeg output follows: $FFMPEG_OUTPUT" >&2; rm -v "$TEMPNEW"; exit 1; }
mv "$TARGETFILE" "$TEMPOLD" || { echo "Recompress error: couldn't move $TARGETFILE to $TEMPOLD." >&2; rm -v "$TEMPNEW"; exit 1; }
mv "$TEMPNEW" "$TARGETFILE" || { echo "Recompress error: couldn't move $TEMPNEW to $TARGETFILE." >&2; rm -v "$TEMPOLD"; rm -v "$TEMPNEW"; exit 1; }
rm "$TEMPOLD" || { echo "Recompress error: couldn't delete $TEMPOLD." >&2; exit 1; }
debug_print "Recompress and cleanup complete."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment