#!/bin/bash

set -e -u

# For a description of the sample content please see:
# http://docs.unified-streaming.com/installation/tears-of-steel.html

# You can fetch the sample content using:
# wget http://repository.unified-streaming.com/tears-of-steel.zip
# unzip tears-of-steel.zip
# rm tears-of-steel.zip

# The following commands are based on
# http://docs.unified-streaming.com/documentation/package/hls.html#using-multiplexed-audio-video-with-aes-128-protocol-version-3

openssl rand 16 > presentation.key

# Unified Streaming Platform requires a license key
# Please contact license@unified-streaming.com
if [ -z "${USP_LICENSE_KEY:-}" ]; then
  echo "Missing USP_LICENSE_KEY"
  exit 1
fi
MP4SPLIT="mp4split --license_key=${USP_LICENSE_KEY}"

${MP4SPLIT} --package-hls -o tears-of-steel_s1/prog_index.m3u8 \
  --fragment-duration=192/24 --base-media-file=s1_ \
  --encrypt-key-file=presentation.key \
  --encrypt-key-url=../presentation.key \
  tears-of-steel-avc1-400k.mp4 tears-of-steel-aac-128k.mp4

${MP4SPLIT} --package-hls -o tears-of-steel_s2/prog_index.m3u8 \
  --fragment-duration=192/24 --base-media-file=s2_ \
  --encrypt-key-file=presentation.key \
  --encrypt-key-url=../presentation.key \
  tears-of-steel-avc1-750k.mp4 tears-of-steel-aac-128k.mp4

${MP4SPLIT} --package-hls -o tears-of-steel_s3/prog_index.m3u8 \
  --fragment-duration=192/24 --base-media-file=s3_ \
  --encrypt-key-file=presentation.key \
  --encrypt-key-url=../presentation.key \
  tears-of-steel-avc1-1000k.mp4 tears-of-steel-aac-128k.mp4

${MP4SPLIT} --package-hls -o tears-of-steel.m3u8 \
  tears-of-steel_s1/prog_index.m3u8 \
  tears-of-steel_s2/prog_index.m3u8 \
  tears-of-steel_s3/prog_index.m3u8

