streaming
This commit is contained in:
parent
4a620ab7a3
commit
0e339fc88d
4 changed files with 27 additions and 16 deletions
|
@ -635,6 +635,7 @@ nav ul li.active {
|
||||||
#yt .go {
|
#yt .go {
|
||||||
width: 96px;
|
width: 96px;
|
||||||
height: 96px;
|
height: 96px;
|
||||||
|
justify-content: center;
|
||||||
}
|
}
|
||||||
#yt .go:disabled {
|
#yt .go:disabled {
|
||||||
position: relative;
|
position: relative;
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
.go {
|
.go {
|
||||||
width: 96px;
|
width: 96px;
|
||||||
height: 96px;
|
height: 96px;
|
||||||
|
justify-content: center;
|
||||||
|
|
||||||
&:disabled {
|
&:disabled {
|
||||||
position: relative;
|
position: relative;
|
||||||
|
|
16
app/js/yt.js
16
app/js/yt.js
|
@ -5,6 +5,11 @@ import * as ui from "./lib/ui.js";
|
||||||
import * as conf from "./conf.js";
|
import * as conf from "./conf.js";
|
||||||
|
|
||||||
let node;
|
let node;
|
||||||
|
const decoder = new TextDecoder("utf-8");
|
||||||
|
|
||||||
|
function decodeChunk(byteArray) {
|
||||||
|
return decoder.decode(byteArray);
|
||||||
|
}
|
||||||
|
|
||||||
async function onClick(e) {
|
async function onClick(e) {
|
||||||
let url = prompt("Please enter a YouTube URL:");
|
let url = prompt("Please enter a YouTube URL:");
|
||||||
|
@ -19,11 +24,16 @@ async function onClick(e) {
|
||||||
let body = new URLSearchParams();
|
let body = new URLSearchParams();
|
||||||
body.set("url", url);
|
body.set("url", url);
|
||||||
let response = await fetch("/youtube", {method:"POST", body});
|
let response = await fetch("/youtube", {method:"POST", body});
|
||||||
let text = await response.text();
|
|
||||||
|
let reader = response.body.getReader();
|
||||||
|
while (true) {
|
||||||
|
let { done, value } = await reader.read();
|
||||||
|
if (done) { break; }
|
||||||
|
p.textContent += decodeChunk(value);
|
||||||
|
}
|
||||||
|
reader.releaseLock();
|
||||||
|
|
||||||
button.disabled = false;
|
button.disabled = false;
|
||||||
p.textContent = text;
|
|
||||||
|
|
||||||
if (response.status == 200) {
|
if (response.status == 200) {
|
||||||
mpd.command(`update ${mpd.escape(conf.ytPath)}`);
|
mpd.command(`update ${mpd.escape(conf.ytPath)}`);
|
||||||
}
|
}
|
||||||
|
|
25
index.js
25
index.js
|
@ -2,7 +2,13 @@ const static = require("node-static");
|
||||||
const app = new static.Server("./app");
|
const app = new static.Server("./app");
|
||||||
const port = Number(process.argv[2]) || 8080;
|
const port = Number(process.argv[2]) || 8080;
|
||||||
|
|
||||||
|
const cmd = "youtube-dl";
|
||||||
|
//const cmd = "./test.sh";
|
||||||
|
|
||||||
function downloadYoutube(url, response) {
|
function downloadYoutube(url, response) {
|
||||||
|
response.setHeader("Content-Type", "text/plain"); // necessary for firefox to read by chunks
|
||||||
|
// response.setHeader("Content-Type", "text/plain; charset=utf-8");
|
||||||
|
|
||||||
// FIXME create directory
|
// FIXME create directory
|
||||||
console.log("YouTube downloading", url);
|
console.log("YouTube downloading", url);
|
||||||
let args = [
|
let args = [
|
||||||
|
@ -10,12 +16,10 @@ function downloadYoutube(url, response) {
|
||||||
"-o", `${__dirname}/_youtube/%(title)s-%(id)s.%(ext)s`,
|
"-o", `${__dirname}/_youtube/%(title)s-%(id)s.%(ext)s`,
|
||||||
url
|
url
|
||||||
]
|
]
|
||||||
let child = require("child_process").spawn("youtube-dl", args);
|
let child = require("child_process").spawn(cmd, args);
|
||||||
let stdOut = "";
|
|
||||||
let stdErr = "";
|
|
||||||
|
|
||||||
child.stdout.setEncoding("utf8").on("data", chunk => stdOut += chunk);
|
child.stdout.setEncoding("utf8").on("data", chunk => response.write(chunk));
|
||||||
child.stderr.setEncoding("utf8").on("data", chunk => stdErr += chunk);
|
child.stderr.setEncoding("utf8").on("data", chunk => response.write(chunk));
|
||||||
|
|
||||||
child.on("error", error => {
|
child.on("error", error => {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
@ -24,14 +28,9 @@ function downloadYoutube(url, response) {
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on("close", code => {
|
child.on("close", code => {
|
||||||
if (code == 0) {
|
if (code != 0) { // fixme
|
||||||
console.log("OK");
|
|
||||||
response.end(stdOut);
|
|
||||||
} else {
|
|
||||||
console.log(code, stdOut, stdErr);
|
|
||||||
response.writeHead(500);
|
|
||||||
response.end(stdErr);
|
|
||||||
}
|
}
|
||||||
|
response.end();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,4 +59,4 @@ function onRequest(request, response) {
|
||||||
|
|
||||||
let httpServer = require("http").createServer(onRequest).listen(port);
|
let httpServer = require("http").createServer(onRequest).listen(port);
|
||||||
require("ws2mpd").ws2mpd(httpServer, `http://localhost:${port}`);
|
require("ws2mpd").ws2mpd(httpServer, `http://localhost:${port}`);
|
||||||
require("ws2mpd").logging(false);
|
//require("ws2mpd").logging(false);
|
||||||
|
|
Loading…
Reference in a new issue