Made it easier to wrap the script with another BASH script
Made it easier to wrap the script with another BASH script

--- a/HLS-Stream-Creator.sh
+++ b/HLS-Stream-Creator.sh
@@ -38,9 +38,10 @@
 ######################################################################################
 
 # Basic config
+OUTPUT_DIRECTORY=${OUTPUT_DIRECTORY:-'./output'}
 
 # Change this if you want to specify a path to use a specific version of FFMPeg
-FFMPEG='ffmpeg'
+FFMPEG=${FFMPEG:-'ffmpeg'}
 
 # Lets put our functions here
 
@@ -59,7 +60,7 @@
 See LICENSE
 
 
-Usage: HLS-Stream-Creator.sh inputfile segmentlength(seconds)
+Usage: HLS-Stream-Creator.sh inputfile segmentlength(seconds) [outputdir='./output']
 
 EOM
 
@@ -76,7 +77,7 @@
 
 # We'll add some more headers in a later version, basic support is all we need for now
 # The draft says we need CRLF so we'll use SED to ensure that happens
-cat << EOM | sed 's/$/\r/g' > output/$1.m3u8
+cat << EOM | sed 's/$/\r/g' > $OUTPUT_DIRECTORY/$1.m3u8
 #EXTM3U
 #EXT-X-TARGETDURATION:$2
 #EXT-X-MEDIA-SEQUENCE:0
