m3u8-uri.sh 676 B

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