Allowed segment filename prefix to be specified. See HLS-9
Allowed segment filename prefix to be specified. See HLS-9

--- a/HLS-Stream-Creator.sh
+++ b/HLS-Stream-Creator.sh
@@ -63,6 +63,9 @@
 # leave null to use the input bitrate
 OP_BITRATES=${OP_BITRATES:-''}
 
+# Determines whether the processing for adaptive streams should run sequentially or not
+NO_FORK=${NO_FORK:-0}
+
 # Lets put our functions here
 
 
@@ -80,7 +83,7 @@
 See LICENSE
 
 
-Usage: HLS-Stream-Creator.sh -[l] [-c segmentcount] -i [inputfile] -s [segmentlength(seconds)] -o [outputdir] -b [bitrates]
+Usage: HLS-Stream-Creator.sh -[lf] [-c segmentcount] -i [inputfile] -s [segmentlength(seconds)] -o [outputdir] -b [bitrates]  [-p filename]
 
 	-i	Input file
 	-s	Segment length (seconds)
@@ -88,6 +91,9 @@
 	-l	Input is a live stream
 	-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)
+	-f	Foreground encoding only (don't fork the encoding processes into the background - adaptive non-live streams only)
+	-p	Playlist filename
+	-t	Segment filename prefix
 
 Deprecated Legacy usage:
 	HLS-Stream-Creator.sh inputfile segmentlength(seconds) [outputdir='./output']
@@ -123,8 +129,7 @@
     $bitrate \
     $FFMPEG_ADDITIONAL \
     $FFMPEG_FLAGS \
-    $OUTPUT_DIRECTORY/"$output_name" 
-
+    "$OUTPUT_DIRECTORY/$output_name"
 }
 
 
@@ -147,6 +152,28 @@
 }
 
 
+function awaitCompletion(){
+# Monitor the encoding pids for their completion status
+while [ ${#PIDS[@]} -ne 0 ]; do
+    # Calculate the length of the array
+    pid_length=$((${#PIDS[@]} - 1))
+
+    # Check each PID in the array
+    for i in `seq 0 $pid_length`
+    do
+	  # Test whether the pid is still active
+	  if ! kill -0 ${PIDS[$i]} 2> /dev/null
+	  then
+		echo "Encoding for bitrate ${BITRATE_PROCESSES[$i]}k completed"
+		unset BITRATE_PROCESSES[$i]
+		unset PIDS[$i]
+	  fi
+    done
+    PIDS=("${PIDS[@]}") # remove any nulls
+    sleep 1
+done
+}
+
 
 # This is used internally, if the user wants to specify their own flags they should be
 # setting FFMPEG_FLAGS
@@ -159,7 +186,7 @@
 LEGACY_ARGS=1
 
 # If even one argument is supplied, switch off legacy argument style
-while getopts "i:o:s:c:b:l" flag
+while getopts "i:o:s:c:b:p:t:lf" flag
 do
 	LEGACY_ARGS=0
         case "$flag" in
@@ -169,6 +196,9 @@
 		l) LIVE_STREAM=1;;
 		c) LIVE_SEGMENT_COUNT="$OPTARG";;
 		b) OP_BITRATES="$OPTARG";;
+		f) NO_FORK=1;;
+		p) PLAYLIST_PREFIX="$OPTARG";;
+		t) SEGMENT_PREFIX="$OPTARG";;
         esac
 done
 
@@ -204,15 +234,6 @@
   exit 1
 fi
 
-
-# Now we want to make sure out input file actually exists
-# This will need tweaking in future if we want to allow a RTMP stream (for example) to be used as input
-if ! [ -f "$INPUTFILE" ]
-then
-  echo "Error: You gave me an incorrect filename. Please re-run specifying something that actually exists!"
-  exit 1
-fi
-
 # Check output directory exists otherwise create it
 if [ ! -w $OUTPUT_DIRECTORY ]
 then
