Finding and Playing Multimedia in Linux

http://www.64Lines.com »» Use Linux to search and play multimedia files «

Overview

When using Linux as your operating system, your are presented with a wide array of options for your multimedia needs. The simplest solution that I found to play multimedia files is to simple use the command line program mplayer. If you want to play multiple files, a list of locations has to be supplied. To simplify the process of finding the files this script uses slocate, known as the locate command in the shell. This script is extremely simple to use: just supply a list of keywords (artists, titles, albums) and it will create and play the playlist

Installation and Usage

We suggest saving the script into your home directory under the name play_media.sh
The script file must have execute permissions, use chmod +x play_media.sh to add execute permissions

Source Code

Play Multimedia in Linux in just 28 lines #!/bin/bash
#Generate a list of files from the supplied arguments and convert it into a playlist

temp_file=`tempfile 2>/dev/null` || temp_file=/temp/$1$$
trap "rm -f $temp_file" 0 1 2 5 15
file_count=0
until [ -z "$1" ]
do
   echo -n "Searching for $1... "
   file_count=$(cat $temp_file|wc -l)
   locate -i $1 | grep -i -e mp3$ -e ogg$ -e wma$>> $temp_file
   new_files=`expr $(cat $temp_file|wc -l) - $file_count`
   echo -e "found $new_files files"
   shift
done
echo -e "
***FOUND A TOTAL OF $(cat $temp_file|wc -l) FILES***"
echo "Press v to view the files, otherwise press any key"

old_set=$(stty -g)
stty -icanon -echo
key=$(dd bs=1 count=1 2> /dev/null)
stty "$old_set"
if [ "$key" = "v" ]
then
   less $temp_file
fi
mplayer -shuffle -loop 0 -playlist $temp_file | grep -e "^Playing" -e "^\ "
echo "Finished playing $(cat $temp_file|wc -l) files"

References

Official English MPlayer documentation - a great document to look up options for mplayer if the man page is not available.
Online copy of the slocate man page - learn more about slocate at Linux Command, a great place for novices to learn the command line.

Check out the other 64Lines.com articles, tutorials, and tools

All article content is copyright of Michael Petrov, 2012©.
The source code presented in the articles is distributed as freeware, please feel free to use it in your own projects - both commercial and non commercial.
Valid HTML 4.01 TransitionalValid CSS!