Update README to contain more information on encrypted streams (HLS-20)
Update README to contain more information on encrypted streams (HLS-20)

--- a/HLS-Stream-Creator.sh
+++ b/HLS-Stream-Creator.sh
@@ -216,27 +216,32 @@
         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"')
 
-    count=0
+    echo "Encrypting Segments"
     for file in ${OUTPUT_DIRECTORY}/*.ts
     do
-        ENC_FILENAME="$OUTPUT_DIRECTORY/${SEGMENT_PREFIX}_enc_${count}".ts
-
-	INIT_VECTOR=$(printf '%032x' $count)
+        SEG_NO=$( echo "$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 $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 $file
-
-        count=$((count+1))
+        
     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

file:a/README.md -> file:b/README.md
--- a/README.md
+++ b/README.md
@@ -42,6 +42,7 @@
 
 	-o [directory]	Output directory (default: ./output)
 	-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)
 	-p [name]	Playlist filename prefix
 	-t [name]	Segment filename prefix
@@ -72,6 +73,34 @@
 ```
 
 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

--- /dev/null
+++ b/output/false_master.m3u8
@@ -1,1 +1,8 @@
+#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