@@ -234,15 +255,19 @@
 # Pulls file name from INPUTFILE which may be an absolute or relative path.
 INPUTFILENAME=${INPUTFILE##*/}
 
-# Will look at making this configurable later
-PLAYLIST_PREFIX=$INPUTFILENAME
-
+# If a prefix hasn't been specified, use the input filename
+PLAYLIST_PREFIX=${PLAYLIST_PREFIX:-$INPUTFILENAME}
+SEGMENT_PREFIX=${SEGMENT_PREFIX:-$PLAYLIST_PREFIX}
 
 # Set the bitrate
 if [ ! "$OP_BITRATES" == "" ]
 then
       # Make the bitrate list easier to parse
       OP_BITRATES=${OP_BITRATES//,/$'\n'}
+
+      # Create an array to house the pids for backgrounded tasks
+      declare -a PIDS
+      declare -a BITRATE_PROCESSES
 
       # Get the variant playlist created
       createVariantPlaylist "$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_master.m3u8"
@@ -255,28 +280,40 @@
       for br in $OP_BITRATES
       do
 	      BITRATE="-b:v ${br}k -bufsize ${br}k"
-
 	      # Finally, lets build the output filename format
-	      OUT_NAME="${INPUTFILENAME}_${br}_%05d.ts"
+	      OUT_NAME="${SEGMENT_PREFIX}_${br}_%05d.ts"
 	      PLAYLIST_NAME="$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}_${br}.m3u8"
 
 	      echo "Generating HLS segments for bitrate ${br}k - this may take some time"
 
-	      # Processing Starts
-
-	      createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE"
-
+	      if [ "$NO_FORK" == "0" ] || [ "$LIVE_STREAM" == "1" ]
+	      then
+		      # Processing Starts
+		      createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE" &
+		      PID=$!
+		      PIDS=(${PIDS[@]} $PID)
+		      BITRATE_PROCESSES=(${BITRATE_PROCESSES[@]} $br)
+	      else
+		      createStream "$PLAYLIST_NAME" "$OUT_NAME" "$BITRATE"
+	      fi
 	      # Will deal with exit statuses shortly.
 	      #|| exit 1
 
       done
 
+      if [ "$NO_FORK" == "0" ] || [ "$LIVE_STREAM" == "1" ]
+      then
+	    # Monitor the background tasks for completion
+	    echo "All transcoding processes started, awaiting completion"
+	    awaitCompletion
+      fi
+
 else
 
   # No bitrate specified
 
   # Finally, lets build the output filename format
-  OUT_NAME="${INPUTFILENAME}_%05d.ts"
+  OUT_NAME="${SEGMENT_PREFIX}_%05d.ts"
   PLAYLIST_NAME="$OUTPUT_DIRECTORY/${PLAYLIST_PREFIX}.m3u8"
 
   echo "Generating HLS segments - this may take some time"
@@ -287,3 +324,4 @@
 
 
 fi
+

file:a/README.md -> file:b/README.md
--- a/README.md
+++ b/README.md
@@ -16,7 +16,7 @@
 Usage is incredibly simple
 
 ```
-./HLS-Stream-Creator.sh -[l] [-c segmentcount] -i [inputfile] -s [segmentlength(seconds)] -o [outputdir]
+./HLS-Stream-Creator.sh -[lf] [-c segmentcount] -i [inputfile] -s [segmentlength(seconds)] -o [outputdir] -b [bitrates]
 
 
 Deprecated Legacy usage:
@@ -33,14 +33,39 @@
 **Arguments**
 
 ```
+    Mandatory Arguments:
+
 	-i [file]	Input file
 	-s [s]  	Segment length (seconds)
+
+    Optional Arguments:
+
 	-o [directory]	Output directory (default: ./output)
 	-c [count]	Number of segments to include in playlist (live streams only) - 0 is no limit
-	-l	Input is a live stream
+	-b [bitrates]	Output video Bitrates in kb/s (comma seperated list for adaptive streams)
+	-l		Input is a live stream
+	-f		Foreground encoding only (adaptive non-live streams only)
+```
+
+
+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.
+
+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
+```
 
+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
+```
+
+In either case, in accordance with the HLS spec, the audio bitrate will remain unchanged
 
 
 Output