プログラミング

WSL bashでvlcで音楽をランダム再生する音楽プレイヤー

Windows 10のWSL Linux bashでvlcで音楽をランダム再生する音楽プレイヤープログラムみたいなものをスクリプトしたのでシェアします。

成り行きで書いた。あとvlcじゃなくてもたぶんいけますね。まぁvlcしか使わないですね。

追記

現在バージョンアップして以下になっています。バグもほぼありません。動画ファイルも対応。


shufmusic(){

    : Play randomized music on current directory with vlc.exe. compatible with wsl bash.
    : e.g. shufmusic
    : e.g. shufmusic mp4

# find -regextype posix-extended -regex '.*\.(wav|mp3|flac|wave)' > list.txt
  # find -L -regextype posix-extended -regex '.*\.(wav|mp3|flac|wave|mp4)' > list.txt
    [[ "${1}" =~ mp4|video ]] && find -L -regextype posix-extended -regex '.*\.(wav|mp3|flac|wave|mp4|avi)' > shufmusic_list.tmp.txt || find -L -regextype posix-extended -regex '.*\.(wav|mp3|flac|wave)' > shufmusic_list.tmp.txt

    # [ -s shufmusic_list.tmp.txt ] || ( printf "\n\n${red}E: The list file empty. It's seems something worng.${end}\nINFO: You may have to use ${cyn}shufmusic mp4${end}.\n\n" ; return 1 )
      # [ ! -s shufmusic_list.tmp.txt ] && return 1
        [ -s shufmusic_list.tmp.txt ] || { printf "\n\n${red}E: The list file empty. It's seems something worng.${end}\nINFO: You may have to use ${cyn}shufmusic mp4${end}.\n\n" ; return 1 ; }

    printf "INFO: $(wc -l shufmusic_list.tmp.txt | awk '{print $1}') files found.\n"

# while [[ "$?" == 0 ]];
  : issue -- bash return while and function. return 1 not work in while in function.
while [ true ];
do
  sleep 1

  [ -s shufmusic_list.tmp.txt ] && currentMusic="$(cat shufmusic_list.tmp.txt | shuf | head -n 1)" || { printf "\n\n${red}E: The list file is being empty.${end}\n\n" ; return 1 ; }

  [ -z "${currentMusic}" ] && { printf "\n\n${red}E: \$currentMusic is empty.${end}\n\n" ; return 1 ; }

    perl -i -pe "BEGIN{undef $/;} s/$(printf "%q" "$currentMusic" | sed -Ee 's/\//\\\//g')\n//smg" shufmusic_list.tmp.txt
    # vlc.exe "${currentMusic}" &
    vlc.exe "$(wslpath -w "$(realpath "${currentMusic}")")" &
    # sleep "$(sox --info -D "${currentMusic}")" || return 1
    [[ "${currentMusic##*.}" =~ mp4|avi ]] && sleep "$(ffprobe -i "${currentMusic}" -show_entries format=duration -v quiet -of csv="p=0")" || sleep "$(sox --info -D "${currentMusic}")"
    # sleep "$(sox --info -D "${currentMusic}")" || ( printf "${red}E:${end} No music found." ; return 1 )
  sleep 1
done

}

自分で作っておいてあれですが、なかなか便利です。良さげな曲を発見したらそのファイルのあるフォルダにcdしてこれを実行するだけで、ランダム再生できるわけなので、とりあえず音楽流したい的な需要にぴったり。
vlcにもプレイリストの機能はありますけど、D&Dするの面倒だし、flacだと重いし。こちらのコマンドなら、シンボリックリンクを置いたディレクトリ下で実行すれば、数万のハイレゾファイルでも難なくランダム再生できます。いいね。

コードは更新があった場合はgithubか.bashrcに上げます。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です