#!/bin/bash #Author: Copyright Jelle de Jong #Note: Please send me an email if you enhanced this script #Version: 0.0.8 #Date: 23-07-2006 / .... / 04-08-07 / 27-10-07 / 20-01-2008 / 2009-01-05 #System: Ubuntu/Debian GNU/Linux #Information: man mpg123 ogg123 flac123 #Information: http://www.grymoire.com/Unix/Quote.html #Information: http://www.tldp.org/LDP/abs/html/arrays.html #Information: http://rafb.net/p/WOTlt235.html #Category Thunar scripts #Name: Play Music #Description: Play a directory full with mpg3/flac/ogg files #Command: xfce4-terminal --disable-server --hide-toolbars --execute /usr/share/pct-thunar-scripts/scripts/play-music %F #Patterns: *.ogg;*.flac;*.mp3;*.wma;*.OGG;*.FLAC;*.MP3;*.WMA; #Directories: True #Audio Files: True # Did the script work for you? # Yes # Yes, but with some errors # Yes, but I had to change some things # Not at all # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. TEXTDOMAINDIR=/usr/share/locale TEXTDOMAIN=${0##*/} NAME=$"Play Music" DESCRIPTION=$"Play a directory full with mpg3/flac/ogg files" GUIPLAYER=0 cecho() { case "$1" in "black") echo -ne "\E[30m" ;; "red") echo -ne "\E[31m" ;; "green") echo -ne "\E[32m" ;; "yellow") echo -ne "\E[33m" ;; "blue") echo -ne "\E[34m" ;; "magenta") echo -ne "\E[35m" ;; "cyan") echo -ne "\E[36m" ;; *) echo -ne "\E[30m" exit ;; esac echo "$2" "$3" tput sgr0 } cecho "green" $"program:" echo "$0" cecho "green" $"description:" echo "$DESCRIPTION" play_music_file() { shopt -s dotglob filename="mp3playlist.txt" sort_mp3_album "$@" if [ -e "$filename" ] then echo $"muine: load playlist" check_program muine export LANG=nl_NL.UTF-8 xargs --delimiter=$'\n' --arg-file="$filename" muine fi echo $"removing playlist" rm "$filename" } play_mp3_file() { # mpg123 -C -v -a /dev/dsp1 "$@" # aoss mpg123 -C -v "$@" shopt -s dotglob filename="mp3playlist.txt" if [ "$mp3playlist" = 0 ] then sort_mp3_album "$@" fi if [ -e "$filename" ] then echo $"mpg123: load playlist" check_program mpg123 check_program screen #aoss mpg123 --buffer 1024 --control --verbose --title --list "$filename" #mpg123 --output alsa --buffer 1024 --control --verbose --title --list "$filename" #screen -S mpg123 mpg123 --output alsa --buffer 1024 --control --verbose --title --list "$filename" mpg123 --output alsa --buffer 1024 --control --verbose --title --list "$filename" fi echo $"removing playlist" rm "$filename" } play_ogg_file() { check_program ogg123 aoss ogg123 "$@" } play_flac_file() { check_program ogg123 #~ ogg123 --verbose --device alsa --prebuffer 100 --buffer 2048 --audio-buffer 2048 "$@" aoss ogg123 "$@" } check_program() { #whereis pdftk application="$1" if [ ! -e /usr/bin/$application ] then cecho "red" $"$application not found, installing now" if [ "$application" = "ogg123" ] then application="vorbis-tools" fi if [ "$application" = "aoss" ] then application="alsa-oss" fi sudo apt-get install "$application" fi } sort_mp3_album() { declare -a mp3_query declare -a array_album declare -a array_number unset debug mp3_query=("$@") # set -x echo $"sorting: query: start" for file in "${mp3_query[@]}" do album=$(mp3info -p %l "$file" 2> /dev/null) [ "$album" == "" ] && album="unkown" && echo $"album: empty: set: $album" && debug=1 number=$(mp3info -p %n "$file" 2> /dev/null) [ "$number" == "" ] && number="empty" && debug=1 count=1 match=0 step=1000 for value in "${array_album[@]}" do if [ "$value" = "$album" ] then match="$count" && continue fi ((count++)) done if [ "$match" != 0 ] then index=$(($match * $step)) else array_album["$count"]="$album" index=$(($count * $step)) fi step=1000 length=0 for value in ${!array_number[*]} do ((value >= index && value < (index + step) && value > length)) && length=$value done count=1 match=0 empty=0 while [ $(($index + $count)) -le $(($length + 1)) ] do value=${array_number[$(($index + $count))]} if [ "$value" = "$file" ] then match="$count" echo $"duplicate file: skip" continue fi if [ $empty = 0 ] && [ "$value" = "" ] then empty="$count" fi ((count++)) done if [[ "$number" = *[^0-9]* ]] then [ $empty == 0 ] && empty=$count number=$empty echo $"number: empty: set: $number" fi if [ "$match" == 0 ] then step=1000 index=$(($index + $number)) check=${array_number[$(($index))]} while [ -n "$check" ] do index=$(($index + $step)) check=${array_number[$(($index))]} done array_number["$index"]="$file" fi # debug: # echo ${!array_album[*]} # echo ${array_album[*]} # echo ${!array_number[*]} # echo ${array_number[*]} done echo $"sorting: query: finished" # set +x if [ "$debug" = 1 ] then echo "sorting: summary" echo "debug: $debug" echo "array_album: ${!array_album[*]}" # echo "array_album: ${array_album[*]}" echo "array_number: ${!array_number[*]}" # echo "array_number: ${array_number[*]}" fi echo $"sorting: save playlist" IFS=$'\n' echo "${array_number[*]}" >> "$filename" } check_mp3playlist() { shopt -s dotglob filename="mp3playlist.txt" echo $"sorting: check playlist" if [ -e "$filename" ] then modificationtime_playlist=$(stat --printf='%Y' "$filename") unixtime_date="$modificationtime_playlist" unixtime_buffer=$(date -d "today - 24 hours" +%s) if [ -n "$unixtime_date" ] && [ -n "$unixtime_buffer" ] && [[ ! "$unixtime_date" = *[^0-9]* ]] && [[ ! "$unixtime_buffer" = *[^0-9]* ]] then if [ "$unixtime_date" -lt "$unixtime_buffer" ] then echo $"found playlist: playlist older then 24 hours, removing the playlist" rm --verbose "$filename" return 0 else echo $"found playlist: playlist newer then 24 hours, using the playlist" fi fi else return 0 fi last_access_time=0 last_acces_file=0 number=1 count=0 while read line do ((count++)) if [ -e "$line" ] then accesstime=$(stat --printf='%X\n' "$line") ((accesstime > last_access_time && accesstime > modificationtime_playlist)) && last_access_time=$accesstime && last_acces_file=$line && number=$count fi done < "$filename" echo $"last_access_time: $last_access_time" echo $"last_acces_file: $last_acces_file" echo $"accesstime: $accesstime" echo $"count: $count" echo $"number: $number" sed -i -n "$number,${count}p" "$filename" return 1 } scan_directory() { #step 1: is there a mp3 in the folder #step 2: query that mp3 #step 3: check for other folders #step 4: scan that folder and repeat #find / -type f -name '*.mp3' -exec mpg123 -C -v {} \; #query+=$(find "$1" -name *.mp3) #var1="test one.mp3";var2="test two.mp3";unset query;set -x;query+=("$var1");query+=("$var2");mpg123 -C -v -a /dev/dsp "${query[@]}" for file in "$1"/* do if [ -f "$file" ] then extension=${file##*.} extension=$(echo $extension | tr '[:upper:]' '[:lower:]') [[ "$extension" = mp3 ]] && mp3_query+=("$file") [[ "$extension" = ogg ]] && ogg_query+=("$file") [[ "$extension" = flac ]] && flac_query+=("$file") [[ "$extension" = wma ]] && wma_query+=("$file") fi if [ -d "$file" ] then scan_directory "$file" fi done } cecho "green" $"processing:" for I in "$@" do filename=${I##*/} cecho "magenta" "$filename" done cecho "blue" "---------------" check_program aoss check_program mp3info if [ $GUIPLAYER = 1 ] then [ -f mp3playlist.txt ] && rm mp3playlist.txt else check_mp3playlist mp3playlist="$?" fi unset mp3_query unset ogg_query unset flac_query for I in "$@" do cecho "green" $"starting: " echo $"filling: query:" ${I##*/} if [ -f "$I" ] then extension=${I##*.} extension=$(echo $extension | tr '[:upper:]' '[:lower:]') [[ "$extension" = mp3 ]] && mp3_query+=("$I") [[ "$extension" = ogg ]] && ogg_query+=("$I") [[ "$extension" = flac ]] && flac_query+=("$I") [[ "$extension" = wma ]] && wma_query+=("$I") fi if [ -d "$I" ] then scan_directory "$I" fi cecho "green" $"completed:" cecho "blue" "---------------" done if [ $GUIPLAYER = 1 ] then music_query=("${mp3_query[@]}") music_query+=("${ogg_query[@]}") music_query+=("${flac_query[@]}") music_query+=("${wma_query[@]}") play_music_file "${music_query[@]}" else if [ -n "$mp3_query" ] then play_mp3_file "${mp3_query[@]}" fi if [ -n "$ogg_query" ] then play_ogg_file "${ogg_query[@]}" fi if [ -n "$flac_query" ] then play_flac_file "${flac_query[@]}" fi fi cecho "green" $"done, closing after 3 seconds" sleep 3 exit