How can I generate an M3U playlist (http URL format) from the terminal?

21,564

Solution 1

I think the following one-liner should work:

for f in *.mp3; do echo "http://..../$f" >> play.m3u; done

Solution 2

This is @chronitis answer with some improvements :

  • stores the file name on the variable $playlist for later use
  • will delete the file if exists previously
  • writes the full path of the file on the playlist

The command

playlist='play.m3u' ; if [ -f $playlist ]; then rm $playlist ; fi ; for f in *.mp3; do echo "$(pwd)/$f" >> "$playlist"; done

To play it with mplayer on the command line also

mplayer -playlist play.m3u

Solution 3

You originally asked to create each entry as a web URL formatted line. In addition to replacing the local path with http://..., you'll also need to replace spaces with '%20'. So, long line, but here you go:

find /path/to/mp3s/ -name "*.mp3" | sed 's/ /%20/g' | sed 's|/path/to/mp3s/|http://www.server.com/serverpath/|g' > playlist.m3u

Solution 4

This bash script can do the job:

rawurlencode() {
  local string="${1}"
  local strlen=${#string}
  local encoded=""
  local pos c o

  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  echo "${encoded}"
}

rm -rf p.m3u
for f in *.mkv; do echo "#EXTINF:-1,SR:$f
  http://10.0.0.144/tvtmp/"$(rawurlencode $f) >> p.m3u; 
done
sed -i '1s/^/#EXTM3U\n/' p.m3u
rm -rf l.m3u
for f in *.mkv; do echo "#EXTINF:-1,SR:$f
  http://127.0.0.1/tvtmp/$f" >> l.m3u; 
done
sed -i '1s/^/#EXTM3U\n/' l.m3u

A little more developed version. The URL is encoded in proper .m3u style.

Share:
21,564
3k-
Author by

3k-

Updated on September 18, 2022

Comments

  • 3k-
    3k- over 1 year

    I'd like to generate a M3U playlist for a directory containing mp3 files on my server from the terminal. Since I'd like to ensure that every player will be able to stream those files I'd like to prefix each file entry with the absolute URL to that directory, like this:

    http://server.com/dir/file1.mp3
    http://server.com/dir/file2.mp3
    ...
    

    So unfortunately simply doing ls -1 *.mp3 > play.m3u isn't enough. Is there a one-liner to achieve this?

  • Geppettvs D'Constanzo
    Geppettvs D'Constanzo over 9 years
    I don't know why somebody downvoted this answer. This is the only solution that seems to work right out of the box and should be accepted or at least promoted. Thank you very much!
  • 3k-
    3k- over 7 years
    good idea! using a plus (+) sign instead would result in a more readable URL though
  • Sumeet Deshmukh
    Sumeet Deshmukh about 7 years
    what if i want to add multiple file formats into this command?