0
1

More than 1 year has passed since last update.

WSLのUbuntu環境でPC内音楽データをWeb操作する(おまけでyoutube音楽とGooglehomeで伝言)

Last updated at Posted at 2019-11-27

#はじめに
●PC内音楽データをWEBで操作し、PCスピーカで再生できるようにする。
(Googlehomeで声で操作するのWeb操作版です。声でキーワードがうまく入力できない場合に使用してます)
【操作例】WEBから「竹内まりや」と入力する
PC音楽ボタンを押すと次の曲が再生します。(複数のファイルが検索一致した場合)
コメント 2019-11-27 230824.png
【追加】2020-4-29
曲検索でAND検索機能を追加しました。
なごり雪&イルカ なごり雪&かぐや姫で再生する曲が変わる。
区切り文字は全角の&としてます。
【追加】2020-5-1
playlist作成とmplayerの再生で同期が取れず、playlistが未作成で再生しようとするので、
setTimeoutで遅延をいれました。
●おまけ
youtube音楽:下記の記事と同じです。
 WSLのUbuntu環境でyoutube音楽をWeb操作する(おまけでradikoとサイマルラジオ)
伝言:Googlehomeで伝言を再生します。google-home-notifierを使用。

#環境
●Windows10 HOMEのPCにWSLのubuntuをインストールする。
●ubuntuでapache2,npm,node.js,youtube-dl,mplayerをインストールする
●WSLのubuntuでpulseaudioでPCのスピーカを使うようにする
●Windows側でpulseaudioサーバのインストールが必要です
 https://www.cendio.com/thinlinc/download の 「Client Bundle」のリンクからダウンロードできます

#PC内音楽データを再生するプログラム(pcplay.js)
●単体で起動する方法
$node pcplay.js 検索キー(例 竹内まりや)

const exec   = require('child_process').exec;

var limit = 1;
var items;
var item;
var title;
var id;

//値取得
const value = process.argv[2];
if (!value) return
console.log(value)
const value3 = value.split('&');

// キーワードから検索し、音楽データのパスを取得する。
play_pcmusic(value3);
// 音楽ファイルのパスを取得し、再生する。
function play_pcmusic(keyword) {
  command = 'cat /mnt/j/music/htdocs/music/list.txt | grep "'  + keyword + '"';
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 40000*1024},function(error, stdout, stderr) {
    if (error !== null) {
      command = 'curl -X POST -d "text=指定された曲はありません" http://192.168.1.82:8080/google-home-notifier'
      exec(command, {maxBuffer: 40000*1024}, function(error, stdout, stderr) {
        if (error !== null) {
          console.log('Exec error: ' + error);
        }
      });
    } else {
      console.log(stdout);
    }
 });
  if(keyword.length == 1) {
    command = 'cat /mnt/j/music/htdocs/music/list.txt | grep "'  + keyword[0] + '">/mnt/j/music/htdocs/music/playlist';
  } else {
    command = 'cat /mnt/j/music/htdocs/music/list.txt | grep '  + keyword[0] + ' | grep ' + keyword[1] + '>/mnt/j/music/htdocs/music/playlist';
  }
  console.log(command)
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 40000*1024}, 
function(error, stdout, stderr) {
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
  });
  function mplay(){
  command = 'mplayer -shuffle -playlist /mnt/j/music/htdocs/music/playlist';
  console.log(command)
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 40000*1024},function(error, stdout, stderr) {
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
  });
  }
  setTimeout(mplay,3000);
}

#index.php

<?php
if(isset($_GET['comment1'])){
$comment = $_GET['comment1'];
exec("killall mplayer");
exec("node /home/ユーザ名/ytplay.js " .$comment);
}
if(isset($_GET['comment2'])){
$comment = $_GET['comment2'];
exec("killall mplayer");
exec("node /home/ユーザ名/pcplay.js " .$comment);
}
if(isset($_GET['comment3'])){
$comment = $_GET['comment3'];
exec("killall mplayer");
exec("curl -X POST -d 'text=".$comment."' http://192.168.1.82:8080/google-home-notifier");
}
if(isset($_GET['id'])){
$id = $_GET['id'];
if($id=="stop"){
exec("killall mplayer");
}
}
?>
<html>
<head>
<meta name="viewport" content="width=device-width">
</head>
<body>
<p><a href="index.php?id=stop">stop</a></p>
<form action="index.php" method="get">
<input type="text" name=comment1>
<input type="submit" value="youtube">
</form>
<form action="index.php" method="get">
<input type="text" name=comment2>
<input type="submit" value="PC音楽">
</form>
<form action="index.php" method="get">
<input type="text" name=comment3>
<input type="submit" value="伝言">
</form>
</body>
</html>

まとめ

●お気に入りのPCスピーカでPC内の音楽データを再生します
●プログラムはラズパイでも同様に動作しますが、youtube-dlの動作が遅く、再生開始に少々時間がかかります。

#参考
WSLのUbuntuでPC内の音楽データをGoogleHomeで操作する(shuffle再生)
WSLのUbuntuでyoutube音楽をGoogleHomeで操作する
WSLのUbuntuでPC内の音楽データをGoogleHomeで操作する
WSLのUbuntu環境でyoutube音楽をWeb操作する

0
1
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
0
1