5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mplayerで再生中の曲をtmuxのステータスラインに表示する方法

Last updated at Posted at 2014-09-24

mplayer

曲の再生

まず、曲の再生はディレクトリ単位で行います。そのためには、以下の様なスクリプトを実行することにします。

pecoを使ってますが、その箇所は、好きなインターフェイスをお使いください。あと、個人的な事情から、再生速度は2倍速にしています。

ここで重要なのは、ログを~/Music/info.txtに出力していることです。

mplayer_play.sh
# !/bin/bash

sea="ID_FILENAME"
dir1=$HOME/Music
rm -rf $dir1/*/.DS_Store >/dev/null 2>&1

dir2="$dir1/"`zsh -c "ls -A $dir1 | peco"`
pla=`mplayer -novideo -speed 2 -af scaletempo,volnorm -loop 20 -quiet -msglevel all=0 -identify $dir2/* > $dir1/info.txt`

# cat $dir1/info.txt | grep FILE | tail -n 1 | xargs basename | cut -b 1-6

rm $dir1/info.txt

tmuxのステータスラインに曲を表示する

次に、tmuxのステータスラインに現在再生中の曲を表示します。先ほどのログ~/Music/info.txtから再生中のファイル名を取得します。

~/.tmux.conf
set-option -g status-left "#(cat ~/Music/info.txt | grep FILE | tail -n 1 | xargs basename)"

ここで、曲名が長すぎる場合は、cut -b 1-6などしましょう。

曲名を流す方法

for文

しかし、短いスペースで全部のタイトルを表示する最もオススメな方法は、指定の文字数以内で文字列を流すことです。適当にスクリプトを書いてみます。

mplayer_name.sh
# !/bin/bash

dir=$HOME/Music
cha=`cat $dir/info.txt | grep FILE | tail -n 1 | xargs basename`
sum=`echo $cha | wc -c | tr -d ' '`

for (( i = 1; i < $sum; i=`expr $i + 4` ))
do
    a=`expr $i + 4`
    b=$i"-"$a
    cat $dir/info.txt | grep FILE | tail -n 1 | xargs basename | cut -b $b
    sleep 0.3
done

mplayerを実行中に上のスクリプトを実行すると、曲のタイトルが流れます。しかし、forを使っているため、そのままでは、tmuxには使えません。

if文

したがって、数字を保存しておいて、1個ずつ実行していく方法を採用します。スマートではないですが、一応、これでtmuxのステータスラインに再生中の曲名を流すことができます。

文字化けする場合は、cut -d '¥n'を使ってみてください。その他、スペースの問題などもありますので、最終的には以下の形になりました。

mplayer_tmux.sh
# !/bin/zsh

dir=$HOME/Music
cha=`cat $dir/info.txt | grep FILE | tail -n 1 | cut -d '/' -f 6 | sed "s/\.[^.]*$//"`
# cha=`cat $dir/info.txt | grep FILE | tail -n 1 | xargs basename`
sum=`echo $cha | wc -c | tr -d ' '`
fil=$dir/info.txt
inf=$dir/info.s

if [ -e $fil ]; then
    if [ -e $inf ];then
        i=`cat $inf | tail -n 1`
        if [ $i -le $sum ]; then
            a=`expr $i + 4`
            b=$i"-"$a
            c=`cat $fil | grep FILE | tail -n 1 | cut -d '/' -f 6 | sed "s/\.[^.]*$//" | cut -c $b | tr -d '\n'` > /dev/null
            echo -ne "| ♫ $c"
            echo `expr $i + 1` >> $inf
        else
            rm $inf
        fi
    else
        echo 1 > $inf
    fi
fi

tmuxのステータスラインに曲名を流す

上に書いたスクリプトを指定します。

~/.tmux.conf
set-option -g status-left "#(/path/to/mplayer_tmux.sh)"

一応、GIF画像撮ってみました。ステータスラインにも曲名が流れています。

適当に書いたものなので、改善、調整してくれる人がいるとありがたいです。もっとスマートなやり方があったら、是非教えて下さい。また、便利なオプションあったら教えて欲しいです。

https://github.com/syui/mplayer_script

拡張子を除く方法

echo test.mp4 | sed "s/\.[^.]*$//" 

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?