#!/bin/sh # generate a m3u playlist of all files in current directory # poor urlescape, most are useless. urlescape() { sed ' s/%/%25/g; s/[[:space:]]/%20/g; s//%3E/g; s/\#/%23/g; s/{/%7B/g; s/}/%7D/g; s/|/%7C/g; s/\\/%5C/g; s/\^/%5E/g; s/~/%7E/g; s/\[/%5B/g; s/\]/%5D/g; s/`/%60/g; s/;/%3B/g; s/?/%3F/g; s/:/%3A/g; s/@/%40/g; s/:/%3D/g; s/&/%26/g; s/\$/%24/g; ' } usage() { printf "usage:\n\t" printf "%s (-h) (-u )\n" "$0" printf "\t-h : print help\n" printf "\t-f : print fast run, no metadata\n" printf "\t-u : prepend to path for streaming\n" printf "\t\t i.e. -u "http://example.com/audio/"\n" exit } fast=0 while getopts 'hfu:' c do case $c in h) usage ;; u) url="${OPTARG}" ;; f) fast=1 ;; esac done if [ $fast != 1 ]; then printf "#EXTM3U\n\n" fi find . -type f \ -iname '*.flac' -o \ -iname '*.mp3' -o \ -iname '*.m4a' -o \ -iname '*.ogg' -o \ -iname '*.opus' -o \ -iname '*.webm'| \ sed 's/^\.\///' |\ while read -r line; do if [ $fast != 1 ]; then duration="$(ffprobe -v 0 -show_entries stream=duration "${line}" |\ awk -F '=' '/duration=/ {sub("\\..*", "", $2); printf $2; exit}')" title="$(ffprobe -v 0 -show_entries 'format_tags=title' "${line}" |\ awk -F '=' '/title=/ {print $2}')" artist="$(ffprobe -v 0 -show_entries 'format_tags=artist' "${line}" |\ awk -F '=' '/artist=/ {print $2}')" test -z "${title}" && title="$(basename "${line}")" printf "#EXTINF:${duration}, %s - %s\n" "${artist}" "${title}" fi path="$(printf "%s" "${line}" | urlescape)" printf "${url}%s\n\n" "${path}" done