Created
May 3, 2017 23:09
-
-
Save DKbyo/5ef45bc1fc5f27bddada2386842fab16 to your computer and use it in GitHub Desktop.
GoPro Hero 5 Node Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var GoPro = require('goproh4'); | |
| var express = require('express'); | |
| var bodyParser = require('body-parser') | |
| var WebSocket = require('ws'); | |
| var fs = require("fs"); | |
| var cam = new GoPro.Camera(); | |
| cam.mode(GoPro.Settings.Modes.Video, GoPro.Settings.Submodes.Video.Video) | |
| .then(function(){ | |
| return cam.set(GoPro.Settings.VIDEO_RESOLUTION, GoPro.Settings.VideoResolution.R720S)}) | |
| .then(function(){ | |
| return cam.set(GoPro.Settings.SETUP_SECONDARY_STREAM_WINDOW_SIZE,GoPro.Settings.SetupSecondaryStreamWindowSize.Default)}) | |
| .then(function(){ | |
| return cam.set(GoPro.Settings.SETUP_SECONDARY_STREAM_BIT_RATE,GoPro.Settings.SetupSecondaryStreamBitRate.S240000)}) | |
| .then(function(){ | |
| return cam.restartStream() }) | |
| .then(function () { | |
| console.log('[livestream]', 'started'); | |
| var STREAM_PORT = 8082; | |
| var WEBSOCKET_PORT = 8084; | |
| var STREAM_MAGIC_BYTES = 'jsmp'; | |
| var width = 320; | |
| var height = 240; | |
| var socketServer = new WebSocket.Server({ port: WEBSOCKET_PORT }); | |
| socketServer.on('connection', function(socket) { | |
| socket.on('close', function(code, message){ | |
| console.log( 'Disconnected WebSocket ('+socketServer.clients.length+' total)' ); | |
| }); | |
| }); | |
| socketServer.broadcast = function(data, opts) { | |
| socketServer.clients.forEach(function(client) { | |
| if (client.readyState === WebSocket.OPEN) { | |
| client.send(data, opts); | |
| } | |
| else { | |
| } | |
| }); | |
| }; | |
| var app = express(); | |
| app.use(function(req, res, next) { | |
| res.header("Access-Control-Allow-Origin", "*"); | |
| res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
| next(); | |
| }); | |
| app.use(bodyParser.json({limit: '10mb'})) | |
| app.post('/publish', function (req, res) { | |
| console.log( | |
| 'Stream Connected: ' + req.socket.remoteAddress + | |
| ':' + req.socket.remotePort + ' size: ' + width + 'x' + height | |
| ); | |
| req.socket.setTimeout(0); | |
| req.on('data', function(data){ | |
| socketServer.broadcast(data, {binary:true}); | |
| }); | |
| }); | |
| app.get("/capture",function(req,res) { | |
| cam.start() | |
| .delay(10000) | |
| .then(function () { | |
| return cam.stop() | |
| }); | |
| res.sendStatus(200); | |
| }); | |
| app.get("/last",function(req,res){ | |
| cam.listMedia().then(function (result) { | |
| var lastDirectory = result.media[result.media.length - 1]; | |
| var lastFile = lastDirectory.fs[lastDirectory.fs.length - 1]; | |
| var file ='http://' + cam._ip + '/videos/DCIM/'+lastDirectory.d+"/"+lastFile.n; | |
| res.send({file:file}); | |
| }); | |
| }); | |
| app.post("/save",function(req,res){ | |
| fs.appendFile('/tmp/videos.txt', '\n\r'+req.body.code+","+req.body.name+","+req.body.phone+","+req.body.email+","+req.body.video+","+req.body.preview); | |
| res.sendStatus(200); | |
| }); | |
| app.use('/booth', express.static(__dirname + '/client')); | |
| console.log(__dirname + '/client'); | |
| app.listen(STREAM_PORT); | |
| var spawn_process = function () { | |
| var ffmpeg = require('child_process').spawn("ffmpeg", [ | |
| "-i", | |
| "udp://" + cam._ip + ":8554", | |
| "-f", | |
| "mpegts", | |
| "-c:v", | |
| "mpeg1video", | |
| "http://127.0.0.1:8082/publish" | |
| ]); | |
| ffmpeg.stdout.pipe(process.stdout); | |
| ffmpeg.stderr.pipe(process.stdout); | |
| ffmpeg.on('exit', function () { | |
| spawn_process(); | |
| }); | |
| }; | |
| spawn_process(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment