Skip to content

Commit

Permalink
Video loops now
Browse files Browse the repository at this point in the history
  • Loading branch information
makidoll committed Apr 19, 2019
1 parent c809978 commit 589e9b2
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion express-routes/video-sync/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ const mongoose = require("mongoose");
const express = require("express");
const path = require("path");
const fs = require("fs");
const { spawn } = require("child_process");

let getVideoDurationFromURL = url=>new Promise((resolve,reject)=>{
let args = "-v error -select_streams v:0 -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "+url;
const ffprobe = spawn("ffprobe", args.split(" "));

ffprobe.stdout.on("data", data=>{
try {
let dur = data.toString();
dur = parseFloat(dur);
return resolve(dur);
} catch(err) {
return reject();
};
});

ffprobe.stderr.on("data", data=>{
reject();
});
});

module.exports = function(opts) {

Expand Down Expand Up @@ -72,12 +92,17 @@ module.exports = function(opts) {
src: "",
playing: false,
position: 0,
duration: -1,
volume: 0.05,
backdrop: "",
};

setInterval(()=>{
if (room.info.playing) room.info.position++;
if (room.info.position>room.info.duration) {
room.info.position = 0;
room.io.emit("getInfo", {position: room.info.position});
}
}, 1000);

// database
Expand Down Expand Up @@ -304,7 +329,14 @@ module.exports = function(opts) {
socket.on("setInfo", info=>{
if (info.password != room.password) return;

if (info.src!=undefined) room.info.src = info.src;
if (info.src!=undefined) {
room.info.src = info.src;
getVideoDurationFromURL(info.src).then(duration=>{
room.info.duration = duration;
}).catch(err=>{
room.info.duration = -1;
});
}
if (info.playing!=undefined) room.info.playing = info.playing;
if (info.position!=undefined) room.info.position = info.position;
if (info.volume!=undefined) room.info.volume = info.volume;
Expand Down

0 comments on commit 589e9b2

Please sign in to comment.