Uncategorized

Tech tip #2: MPlayer play music recursively in a directory.

I have always wondered how to do this. It’s quite often I have directories full of media files (specifically music) and subdirectories within them also with music files, and though the manual for MPlayer is thicker than the Chinese phonebook I have not been able to find any option for it.

I did however find a method which isn’t exactly the guru one-liner, but here it is anyway. It’s broken into two steps, the first to create a playlist:

find -maxdepth 1 -type f -name \*.\* > playlist

Then finally play the playlist:

mplayer -playlist playlist

Just add a -loop 0 suffix if you want to loop :)

Dion Moult

I've been developing software for well over 10 years, work as an architect (not the computer kind, the regular sort), and am classically trained as a pianist. I try to do the right thing when I get the chance in my field, such as through contributing to open-source communities and promoting sustainable living.

More Posts

Spread the love

8 Comments

  1. Hi.

    I just found your site, and was reading through some old post when I found this. I figured I’d leave a few tips for anyone who might care.

    First, I assume you have a good reason for only wanting to parse one level deep, and only files with a dot in the name. For my music collection, I found this more practical:

    find -type f

    Next, if you are using bash, you can use <() to redirect the output of one command to a temporary file descriptor. This file descriptor is opened in /dev/fd on most systems, so if we want to use this in a one-liner, we need find to output absolute paths. Otherwise, mplayer will assume the file locations are relative to /dev/fd, and not the current directory. We can do this by giving find a start path.

    find "$PWD" -type f

    So the final one-liner becomes:

    mplayer -playlist <(find "$PWD" -type f)

    And the best part is that you don't have to clean up your playlist, or update it when you add more music to the file system.

    By the way, try both these commands to see what's really going on:

    echo <(echo foobar)
    cat <(echo foobar)

    Cheers,
    OC

    PS: Love your blog design. It makes me want to create one again, but I never have the patience to keep them alive.

  2. Thanks Ole Christian Tvedt! A quick test in my Music directory and now I’ve solved one of my woes on the computer :P

    Glad you enjoyed the blog design and thanks for reading!

  3. Searching google for this problem landed me here…

    I wanted to have my files played in alphabetical order and only avi files (since a couple .srt subtitle files were in there…), so I came up with this :

    mplayer -playlist <(find "$PWD" -name "*.avi" -type f | sort)

    of course if there's only audio files then

    mplayer -playlist <(find "$PWD" -type f | sort)

  4. I needed to do the same on windows, so here is a dirty solution that works even with files and dirs containing spaces and non-ascii characters since everything is converted to short dos names (via `~s`).

    ====== mplayer_recursive.cmd =============

    set mplayer=z:\path_to_mplayer\mplayer.exe
    for /f “delims=” %%f in (‘dir /b /s “%cd%”‘) do (%mplayer% “%%~sf”)
    ==========================================

  5. Was working on this too and had some trouble with getting it perfect. Combining my work with this yields something like:

    #!/bin/zsh
    #or change it to bash, but not sh
    mplayer -loop 0 -playlist <(find "$PWD" -type f | egrep -i '(\.mp3|\.wav|\.flac|\.ogg|\.avi|\.flv|\.mpeg|\.mpg)'| sort)

    Or you can hit it up as an alias:
    alias mplall="mplayer -loop 0 -playlist <(find \"$PWD\" -type f | egrep -i '(\.mp3|\.wav|\.flac|\.ogg|\.avi|\.flv|\.mpeg|\.mpg)'| sort)"

  6. @daid: Also remember the -name, -iname, -regex and -iregex flags of find. Something like this for your command:

    find “$PWD” -type f -iregex ‘.*\.\(mp3\|wav\|flac\|ogg\|avi\|flv\|mpe?g\)’

    Notice that find uses emacs style regexen, so other characters needs escaping. Optionally add | sort at the end. If you prefer your egrep variant, you should probably add a $ at the end of your expression to match the end of line character.

Leave a Reply

Your email address will not be published. Required fields are marked *