Skip to content

Instantly share code, notes, and snippets.

@cogwirrel
Last active March 8, 2020 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cogwirrel/ab145cd38d4fc7900c2a811a5297a06b to your computer and use it in GitHub Desktop.
Save cogwirrel/ab145cd38d4fc7900c2a811a5297a06b to your computer and use it in GitHub Desktop.
Download a particular time range of a kinesis video stream as an mp4
#!/bin/bash
# Download a chunk of kinesis stream!
# Prerequisites: awscli, ffmpeg, jq
# Usage: ./download-kinesis-video-stream.sh --start "2020-03-08T13:53:00+11:00" --end "2020-03-08T13:54:00+11:00" --output output.mp4
# Change these to whatever you use...
STREAM_NAME="skyline-dash-front"
AWS_PROFILE="skyline"
AWS_REGION="ap-southeast-2"
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-s|--start)
START="$2"
shift # past argument
shift # past value
;;
-e|--end)
END="$2"
shift # past argument
shift # past value
;;
-o|--output)
OUTPUT_FILE="$2"
shift # past argument
shift # past value
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
echo "Getting data endpoint for HLS streaming session..."
ENDPOINT=$(aws --region $AWS_REGION --profile $AWS_PROFILE kinesisvideo get-data-endpoint --stream-name "$STREAM_NAME" \
--api-name GET_HLS_STREAMING_SESSION_URL | jq -r '.DataEndpoint')
echo "Got $ENDPOINT"
echo "Getting HLS streaming session url..."
HLS_URL=$(aws kinesis-video-archived-media get-hls-streaming-session-url \
--region $AWS_REGION --profile $AWS_PROFILE --endpoint-url $ENDPOINT \
--stream-name $STREAM_NAME --playback-mode ON_DEMAND \
--hls-fragment-selector "FragmentSelectorType=SERVER_TIMESTAMP,TimestampRange={StartTimestamp=$START,EndTimestamp=$END}" \
--expires 43000 | jq -r '.HLSStreamingSessionURL')
echo "Got $HLS_URL"
echo "Downloading with ffmpeg"
ffmpeg -re -i "$HLS_URL" -c copy -bsf:a aac_adtstoasc "$OUTPUT_FILE"
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment