Last active
June 12, 2022 17:21
-
-
Save philcali/a786ee1fd6ead0cae598a0bf1171aa1b to your computer and use it in GitHub Desktop.
Bash AWS Lambda runtime for converting raw h264 to mkv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Env to help ffmpeg to convert the raw stream to desirable framerate | |
FRAMERATE=${FRAMERATE:-15} | |
# Env to route converted video to new base prefix | |
CONVERSION_PATH=${CONVERSION_PATH:-motion_videos_converted} | |
# Env param to control which output you want.. ie 'mp4' for browser baseed video | |
CONVERSION_FORMAT=${CONVERSION_FORMAT:-mkv} | |
function handler () { | |
EVENT_DATA=$1 | |
echo "Event data: $EVENT_DATA" | |
VIDEO_URLS=$(echo "$EVENT_DATA" | jq '.Records[].s3 | "s3://" + .bucket.name + "/" + .object.key' | tr -d '"') | |
for video_object in $VIDEO_URLS; do | |
CAMERA_NAME=$(basename "$(dirname $video_object)") | |
VIDEO_FILE=$(basename $video_object) | |
mkdir -p $CAMERA_NAME | |
aws s3 cp $video_object $CAMERA_NAME | |
NEW_VIDEO_FILE="${VIDEO_FILE%.*}.${CONVERSION_FORMAT}" | |
NEW_PATH="${CAMERA_NAME}/${NEW_VIDEO_FILE}" | |
# Fast copy with '-c' ... no need for a deep conversation | |
ffmpeg -r $FRAMERATE -i "$CAMERA_NAME/$VIDEO_FILE" -c copy "$NEW_PATH" | |
BUCKET_NAME=$(echo "$video_object" | sed -E 's|^s3://([^/]+)/.+|\1|') | |
aws s3 cp "$NEW_PATH" s3://$BUCKET_NAME/$CONVERSION_PATH/$NEW_PATH | |
# clean up working area... we don't want to step on other Lambda containers | |
rm -f $NEW_PATH | |
done | |
echo "Finished converting videos" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment