LoginSignup
0
2

More than 5 years have passed since last update.

WSLのUbuntuでyoutube動画をGoogleHomeで操作する

Posted at

はじめに

「WSLのUbuntuでyoutube音楽をGoogleHomeで操作する」の動画バージョンです。
●youtube動画を声で操作し、PCで再生できるようにする。
【操作例】GoogleHomeに「youtube音楽 門あさ美」と話す

環境(「WSLのUbuntuでyoutube音楽をGoogleHomeで操作する」と同じです)

●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 ;

function sleep(time) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        },time);
    });
}

//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 = "/mnt/c/Windows/System32/taskkill.exe /im chrome.exe";

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

//コマンド実行
if(command === STOP1){
  sleep(2000);
  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 === '未定義') {
  sleep(2000);
  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'} , 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 = "'/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe' --start-maximized --incognito 'https://www.youtube.com/watch?v="+item.id.videoId+"'"
        console.log(command);
        exec(command,  {maxBuffer: 40000*1024},function(error, stdout, stderr) {
          if (error !== null) {
            console.log('exec error: '+error);
          }
        });
        var exec = require('child_process').exec;
        //全画面表示させるためにUWSCでキーfを入力する
        command = "/mnt/c/uwsc520/UWSC.exe 'c:/uwsc520/key-f.UWS'"
        console.log(command);
        exec(command,  {maxBuffer: 40000*1024},function(error, stdout, stderr) {
          if (error !== null) {
            console.log('exec error: '+error);
          }
        });
      }
    }
  });
}

まとめ

●声でyoutubue動画を再生させます(Chromecast不要)
●chromeでyoutubeを再生します
●動画切り替えのためにchromeをkillしてからchromeを起動してます
chromeの子タスクも終了するので、バックグランドで起動している雨アラームなどは終了します。
●Chromecastのように「次の動画」は使用できません
●chrome起動で、全画面表示しないのでUWSCで強制的にfキーを押しています

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