const fs = require('fs'); const path = require('path'); const express = require('express'); const app = express(); const fileUpload = require('express-fileupload'); const fetch = require('node-fetch'); app.use(express.json()); app.use(fileUpload()); const port = 8080; app.listen(port, () => { console.log(`Server is running at http://localhost:${port}`); }); app.use(express.static('public')); let videoDirs = [ './public/videos', '/media/pi', '/video' ]; let fileEndings = [ '.mp4', '.mkv', '.avi', '.mov', '.flv', '.wmv', '.webm', '.m4v', '.mpg', '.mpeg', '.3gp', '.3g2', '.m2v', '.m4v' ]; app.get('/videos', (req, res) => { const list = []; for (const dir of videoDirs) { try { if (!fs.existsSync(dir)) { continue; } fs.readdirSync(dir, { recursive: true }).forEach(fileName => { if (!fileEndings.includes(path.extname(fileName))) { return; } const file = { name: fileName, path: path.resolve(dir, fileName) }; list.push(file); }); } catch (error) { console.error(error); } } res.json(list); }); let process = null; let mayRestart = false; if (fs.existsSync('/selectedvideo')) { const initialVideo = fs.readFileSync('/selectedvideo'); startVideo(initialVideo); } function startVideo(path) { const newProcess = require('child_process').spawn('ffplay', ['-fs', '-loop', '2147483647', '-hide_banner', '-an', '-loglevel', 'error', path]); // process.stdout.on('data', (data) => { // console.log(`stdout: ${data}`); // }); // process.stderr.on('data', (data) => { // console.error(`stderr: ${data}`); // }); // newProcess.on("exit", (code) => { // if (!mayRestart && code !== 0) { // startVideo(path); // } // }); setTimeout(() => { if (process) { process.kill(); } // newProcess.on("exit", (code) => { // if (!mayRestart && code !== 0 && process === newProcess) { // startVideo(path); // } // }); process = newProcess; }, 5000); } fs.mkdirSync('/video', { recursive: true }); app.post('/play', (req, res) => { if (process) { mayRestart = true; process.kill(); mayRestart = false; } let video = req.query.video; console.log(video); if (video.startsWith('/media/pi')) { fs.copyFileSync(video, path.join('/video/', path.basename(video))); video = path.join('/video/', path.basename(video)) } fs.writeFileSync('/selectedvideo', video); startVideo(video); res.json({ status: 'ok' }); }); app.post("/select", (req, res) => { let video = req.query.video; fs.writeFileSync('/overlayvideo', video); res.json({ status: 'ok' }); }); app.post("/playoverlay", (req, res) => { const p = require('child_process').spawn('/home/pi/video-player/play-overlay.sh'); // p.stdout.on('data', (data) => { // console.log(`stdout: ${data}`); // }); // p.stderr.on('data', (data) => { // console.error(`stderr: ${data}`); // }); res.json({ status: 'ok' }); }); app.post("/startprojector", (req, res) => { require('child_process').spawn('sudo', ['/home/pi/video-player/start-projector.sh']); res.json({ status: 'ok' }); }); app.post("/stopprojector", (req, res) => { require('child_process').spawn('sudo', ['/home/pi/video-player/stop-projector.sh']); res.json({ status: 'ok' }); }); app.get("/projectorstatus", async (req, res) => { const res1 = await fetch('http://192.168.1.93/api/v01/control/escvp21?cmd=PWR?'); const status1 = await res1.text(); const res2 = await fetch('http://192.168.1.67/api/v01/control/escvp21?cmd=PWR?'); const status2 = await res2.text(); res.json({ status1, status2 }); }); app.post('/upload', function (req, res) { if (!req.files || Object.keys(req.files).length === 0) { return res.status(400).send('No files were uploaded.'); } // The name of the input field (i.e. "sampleFile") is used to retrieve the uploaded file // const uploadPath = path.join(__dirname, '/public/videos/'); const uploadPath = "/video"; console.log(req.files.file); req.files.file.mv(path.join(uploadPath, req.files.file.name), function (err) { if (err) { return res.status(500); } }); res.redirect('/'); });