Switch variable name to something that isn't a command, as it was bugging me. HLS-21
Switch variable name to something that isn't a command, as it was bugging me. HLS-21

#!/bin/bash #!/bin/bash
# #
# A very simple BASH script to take an input video and split it down into Segments # A very simple BASH script to take an input video and split it down into Segments
# before creating an M3U8 Playlist, allowing the file to be served using HLS # before creating an M3U8 Playlist, allowing the file to be served using HLS
# #
# #
   
###################################################################################### ######################################################################################
# #
# Copyright (c) 2013, Ben Tasker # Copyright (c) 2013, Ben Tasker
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without modification, # Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met: # are permitted provided that the following conditions are met:
# #
# Redistributions of source code must retain the above copyright notice, this # Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer. # list of conditions and the following disclaimer.
# #
# Redistributions in binary form must reproduce the above copyright notice, this # Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or # list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution. # other materials provided with the distribution.
# #
# Neither the name of Ben Tasker nor the names of his # Neither the name of Ben Tasker nor the names of his
# contributors may be used to endorse or promote products derived from # contributors may be used to endorse or promote products derived from
# this software without specific prior written permission. # this software without specific prior written permission.
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
###################################################################################### ######################################################################################
   
# Basic config # Basic config
OUTPUT_DIRECTORY=${OUTPUT_DIRECTORY:-'./output'} OUTPUT_DIRECTORY=${OUTPUT_DIRECTORY:-'./output'}
   
# Change this if you want to specify a path to use a specific version of FFMPeg # Change this if you want to specify a path to use a specific version of FFMPeg
FFMPEG=${FFMPEG:-'ffmpeg'} FFMPEG=${FFMPEG:-'ffmpeg'}
   
# Number of threads which will be used for transcoding. With newer FFMPEGs and x264 # Number of threads which will be used for transcoding. With newer FFMPEGs and x264
# encoders "0" means "optimal". This is normally the number of CPU cores. # encoders "0" means "optimal". This is normally the number of CPU cores.
NUMTHREADS=${NUMTHREADS:-"0"} NUMTHREADS=${NUMTHREADS:-"0"}
   
# Video codec for the output video. Will be used as an value for the -vcodec argument # Video codec for the output video. Will be used as an value for the -vcodec argument
VIDEO_CODEC=${VIDEO_CODEC:-"libx264"} VIDEO_CODEC=${VIDEO_CODEC:-"libx264"}
   
# Video codec for the output video. Will be used as an value for the -acodec argument # Audio codec for the output video. Will be used as an value for the -acodec argument
AUDIO_CODEC=${AUDIO_CODEC:-"libfdk_aac"} AUDIO_CODEC=${AUDIO_CODEC:-"aac"}
   
# Additional flags for ffmpeg # Additional flags for ffmpeg
FFMPEG_FLAGS=${FFMPEG_FLAGS:-""} FFMPEG_FLAGS=${FFMPEG_FLAGS:-""}
   
# If the input is a live stream (i.e. linear video) this should be 1 # If the input is a live stream (i.e. linear video) this should be 1
LIVE_STREAM=${LIVE_STREAM:-0} LIVE_STREAM=${LIVE_STREAM:-0}
   
# Video bitrates to use in output (comma seperated list if you want to create an adaptive stream.) # Video bitrates to use in output (comma seperated list if you want to create an adaptive stream.)
# leave null to use the input bitrate # leave null to use the input bitrate
OP_BITRATES=${OP_BITRATES:-''} OP_BITRATES=${OP_BITRATES:-''}
   
# Determines whether the processing for adaptive streams should run sequentially or not # Determines whether the processing for adaptive streams should run sequentially or not
NO_FORK=${NO_FORK:-0} NO_FORK=${NO_FORK:-0}
   
