LoginSignup
1
2

More than 1 year has passed since last update.

WSLのUbuntuでyoutube音楽をGoogleHomeで操作する

Last updated at Posted at 2018-12-25

はじめに

はじめての投稿です。よろしくお願いします。
●youtube音楽を声で操作し、PCスピーカで再生できるようにする。
【操作例】GoogleHomeに「youtube音楽 山下達郎 クリスマスイブ」と話す
#環境
●Windows10 HOMEのPCにWSLのubuntuをインストールする。
●ubuntuでnpm,node.js,firebase,youtube-dl,mplayerをインストールする
●iftttでGoogleアシスタント(this)でfirebaseにテキストをPUTする(that)ように設定する
●firebaseのDatabaseはgooglehome-wordにしています
●WSLのubuntuでpulseaudioでPCのスピーカを使うようにする
●Windows側でpulseaudioサーバのインストールが必要です
 https://www.cendio.com/thinlinc/download の 「Client Bundle」のリンクからダウンロードできます

プログラム(node.js)

●firebaseとyoutubeのKeyは自分のKeyに置き換えてください
●Googlehomeからのテキストが「停止」、「止めて」以外の場合、youtube音楽を検索して再生します。

const firebase = require("firebase");
var iconv = require('iconv-lite');
var value1 ;
var str1 = '" | sed -e "s/^/\'/" -e '
var str2 = '"s/$/\'/" | xargs -n1 mplayer'
var command ;

//firebase config
var config = {
    apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxx.firebaseapp.com",
    databaseURL: "https://xxxxxxxxxx.firebaseio.com",
    projectId: "xxxxxxxxxx",
    storageBucket: "xxxxxxxx.appspot.com",
    messagingSenderId: "xxxxxxxxxx"
};
firebase.initializeApp(config);

const Youtube = require('youtube-node');
const youtube = new Youtube();
youtube.setKey('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');

//jsonからvalueに一致する値取得
const getJsonData = (value, json) => {
  for (var word in json)  if (value == word) return json[word]
  return json["default"]
}

//database更新時
const path = "/googlehome"
const key = "word"
const db = firebase.database()
db.ref(path).on("value", function(changedSnapshot) {
//値取得
let value = changedSnapshot.child(key).val()
if (!value) return
console.log(value);
var keyword = value.replace(/ /g,"");

//コマンド
const STOP1 = "pkill -KILL -f mplayer";

if(value === "停止"){
  command = STOP1
} else if(value === "止め て"){
  command = STOP1
} else {
  command = '未定義'
}
console.log(command)

//コマンド実行
if(command === STOP1){
  var exec = require('child_process').exec;
  exec(command, {maxBuffer: 1000*1024}, function(error, stdout, stderr) {
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
  });
}
if(command === '未定義') {
  var exec = require('child_process').exec;
  exec(STOP1, {maxBuffer: 1000*1024}, function(error, stdout, stderr) {
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
  });
  play_youtube(keyword);
}
//firebase clear
db.ref(path).set({[key]: ""});
})

function play_youtube(keyword) {
  youtube.search(keyword, 1, {'type':'video','videoCategoryId':10} , function(error, result) {
    if (error) {
      console.log(error);
      return ;
    }
    //console.log(JSON.stringify(result, null, 2));
    for (const item of result.items) {
      if (item.id.videoId) {
        var exec = require('child_process').exec;
        command = "youtube-dl 'https://www.youtube.com/watch?v="+item.id.videoId+"' -o - | mplayer - -novideo"
        console.log(command);
        exec(command,  {maxBuffer: 40000*1024},function(error, stdout, stderr) {
          if (error !== null) {
            console.log('exec error: '+error);
          }
        });
      }
    }
  });
}

まとめ

●お気に入りのPCスピーカでyoutubue音楽が流せます
●プログラムはラズパイでも同様に動作しますが、youtube-dlの動作が遅く、再生開始に少々時間がかかります。
●次回、GoogleHomeでPC内音楽データを操作する記事を投稿予定です

参考資料

GoogleHomeとIFTTTとFirebaseとラズパイとnode.jsでyoutubeの音を流す

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