m3u8.sh 567 B

1234567891011121314151617181920212223
  1. #!/bin/bash
  2. # Command-line parameters
  3. M3U8="${1}"
  4. PREFIX="${2}"
  5. if [ ! -r "${M3U8}" ]; then
  6. echo "Usage: ${0} input_m3u8 [uri_prefix]" 1>&2
  7. exit 1
  8. fi
  9. # Find the highest-resolution/highest-bandwidth stream
  10. BANDWIDTH="`cat "${M3U8}" | grep '^#EXT-X-STREAM-INF:' | \
  11. sed 's%^.*[,:]BANDWIDTH=\([0-9]*\).*,RESOLUTION=\([0-9]*\)x\([0-9]*\).*$%\2 \1%' | \
  12. sort -n | tail -n 1 | cut -d ' ' -f 2`"
  13. # Extract the URI from the line after that stream
  14. URI="`cat "${M3U8}" | grep -A1 "BANDWIDTH=${BANDWIDTH}" | tail -n 1`"
  15. # Output
  16. echo "${PREFIX}${URI}"
  17. # Cleanup
  18. exit 0