Implement ability to specify resolution with bitrates. See HLS-27
Implement ability to specify resolution with bitrates. See HLS-27

This is just a first pass at this, and I fully expect it to break in a variety of weird and wonderful ways.

There's also some reliance on globals that I don't like (see comments in file), so that's still in need of tidying up. We should probably also think about testing that none of the given resolutions are higher than that of the source.

But, you can now pass a resolution in as part of the bitrate string: {{bitrate-widthxheight}}

As an example, ran the following test encode
{noformat}$ ./HLS-Stream-Creator.sh -i testvid.mp4 -b 2744,1000-1280x720 -s 10
{noformat}
The original is 1080p at 2744k so that should give us a 1080p rendition as well as a 720p.

Master playlist is generated correctly
{noformat}$ cat testvid_master.m3u8
testvid_2744.m3u8
testvid_1000-1280x720.m3u8
{noformat}

Segments report approximately the right bitrate (used vbr to speed up the test), and also the correct resolution
{noformat}$ ffprobe -i testvid_1000_00001.ts
Input #0, mpegts, from 'testvid_1000_00001.ts':
Duration: 00:00:13.70, start: 12.890956, bitrate: 989 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 1280x720 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0x101](und): Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 100 kb/s

$ ffprobe -i testvid_2744_00001.ts
Input #0, mpegts, from 'testvid_2744_00001.ts':
Duration: 00:00:13.70, start: 12.890956, bitrate: 2335 kb/s
Program 1
Metadata:
service_name : Service01
service_provider: FFmpeg
Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc
Stream #0:1[0x101](und): Audio: aac (LC) ([15][0][0][0] / 0x000F), 44100 Hz, stereo, fltp, 100 kb/s
{noformat}

Not been tested with a linear stream yet (though it _should_ work)

--- a/HLS-Stream-Creator.sh
+++ b/HLS-Stream-Creator.sh
@@ -129,6 +129,15 @@
 bitrate="$3"
 infile="$4"
 
+
+# Resolution comes from global $resolution
+#
+# See HLS-27
+#
+# I don't like this use of $resolution when it's not local, but there was already a use 
+# of non-local $br in here so I'll fix this later
+
+
 local PASSVAR=
 if $TWOPASS; then
 	local LOGFILE="$OUTPUT_DIRECTORY/bitrate$br"
@@ -141,11 +150,12 @@
 		-vcodec libx264 \
 		-f mpegts \
 		$bitrate \
+		$resolution \
 		$FFMPEG_ADDITIONAL \
 		-loglevel error -y \
 		/dev/null
 fi
-
+echo "Using res $resolution aaaa"
 $FFMPEG -i "$infile" \
     $PASSVAR \
     -y \
@@ -159,6 +169,7 @@
     -segment_list "$playlist_name" \
     -segment_time "$SEGLENGTH" \
     -segment_format mpeg_ts \
+    $resolution \
     $bitrate \
     $FFMPEG_ADDITIONAL \
     $FFMPEG_FLAGS \
@@ -175,10 +186,19 @@
 function appendVariantPlaylistentry(){
 playlist_name=$1
 playlist_path=$2
-playlist_bw=$(( $3 * 1000 )) # bits not bytes :)
+bw_statement=$3
+m3u8_resolution=''
+
+if [[ "$bw_statement" == *"-"* ]]
+then
+    m3u8_resolution=",RESOLUTION=$(echo "$bw_statement" | cut -d- -f2)"
+    bw_statement=$(echo "$bw_statement" | cut -d- -f1) 
+fi
+
+playlist_bw=$(( $bw_statement * 1000 )) # bits not bytes :)
 
 cat << EOM >> "$playlist_name"
-#EXT-X-STREAM-INF:BANDWIDTH=$playlist_bw
+#EXT-X-STREAM-INF:BANDWIDTH=$playlist_bw$m3u8_resolution
 $PATH_PREFIX$playlist_path
 EOM
 
@@ -413,6 +433,16 @@
       # Now for the longer running bit, transcode the video
       for br in $OP_BITRATES
       do
+      
+              # Check whether there's a resolution included in the bitrate string
+              #
+              # See HLS-27
+              if [[ "$br" == *"-"* ]]
+              then
+                resolution="-vf scale=$(echo "$br" | cut -d- -f2 | sed 's/x/:/')"
+                br=$(echo "$br" | cut -d- -f1) 
+              fi
+      
               if [ -z $QUALITY ]; then
 		if $CONSTANT; then
 	          BITRATE="-b:v ${br}k -bufsize ${br}k -minrate ${br}k -maxrate ${br}k"