@@ -91,7 +92,7 @@
 #
 function append_segment(){
 
-cat << EOM | sed 's/$/\r/g' >> output/$1.m3u8
+cat << EOM | sed 's/$/\r/g' >> $OUTPUT_DIRECTORY/$1.m3u8
 #EXTINF:$2
 $3
 EOM
@@ -105,7 +106,7 @@
 # Usage: close_m3u8 streamname
 #
 function close_m3u8(){
-cat << EOM | sed 's/$/\r/g' >> output/$1.m3u8
+cat << EOM | sed 's/$/\r/g' >> $OUTPUT_DIRECTORY/$1.m3u8
 #EXT-X-ENDLIST
 EOM
 }
@@ -119,9 +120,12 @@
 # Basic Usage is going to be
 # cmd.sh inputfile segmentlength 
 
-INPUTFILE=$1
-SEGLENGTH=$2
-
+INPUTFILE=${INPUTFILE:-$1}
+SEGLENGTH=${SEGLENGTH:-$2}
+if ! [ -z "$3" ]
+then
+  OUTPUT_DIRECTORY=$3
+fi
 
 # Check we've got the arguments we need
 if [ "$INPUTFILE" == "" ] || [ "$SEGLENGTH" == "" ]
@@ -132,7 +136,7 @@
 
 
 # FFMpeg is a pre-requisite, so let check for it
-if hash ffmpeg 2> /dev/null
+if hash $FFMPEG 2> /dev/null
 then
   # FFMpeg exists
   echo "ffmpeg command found.... continuing"
@@ -159,7 +163,7 @@
 #
 # Essentially we want to create the chunks by running
 #
-# ffmpeg -i "$INPUTFILE" -vcodec libx264 -acodec mp3 -ss "START_POINT" -t "$SEGLENGTH" -f mpegts output/"$INPUTFILE"_"$N".ts
+# ffmpeg -i "$INPUTFILE" -vcodec libx264 -acodec mp3 -ss "START_POINT" -t "$SEGLENGTH" -f mpegts $OUTPUT_DIRECTORY/"$INPUTFILE"_"$N".ts
 
 # First we need the duration of the video
 DURATION=$($FFMPEG -i "$INPUTFILE" 2>&1 | grep Duration | cut -f 4 -d ' ')
@@ -170,7 +174,7 @@
 DUR_X=$(echo "$DURATION" | cut -d ':' -f 3 | cut -d '.' -f 1)
 
 # Calculate the duration in seconds
-let "DURATION_S = ( DUR_H * 60 + DUR_M ) * 60 + DUR_X"
+DURATION_S=$(expr \( $DUR_H \* 60 + $DUR_M \) \* 60 + $DUR_X)
 
 
 # Check we've not got empty media
@@ -186,10 +190,15 @@
 START_POS='0'
 let 'N_FILES = DURATION_S / SEGLENGTH + 1'
 
-# For now, INPUTFILENAME is going to == INPUTFILE
-# Later, we'll change so that INPUTFILE could be an absolute path, whilst INPUTFILENAME will just be the filename
-INPUTFILENAME=$INPUTFILE
-
+# Check output directory exists otherwise create it
+if [ ! -w $OUTPUT_DIRECTORY ]
+then
+  echo "Creating $OUTPUT_DIRECTORY"
+  mkdir -p $OUTPUT_DIRECTORY
+fi
+
+# Pulls file name from INPUTFILE which may be an absolute or relative path.
+INPUTFILENAME=${INPUTFILE##*/}
 
 # Create the M3U8 file
 create_m3u8 "$INPUTFILENAME" "$SEGLENGTH"
@@ -197,8 +206,6 @@
 # Finally, lets build the output filename format
 OUT_NAME=$INPUTFILENAME"_%03d.ts"
 
-
-
 # Processing Starts
 
 while [ "$START_POS" -lt "$DURATION_S" ]
@@ -206,7 +213,7 @@
 
   OUTPUT=$( printf "$OUT_NAME" "$N" )
   echo "Creating $OUTPUT ($N/$N_FILES)..."
-  $FFMPEG -i "$INPUTFILE" -loglevel quiet -vcodec libx264 -acodec mp3 -ss "$START_POS" -t "$SEGLENGTH" -f mpegts output/"$OUTPUT"
+  $FFMPEG -i "$INPUTFILE" -loglevel quiet -vcodec libx264 -acodec mp3 -ss "$START_POS" -t "$SEGLENGTH" -f mpegts $OUTPUT_DIRECTORY/"$OUTPUT"
 
   let "N = N + 1"
   let "START_POS = START_POS + SEGLENGTH"
@@ -214,7 +221,7 @@
   # If we're on the last segment, the duration may be less than the seglenth, so we need to reflect this in the m3u8
   if ! [ "$START_POS" -lt "$DURATION_S" ]
   then
-    SEG_DURATION=$($FFMPEG -i output/"$OUTPUT" 2>&1 | grep Duration | cut -f 4 -d ' ')
+    SEG_DURATION=$($FFMPEG -i $OUTPUT_DIRECTORY/"$OUTPUT" 2>&1 | grep Duration | cut -f 4 -d ' ')
     # Now we need to break out the duration into a time we can use
     DUR_H=$(echo "$SEG_DURATION" | cut -d ':' -f 1)
     DUR_M=$(echo "$SEG_DURATION" | cut -d ':' -f 2)
@@ -231,3 +238,4 @@
 
 # Add the close tag (ffplay gives some weird behaviour without this!)
 close_m3u8 "$INPUTFILENAME"
+

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 inputfile segmentlength(seconds)
+./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
@@ -32,19 +32,20 @@
 
 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 MP3 audio (should be AAC really, but I'd compiled *ffmpeg* without) and an m3u8 file in the format
 
->#EXTM3U
->#EXT-X-MEDIA-SEQUENCE:0
->#EXT-X-VERSION:3
->#EXT-X-TARGETDURATION:10
->#EXTINF:10, no desc
->example_001.ts
->#EXTINF:10, no desc
->example_002.ts
->#EXTINF:10, no desc
->example_003.ts
->#EXTINF:5, no desc
->example_004.ts
->#EXT-X-ENDLIST
+>\#EXTM3U  
+>\#EXT-X-MEDIA-SEQUENCE:0  
+>\#EXT-X-VERSION:3  
+>\#EXT-X-TARGETDURATION:10  
+>\#EXTINF:10, no desc  
+>example_001.ts  
+>\#EXTINF:10, no desc  
+>example_002.ts  
+>\#EXTINF:10, no desc  
+>example_003.ts  
+>\#EXTINF:5, no desc  
+>example_004.ts  
+>\#EXT-X-ENDLIST
+