# Lets put our functions here # Lets put our functions here
   
   
## Output the script's CLI Usage ## Output the script's CLI Usage
# #
# #
function print_usage(){ function print_usage(){
   
cat << EOM cat << EOM
HTTP Live Stream Creator HTTP Live Stream Creator
Version 1 Version 1
   
Copyright (C) 2013 B Tasker, D Atanasov Copyright (C) 2013 B Tasker, D Atanasov
Released under BSD 3 Clause License Released under BSD 3 Clause License
See LICENSE See LICENSE
   
   
Usage: HLS-Stream-Creator.sh -[lf] [-c segmentcount] -i [inputfile] -s [segmentlength(seconds)] -o [outputdir] -b [bitrates] [-p filename] Usage: HLS-Stream-Creator.sh -[lf] [-c segmentcount] -i [inputfile] -s [segmentlength(seconds)] -o [outputdir] -b [bitrates] [-p filename]
   
-i Input file -i Input file
-s Segment length (seconds) -s Segment length (seconds)
-o Output directory (default: ./output) -o Output directory (default: ./output)
-l Input is a live stream -l Input is a live stream
-c Number of segments to include in playlist (live streams only) - 0 is no limit -c Number of segments to include in playlist (live streams only) - 0 is no limit
-b Output video Bitrates (comma seperated list for adaptive streams) -b Output video Bitrates (comma seperated list for adaptive streams)
-f Foreground encoding only (don't fork the encoding processes into the background - adaptive non-live streams only) -f Foreground encoding only (don't fork the encoding processes into the background - adaptive non-live streams only)
-p Playlist filename -p Playlist filename
-t Segment filename prefix -t Segment filename prefix
-S Segment directory name (default none) -S Segment directory name (default none)
  -e Encrypt the HLS segments (default none)
  -2 2-pass encoding
  -q Quality (changes to CRF)
  -C Constant Bit Rate (CBR as opposed to AVB)
   
Deprecated Legacy usage: Deprecated Legacy usage:
HLS-Stream-Creator.sh inputfile segmentlength(seconds) [outputdir='./output'] HLS-Stream-Creator.sh inputfile segmentlength(seconds) [outputdir='./output']
   
EOM EOM
   
exit exit
   
} }
   
   
function createStream(){ function createStream(){
# For VoD and single bitrate streams the variables we need will exist in Global scope. # For VoD and single bitrate streams the variables we need will exist in Global scope.
# for live adaptive streams though, that won't be the case, so we need to take them as arguments # for live adaptive streams though, that won't be the case, so we need to take them as arguments
# Some are global though, so we'll leave those as is # Some are global though, so we'll leave those as is
   
playlist_name="$1" playlist_name="$1"
output_name="$2" output_name="$2"
bitrate="$3" bitrate="$3"
infile="$4" infile="$4"
   
  local PASSVAR=
  if $TWOPASS; then
  local LOGFILE="$OUTPUT_DIRECTORY/bitrate$br"
  PASSVAR="-passlogfile \"$LOGFILE\" -pass 2"
   
  $FFMPEG -i "$infile" \
  -pass 1 \
  -passlogfile "$LOGFILE" \
  -an \
  -vcodec libx264 \
  -f mpegts \
  $bitrate \
  $FFMPEG_ADDITIONAL \
  -loglevel error -y \
  /dev/null
  fi
   
$FFMPEG -i "$infile" \ $FFMPEG -i "$infile" \
-loglevel error -y \ $PASSVAR \
  -loglevel verbose -y \
-vcodec "$VIDEO_CODEC" \ -vcodec "$VIDEO_CODEC" \
-acodec "$AUDIO_CODEC" \ -acodec "$AUDIO_CODEC" \
-threads "$NUMTHREADS" \ -threads "$NUMTHREADS" \
-map 0 \ -map 0 \
-flags \ -flags \
-global_header \ -global_header \
-f segment \ -f segment \
-segment_list "$playlist_name" \ -segment_list "$playlist_name" \
-segment_time "$SEGLENGTH" \ -segment_time "$SEGLENGTH" \
-segment_format mpeg_ts \ -segment_format mpeg_ts \
$bitrate \ $bitrate \
$FFMPEG_ADDITIONAL \ $FFMPEG_ADDITIONAL \
$FFMPEG_FLAGS \ $FFMPEG_FLAGS \
"$OUTPUT_DIRECTORY/$output_name" "$OUTPUT_DIRECTORY/$output_name"
} }
   
   
function createVariantPlaylist(){ function createVariantPlaylist(){
playlist_name="$1" playlist_name="$1"
echo "#EXTM3U" > "$playlist_name" echo "#EXTM3U" > "$playlist_name"
} }
   
   
function appendVariantPlaylistentry(){ function appendVariantPlaylistentry(){
playlist_name=$1 playlist_name=$1
playlist_path=$2 playlist_path=$2
playlist_bw=$(( $3 * 1000 )) # bits not bytes :) playlist_bw=$(( $3 * 1000 )) # bits not bytes :)
   
cat << EOM >> "$playlist_name" cat << EOM >> "$playlist_name"
#EXT-X-STREAM-INF:BANDWIDTH=$playlist_bw #EXT-X-STREAM-INF:BANDWIDTH=$playlist_bw
$playlist_path $playlist_path
EOM EOM
   
} }
   
   
function awaitCompletion(){ function awaitCompletion(){
# Monitor the encoding pids for their completion status # Monitor the encoding pids for their completion status
while [ ${#PIDS[@]} -ne 0 ]; do while [ ${#PIDS[@]} -ne 0 ]; do
# Calculate the length of the array # Calculate the length of the array
pid_length=$((${#PIDS[@]} - 1)) pid_length=$((${#PIDS[@]} - 1))
   
# Check each PID in the array # Check each PID in the array
for i in `seq 0 $pid_length` for i in `seq 0 $pid_length`
do do
# Test whether the pid is still active # Test whether the pid is still active
if ! kill -0 ${PIDS[$i]} 2> /dev/null if ! kill -0 ${PIDS[$i]} 2> /dev/null
then then
echo "Encoding for bitrate ${BITRATE_PROCESSES[$i]}k completed" echo "Encoding for bitrate ${BITRATE_PROCESSES[$i]}k completed"
   
  if [ "$LIVE_STREAM" == "1" ] && [ `grep 'EXT-X-ENDLIST' "$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_${BITRATE_PROCESSES[$i]}.m3u8" | wc -l ` == "0" ]
  then
  # Correctly terminate the manifest. See HLS-15 for info on why
  echo "#EXT-X-ENDLIST" >> "$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_${BITRATE_PROCESSES[$i]}.m3u8"
  fi
   
unset BITRATE_PROCESSES[$i] unset BITRATE_PROCESSES[$i]
unset PIDS[$i] unset PIDS[$i]
fi fi
done done
PIDS=("${PIDS[@]}") # remove any nulls PIDS=("${PIDS[@]}") # remove any nulls
  BITRATE_PROCESSES=("${BITRATE_PROCESSES[@]}") # remove any nulls
sleep 1 sleep 1
done done
} }
   
  function encrypt(){
  # Encrypt the generated segments with AES-128 bits
   
   
  # Only run the encryption routine if it's been enabled (and not blocked)
  if [ ! "$ENCRYPT" == "1" ] || [ "$LIVE_STREAM" == "1" ]
  then
  return
  fi
   
  echo "Generating Encryption Key"
  KEY_FILE="$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}.key"
   
  openssl rand 16 > $KEY_FILE
  ENCRYPTION_KEY=$(cat $KEY_FILE | hexdump -e '16/1 "%02x"')
   
  echo "Encrypting Segments"
  for SEGMENT_FILE in ${OUTPUT_DIRECTORY}/*.ts
  do
  SEG_NO=$( echo "$SEGMENT_FILE" | grep -o -P '_[0-9]+\.ts' | tr -dc '0-9' )
  ENC_FILENAME="$OUTPUT_DIRECTORY/${SEGMENT_PREFIX}_enc_${SEG_NO}".ts
   
  # Strip leading 0's so printf doesn't think it's octal
  #SEG_NO=${SEG_NO##+(0)} # Doesn't work for some reason - need to check shopt to look further into it
  SEG_NO=$(echo $SEG_NO | sed 's/^0*//' )
   
  # Convert the segment number to an IV.
  INIT_VECTOR=$(printf '%032x' $SEG_NO)
  openssl aes-128-cbc -e -in $SEGMENT_FILE -out $ENC_FILENAME -nosalt -iv $INIT_VECTOR -K $ENCRYPTION_KEY
   
  # Move encrypted file to the original filename, so that the m3u8 file does not have to be changed
  mv $ENC_FILENAME $SEGMENT_FILE
   
  done
   
  echo "Updating Manifests"
  # this isn't technically correct as we needn't write into the master, but should still work
  for manifest in ${OUTPUT_DIRECTORY}/*.m3u8
  do
  # Insert the KEY at the 5'th line in the m3u8 file
  sed -i "5i #EXT-X-KEY:METHOD=AES-128,URI="${PLAYLIST_PREFIX}.key "$manifest"
  done
  }
   
# This is used internally, if the user wants to specify their own flags they should be # This is used internally, if the user wants to specify their own flags they should be
# setting FFMPEG_FLAGS # setting FFMPEG_FLAGS
FFMPEG_ADDITIONAL='' FFMPEG_ADDITIONAL=''
LIVE_SEGMENT_COUNT=0 LIVE_SEGMENT_COUNT=0
IS_FIFO=0 IS_FIFO=0
TMPDIR=${TMPDIR:-"/tmp"} TMPDIR=${TMPDIR:-"/tmp"}
MYPID=$$ MYPID=$$
  TWOPASS=false
  QUALITY=
  CONSTANT=false
# Get the input data # Get the input data
   
# This exists to maintain b/c # This exists to maintain b/c
LEGACY_ARGS=1 LEGACY_ARGS=1
   
# If even one argument is supplied, switch off legacy argument style # If even one argument is supplied, switch off legacy argument style
while getopts "i:o:s:c:b:p:t:S:lf" flag while getopts "i:o:s:c:b:p:t:S:q:Clfe2" flag
do do
LEGACY_ARGS=0 LEGACY_ARGS=0
case "$flag" in case "$flag" in
i) INPUTFILE="$OPTARG";; i) INPUTFILE="$OPTARG";;
o) OUTPUT_DIRECTORY="$OPTARG";; o) OUTPUT_DIRECTORY="$OPTARG";;
s) SEGLENGTH="$OPTARG";; s) SEGLENGTH="$OPTARG";;
l) LIVE_STREAM=1;; l) LIVE_STREAM=1;;
c) LIVE_SEGMENT_COUNT="$OPTARG";; c) LIVE_SEGMENT_COUNT="$OPTARG";;
b) OP_BITRATES="$OPTARG";; b) OP_BITRATES="$OPTARG";;
f) NO_FORK=1;; f) NO_FORK=1;;
p) PLAYLIST_PREFIX="$OPTARG";; p) PLAYLIST_PREFIX="$OPTARG";;
t) SEGMENT_PREFIX="$OPTARG";; t) SEGMENT_PREFIX="$OPTARG";;
S) SEGMENT_DIRECTORY="$OPTARG";; S) SEGMENT_DIRECTORY="$OPTARG";;
  e) ENCRYPT=1;;
  2) TWOPASS=true;;
  q) QUALITY="$OPTARG";;
  C) CONSTANT=true;;
esac esac
done done
   
   
if [ "$LEGACY_ARGS" == "1" ] if [ "$LEGACY_ARGS" == "1" ]
then then
# Old Basic Usage is # Old Basic Usage is
# cmd.sh inputfile segmentlength # cmd.sh inputfile segmentlength
   
INPUTFILE=${INPUTFILE:-$1} INPUTFILE=${INPUTFILE:-$1}
SEGLENGTH=${SEGLENGTH:-$2} SEGLENGTH=${SEGLENGTH:-$2}
if ! [ -z "$3" ] if ! [ -z "$3" ]
then then
OUTPUT_DIRECTORY=$3 OUTPUT_DIRECTORY=$3
fi fi
fi fi
   
   
# Check we've got the arguments we need # Check we've got the arguments we need
if [ "$INPUTFILE" == "" ] || [ "$SEGLENGTH" == "" ] if [ "$INPUTFILE" == "" ] || [ "$SEGLENGTH" == "" ]
then then
print_usage print_usage
fi fi
   
# FFMpeg is a pre-requisite, so let check for it # FFMpeg is a pre-requisite, so let check for it
if hash $FFMPEG 2> /dev/null if hash $FFMPEG 2> /dev/null
then then
# FFMpeg exists # FFMpeg exists
echo "ffmpeg command found.... continuing" echo "ffmpeg command found.... continuing"
else else
# FFMPeg doesn't exist, uh-oh! # FFMPeg doesn't exist, uh-oh!
echo "Error: FFmpeg doesn't appear to exist in your PATH. Please addresss and try again" echo "Error: FFmpeg doesn't appear to exist in your PATH. Please addresss and try again"
exit 1 exit 1
fi fi
   
# Check whether the input is a named pipe # Check whether the input is a named pipe
if [ -p "$INPUTFILE" ] if [ -p "$INPUTFILE" ]
then then
echo "Warning: Input is FIFO - EXPERIMENTAL" echo "Warning: Input is FIFO - EXPERIMENTAL"
IS_FIFO=1 IS_FIFO=1
   
fi fi
   
# Check output directory exists otherwise create it # Check output directory exists otherwise create it
if [ ! -w $OUTPUT_DIRECTORY ] if [ ! -w $OUTPUT_DIRECTORY ]
then then
echo "Creating $OUTPUT_DIRECTORY" echo "Creating $OUTPUT_DIRECTORY"
mkdir -p $OUTPUT_DIRECTORY mkdir -p $OUTPUT_DIRECTORY
fi fi
   
if [ "$LIVE_STREAM" == "1" ] if [ "$LIVE_STREAM" == "1" ]
then then
FFMPEG_ADDITIONAL+="-segment_list_flags +live" FFMPEG_ADDITIONAL+="-segment_list_flags +live"
   
if [ "$LIVE_SEGMENT_COUNT" -gt 0 ] if [ "$LIVE_SEGMENT_COUNT" -gt 0 ]
then then
FFMPEG_ADDITIONAL+=" -segment_list_size $LIVE_SEGMENT_COUNT" WRAP_POINT=$(($LIVE_SEGMENT_COUNT * 2)) # Wrap the segment numbering after 2 manifest lengths - prevents disks from filling
  FFMPEG_ADDITIONAL+=" -segment_list_size $LIVE_SEGMENT_COUNT -segment_wrap $WRAP_POINT"
fi fi
fi fi
   
   
# Pulls file name from INPUTFILE which may be an absolute or relative path. # Pulls file name from INPUTFILE which may be an absolute or relative path.
INPUTFILENAME=${INPUTFILE##*/} INPUTFILENAME=${INPUTFILE##*/}
   
# If a prefix hasn't been specified, use the input filename # If a prefix hasn't been specified, use the input filename
PLAYLIST_PREFIX=${PLAYLIST_PREFIX:-$INPUTFILENAME} PLAYLIST_PREFIX=${PLAYLIST_PREFIX:-$INPUTFILENAME}
SEGMENT_PREFIX=${SEGMENT_PREFIX:-$PLAYLIST_PREFIX} SEGMENT_PREFIX=${SEGMENT_PREFIX:-$PLAYLIST_PREFIX}
   
# The 'S' option allows segments and bitrate specific manifests to be placed in a subdir # The 'S' option allows segments and bitrate specific manifests to be placed in a subdir
SEGMENT_DIRECTORY=${SEGMENT_DIRECTORY:-''} SEGMENT_DIRECTORY=${SEGMENT_DIRECTORY:-''}
   
if [ ! "$SEGMENT_DIRECTORY" == "" ] if [ ! "$SEGMENT_DIRECTORY" == "" ]
then then
   
if [ ! -d "${OUTPUT_DIRECTORY}/${SEGMENT_DIRECTORY}" ] if [ ! -d "${OUTPUT_DIRECTORY}/${SEGMENT_DIRECTORY}" ]
then then
mkdir "${OUTPUT_DIRECTORY}/${SEGMENT_DIRECTORY}" mkdir "${OUTPUT_DIRECTORY}/${SEGMENT_DIRECTORY}"
fi fi
   
SEGMENT_DIRECTORY+="/" SEGMENT_DIRECTORY+="/"
fi fi
   
# Set the bitrate # Set the bitrate
if [ ! "$OP_BITRATES" == "" ] if [ ! "$OP_BITRATES" == "" ]
then then
# Make the bitrate list easier to parse # Make the bitrate list easier to parse
OP_BITRATES=${OP_BITRATES//,/$'\n'} OP_BITRATES=${OP_BITRATES//,/$'\n'}
   
# Create an array to house the pids for backgrounded tasks # Create an array to house the pids for backgrounded tasks
declare -a PIDS declare -a PIDS
declare -a BITRATE_PROCESSES declare -a BITRATE_PROCESSES
   
# Get the variant playlist created # Get the variant playlist created
createVariantPlaylist "$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_master.m3u8" createVariantPlaylist "$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_master.m3u8"
for br in $OP_BITRATES for br in $OP_BITRATES
do do
appendVariantPlaylistentry "$OUTPUT_DIRECTORY/${SEGMENT_DIRECTORY}${PLAYLIST_PREFIX}_master.m3u8" "${PLAYLIST_PREFIX}_${br}.m3u8" "$br" appendVariantPlaylistentry "$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_master.m3u8" "${SEGMENT_DIRECTORY}${PLAYLIST_PREFIX}_${br}.m3u8" "$br"
done done
   
OUTPUT_DIRECTORY+=$SEGMENT_DIRECTORY OUTPUT_DIRECTORY+=$SEGMENT_DIRECTORY
   
# Now for the longer running bit, transcode the video # Now for the longer running bit, transcode the video
for br in $OP_BITRATES for br in $OP_BITRATES
do do
BITRATE="-b:v ${br}k -bufsize ${br}k" if [ -z $QUALITY ]; then
  if $CONSTANT; then
  BITRATE="-b:v ${br}k -bufsize ${br}k -minrate ${br}k -maxrate ${br}k"
  else
  BITRATE="-b:v ${br}k"
  fi
  else
  BITRATE="-crf $QUALITY -maxrate ${br}k -bufsize ${br}k"
  if [ $VIDEO_CODEC = "libx265" ]; then
  BITRATE="$BITRATE -x265-params --vbv-maxrate ${br}k --vbv-bufsize ${br}k"
  fi
  fi
  echo "Bitrate options: $BITRATE"
# Finally, lets build the output filename format # Finally, lets build the output filename format
OUT_NAME="${SEGMENT_PREFIX}_${br}_%05d.ts" OUT_NAME="${SEGMENT_PREFIX}_${br}_%05d.ts"
PLAYLIST_NAME="$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_${br}.m3u8" PLAYLIST_NAME="$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_${br}.m3u8"
SOURCE_FILE="$INPUTFILE" SOURCE_FILE="$INPUTFILE"
echo "Generating HLS segments for bitrate ${br}k - this may take some time" echo "Generating HLS segments for bitrate ${br}k - this may take some time"
   
if [ "$NO_FORK" == "0" ] || [ "$LIVE_STREAM" == "1" ] if [ "$NO_FORK" == "0" ] || [ "$LIVE_STREAM" == "1" ]
then then
# Processing Starts # Processing Starts
if [ "$IS_FIFO" == "1" ] if [ "$IS_FIFO" == "1" ]
then then
# Create a FIFO specially for this bitrate # Create a FIFO specially for this bitrate
SOURCE_FILE="$TMPDIR/hlsc.encode.$MYPID.$br" SOURCE_FILE="$TMPDIR/hlsc.encode.$MYPID.$br"
mknod "$SOURCE_FILE" p mknod "$SOURCE_FILE" p
fi fi
   
# Schedule the encode # Schedule the encode
createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE" "$SOURCE_FILE" & createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE" "$SOURCE_FILE" &
PID=$! PID=$!
PIDS=(${PIDS[@]} $PID) PIDS=(${PIDS[@]} $PID)
BITRATE_PROCESSES=(${BITRATE_PROCESSES[@]} $br) BITRATE_PROCESSES=(${BITRATE_PROCESSES[@]} $br)
else else
createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE" "$SOURCE_FILE" createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE" "$SOURCE_FILE"
fi fi
   
done done
   
if [ "$IS_FIFO" == "1" ] if [ "$IS_FIFO" == "1" ]
then then
# If the input was a FIFO we need to read from it and push into the new FIFOs # If the input was a FIFO we need to read from it and push into the new FIFOs
cat "$INPUTFILE" | tee $(for br in $OP_BITRATES; do echo "$TMPDIR/hlsc.encode.$MYPID.$br"; done) > /dev/null & cat "$INPUTFILE" | tee $(for br in $OP_BITRATES; do echo "$TMPDIR/hlsc.encode.$MYPID.$br"; done) > /dev/null &
TEE_PID=$! TEE_PID=$!
fi fi
   
if [ "$NO_FORK" == "0" ] || [ "$LIVE_STREAM" == "1" ] if [ "$NO_FORK" == "0" ] || [ "$LIVE_STREAM" == "1" ]
then then
# Monitor the background tasks for completion # Monitor the background tasks for completion
echo "All transcoding processes started, awaiting completion" echo "All transcoding processes started, awaiting completion"
awaitCompletion awaitCompletion
   
  # As of HLS-20 encrypt will only run if the relevant vars are set
  encrypt
fi fi
   
if [ "$IS_FIFO" == "1" ] if [ "$IS_FIFO" == "1" ]
then then
for br in $OP_BITRATES for br in $OP_BITRATES
do do
rm -f "$TMPDIR/hlsc.encode.$MYPID.$br"; rm -f "$TMPDIR/hlsc.encode.$MYPID.$br";
done done
# If we were interrupted, tee may still be running # If we were interrupted, tee may still be running
kill $TEE_PID 2> /dev/null kill $TEE_PID 2> /dev/null
fi fi
   
else else
   
OUTPUT_DIRECTORY+=$SEGMENT_DIRECTORY OUTPUT_DIRECTORY+=$SEGMENT_DIRECTORY
# No bitrate specified # No bitrate specified
   
# Finally, lets build the output filename format # Finally, lets build the output filename format
OUT_NAME="${SEGMENT_PREFIX}_%05d.ts" OUT_NAME="${SEGMENT_PREFIX}_%05d.ts"
PLAYLIST_NAME="$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}.m3u8" PLAYLIST_NAME="$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}.m3u8"
   
echo "Generating HLS segments - this may take some time" echo "Generating HLS segments - this may take some time"
   
# Processing Starts # Processing Starts
   
createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE" "$INPUTFILE" createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE" "$INPUTFILE"
   
  # As of HLS-20 encrypt will only run if the relevant vars are set
fi encrypt
  fi
   
file:a/README.md -> file:b/README.md
HLS-Stream-Creator HLS-Stream-Creator
================== ==================
   
Introduction Introduction
------------- -------------
   
HLS-Stream-Creator is a simple BASH Script designed to take a media file, segment it and create an M3U8 playlist for serving using HLS. HLS-Stream-Creator is a simple BASH Script designed to take a media file, segment it and create an M3U8 playlist for serving using HLS.
There are numerous tools out there which are far better suited to the task, and offer many more options. This project only exists because I was asked to look There are numerous tools out there which are far better suited to the task, and offer many more options. This project only exists because I was asked to look
into HTTP Live Streaming in depth, so after reading the [IETF Draft](http://tools.ietf.org/html/draft-pantos-http-live-streaming-11 "HLS on IETF") I figured I'd start with the basics by creating a script to encode arbitrary video into a VOD style HLS feed. into HTTP Live Streaming in depth, so after reading the [IETF Draft](http://tools.ietf.org/html/draft-pantos-http-live-streaming-11 "HLS on IETF") I figured I'd start with the basics by creating a script to encode arbitrary video into a VOD style HLS feed.
   
   
   
Usage Usage
------ ------
   
Usage is incredibly simple Usage is incredibly simple
   
``` ```
./HLS-Stream-Creator.sh -[lf] [-c segmentcount] -i [inputfile] -s [segmentlength(seconds)] -o [outputdir] -b [bitrates] ./HLS-Stream-Creator.sh -[lf] [-c segmentcount] -i [inputfile] -s [segmentlength(seconds)] -o [outputdir] -b [bitrates]
   
   
Deprecated Legacy usage: Deprecated Legacy usage:
HLS-Stream-Creator.sh inputfile segmentlength(seconds) [outputdir='./output'] HLS-Stream-Creator.sh inputfile segmentlength(seconds) [outputdir='./output']
   
``` ```
   
So to split a video file called *example.avi* into segments of 10 seconds, we'd run So to split a video file called *example.avi* into segments of 10 seconds, we'd run
   
``` ```
./HLS-Stream-Creator.sh -i example.avi -s 10 ./HLS-Stream-Creator.sh -i example.avi -s 10
``` ```
   
**Arguments** **Arguments**
   
``` ```
Mandatory Arguments: Mandatory Arguments:
   
-i [file] Input file -i [file] Input file
-s [s] Segment length (seconds) -s [s] Segment length (seconds)
   
Optional Arguments: Optional Arguments:
   
-o [directory] Output directory (default: ./output) -o [directory] Output directory (default: ./output)
-c [count] Number of segments to include in playlist (live streams only) - 0 is no limit -c [count] Number of segments to include in playlist (live streams only) - 0 is no limit
  -e Encrypt the HLS segments (a key will be generated automatically)
-b [bitrates] Output video Bitrates in kb/s (comma seperated list for adaptive streams) -b [bitrates] Output video Bitrates in kb/s (comma seperated list for adaptive streams)
-p [name] Playlist filename prefix -p [name] Playlist filename prefix
-t [name] Segment filename prefix -t [name] Segment filename prefix
-l Input is a live stream -l Input is a live stream
-f Foreground encoding only (adaptive non-live streams only) -f Foreground encoding only (adaptive non-live streams only)
-S Name of a subdirectory to put segments into -S Name of a subdirectory to put segments into
  -2 Use two-pass encoding
  -q [quality] Change encoding to CFR with [quality]
  -C Use constant bitrate as opposed to variable bitrate
``` ```
   
   
Adaptive Streams Adaptive Streams
------------------ ------------------
   
As of [HLS-6](http://projects.bentasker.co.uk/jira_projects/browse/HLS-6.html) the script can now generate adaptive streams with a top-level variant playlist for both VoD and Linear input streams. As of [HLS-6](http://projects.bentasker.co.uk/jira_projects/browse/HLS-6.html) the script can now generate adaptive streams with a top-level variant playlist for both VoD and Linear input streams.
   
In order to create seperate bitrate streams, pass a comma seperated list in with the *-b* option In order to create seperate bitrate streams, pass a comma seperated list in with the *-b* option
   
``` ```
./HLS-Stream-Creator.sh -i example.avi -s 10 -b 28,64,128,256 ./HLS-Stream-Creator.sh -i example.avi -s 10 -b 28,64,128,256
``` ```
   
By default, transcoding for each bitrate will be forked into the background - if you wish to process the bitrates sequentially, pass the *-f* option By default, transcoding for each bitrate will be forked into the background - if you wish to process the bitrates sequentially, pass the *-f* option
   
``` ```
./HLS-Stream-Creator.sh -i example.avi -s 10 -b 28,64,128,256 -f ./HLS-Stream-Creator.sh -i example.avi -s 10 -b 28,64,128,256 -f
``` ```
   
In either case, in accordance with the HLS spec, the audio bitrate will remain unchanged In either case, in accordance with the HLS spec, the audio bitrate will remain unchanged
   
   
   
  Encrypted Streams
  -------------------
   
  HLS-Stream-Creator can also create encrypted HLS streams, it's enabled by passing *-e*
   
  ```
  ./HLS-Stream-Creator.sh -i example.avi -e -s 10 -b 28,64,128,256
   
  ```
   
  The script will generate a 128 bit key and save it to a *.key* file in the same directory as the segments. Each segment will be AES-128 encrypted using an IV which corresponds to it's segment number (the [default behaviour](https://developer.apple.com/library/content/technotes/tn2288/_index.html#//apple_ref/doc/uid/DTS40012238-CH1-ENCRYPT) for HLS).
   
  The manifests will then be updated to include the necessary `EXT-X-KEY` tag:
   
  ```
  #EXTM3U
  #EXT-X-VERSION:3
  #EXT-X-MEDIA-SEQUENCE:0
  #EXT-X-ALLOW-CACHE:YES
  #EXT-X-KEY:METHOD=AES-128,URI=big_buck_bunny_720p_stereo.avi.key
  #EXT-X-TARGETDURATION:17
  #EXTINF:10.500000,
  big_buck_bunny_720p_stereo.avi_1372_00000.ts
  ```
   
   
   
Output Output
------- -------
   
As of version 1, the HLS resources will be output to the directory *output*. These will consist of video segments encoded in H.264 with AAC audio and an m3u8 file in the format As of version 1, the HLS resources will be output to the directory *output* (unless a different directory has been specified with *-o*). These will consist of video segments encoded in H.264 with AAC audio and an m3u8 file in the format
   
>\#EXTM3U >\#EXTM3U
>\#EXT-X-MEDIA-SEQUENCE:0 >\#EXT-X-MEDIA-SEQUENCE:0
>\#EXT-X-VERSION:3 >\#EXT-X-VERSION:3
>\#EXT-X-TARGETDURATION:10 >\#EXT-X-TARGETDURATION:10
>\#EXTINF:10, no desc >\#EXTINF:10, no desc
>example_00001.ts >example_00001.ts
>\#EXTINF:10, no desc >\#EXTINF:10, no desc
>example_00002.ts >example_00002.ts
>\#EXTINF:10, no desc >\#EXTINF:10, no desc
>example_00003.ts >example_00003.ts
>\#EXTINF:5, no desc >\#EXTINF:5, no desc
>example_00004.ts >example_00004.ts
>\#EXT-X-ENDLIST >\#EXT-X-ENDLIST
   
   
   
Using a Specific FFMPEG binary Using a Specific FFMPEG binary
------------------------------- -------------------------------
   
There may be occasions where you don't want to use the *ffmpeg* that appears in PATH. At the top of the script, the ffmpeg command is defined, so change this to suit your needs There may be occasions where you don't want to use the *ffmpeg* that appears in PATH. At the top of the script, the ffmpeg command is defined, so change this to suit your needs
   
``` ```
FFMPEG='/path/to/different/ffmpeg' FFMPEG='/path/to/different/ffmpeg'
``` ```
   
   
  H265 details
  ------------
   
  Check has been added for libx265 to enforce bitrate limits for H265 since it uses additional parameters.
   
   
   
  Audio Codec Availability
  --------------------------
   
  Because *libfdk_aac* is a non-free codec, and is not available in all builds, commit 0796feb switched the default audio codec to *aac*.
   
  However, in older versions of ffmpeg, aac was marked as experimental - this includes the packages currently in the repos for Ubuntu Xenial. As a result, when running the script, you may see the following error
   
  ```
  The encoder 'aac' is experimental but experimental codecs are not enabled, add '-strict -2' if you want to use it.
  ```
   
  There are two ways to work around this. If you have the libfdk_aac codec installed, you can specify that it should be used instead
  ```
  export AUDIO_CODEC="libfdk_aac"
  ```
   
  Alternatively, you can update the ffmpeg flags to enable experimental codecs
  ```
  export FFMPEG_FLAGS='-strict -2'
  ```
   
  And the re-run HLS-Stream-Creator.
   
  [HLS-23](http://projects.bentasker.co.uk/jira_projects/browse/HLS-23.html) will, in future, update the script to check for this automatically.
   
   
   
   
Additional Environment Variables Additional Environment Variables
------------------------------- -------------------------------
   
There are few environment variables which can control the ffmpeg behaviour. There are few environment variables which can control the ffmpeg behaviour.
   
* `VIDEO_CODEC` - The encoder which will be used by ffmpeg for video streams. Examples: _libx264_, _nvenc_ * `VIDEO_CODEC` - The encoder which will be used by ffmpeg for video streams. Examples: _libx264_, _nvenc_
* `AUDIO_CODEC` - Encoder for the audio streams. Examples: _aac_, _libfdk_acc_, _mp3_, _libfaac_ * `AUDIO_CODEC` - Encoder for the audio streams. Examples: _aac_, _libfdk_acc_, _mp3_, _libfaac_
* `NUMTHREADS` - A number which will be passed to the `-threads` argument of ffmpeg. Newer ffmpegs with modern libx264 encoders will use the optimal number of threads by default. * `NUMTHREADS` - A number which will be passed to the `-threads` argument of ffmpeg. Newer ffmpegs with modern libx264 encoders will use the optimal number of threads by default.
* `FFMPEG_FLAGS` - Additional flags for ffmpeg. They will be passed without any modification. * `FFMPEG_FLAGS` - Additional flags for ffmpeg. They will be passed without any modification.
   
Example usage: Example usage:
   
``` ```
export VIDEO_CODEC="nvenc" export VIDEO_CODEC="nvenc"
export FFMPEG_FLAGS="-pix_fmt yuv420p -profile:v" export FFMPEG_FLAGS="-pix_fmt yuv420p -profile:v"
./HLS-Stream-Creator.sh example.avi 10 ./HLS-Stream-Creator.sh example.avi 10
``` ```
   
License License
-------- --------
   
HLS-Stream-Creator is licensed under the [BSD 3 Clause License](http://opensource.org/licenses/BSD-3-Clause) and is Copyright (C) 2013 [Ben Tasker](http://www.bentasker.co.uk) HLS-Stream-Creator is licensed under the [BSD 3 Clause License](http://opensource.org/licenses/BSD-3-Clause) and is Copyright (C) 2013 [Ben Tasker](http://www.bentasker.co.uk)
   
   
Issue Tracking Issue Tracking
---------------- ----------------
   
Although the Github issue tracker can be used, the bulk of project management (such as it is) happens in JIRA. See [projects.bentasker.co.uk](http://projects.bentasker.co.uk/jira_projects/browse/HLS.html) for a HTML mirror of the tracking. Although the Github issue tracker can be used, the bulk of project management (such as it is) happens in JIRA. See [projects.bentasker.co.uk](http://projects.bentasker.co.uk/jira_projects/browse/HLS.html) for a HTML mirror of the tracking.
   
  #EXTM3U
  #EXT-X-STREAM-INF:BANDWIDTH=436000
  false_436.m3u8
  #EXT-X-STREAM-INF:BANDWIDTH=128000
  false_128.m3u8
  #EXT-X-STREAM-INF:BANDWIDTH=512000
  false_512.m3u8