Created
October 22, 2013 11:28
-
-
Save psi-4ward/7099001 to your computer and use it in GitHub Desktop.
NodeJS MongoDB-GridFS Video range stream example
Lets your browser seek/jump wihin the video-playback.
This file contains 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 app = require('express')(); | |
var GridStore = require('mongodb').GridStore; | |
var ObjectID = require('mongodb').ObjectID; | |
var MongoClient = require('mongodb').MongoClient; | |
var Server = require('mongodb').Server; | |
var dbConnection; | |
MongoClient.connect("mongodb://localhost:27017/ersatz?auto_reconnect", {journal: true}, function(err, db) { | |
dbConnection = db; | |
app.listen(3000); | |
}); | |
function StreamGridFile(req, res, GridFile) { | |
if(req.headers['range']) { | |
// Range request, partialle stream the file | |
console.log('Range Reuqest'); | |
var parts = req.headers['range'].replace(/bytes=/, "").split("-"); | |
var partialstart = parts[0]; | |
var partialend = parts[1]; | |
var start = parseInt(partialstart, 10); | |
var end = partialend ? parseInt(partialend, 10) : GridFile.length -1; | |
var chunksize = (end-start)+1; | |
console.log('Range ',start,'-',end); | |
res.writeHead(206, { | |
'Content-Range': 'bytes ' + start + '-' + end + '/' + GridFile.length, | |
'Accept-Ranges': 'bytes', | |
'Content-Length': chunksize, | |
'Content-Type': GridFile.contentType | |
}); | |
// Set filepointer | |
GridFile.seek(start, function() { | |
// get GridFile stream | |
var stream = GridFile.stream(true); | |
// write to response | |
stream.on('data', function(buff) { | |
// count data to abort streaming if range-end is reached | |
// perhaps theres a better way? | |
start += buff.length; | |
if(start >= end) { | |
// enough data send, abort | |
GridFile.close(); | |
res.end(); | |
} else { | |
res.write(buff); | |
} | |
}); | |
}); | |
} else { | |
// stream back whole file | |
console.log('No Range Request'); | |
res.header('Content-Type', GridFile.contentType); | |
res.header('Content-Length', GridFile.length); | |
var stream = GridFile.stream(true); | |
stream.pipe(res); | |
} | |
} | |
app.get('/', function(req, res) { | |
console.log('GET request'); | |
new GridStore(dbConnection, new ObjectID("526652540d9e077978000230"), null, 'r').open(function(err, GridFile) { | |
if(!GridFile) { | |
res.send(404,'Not Found'); | |
return; | |
} | |
StreamGridFile(req, res, GridFile) | |
}); | |
}); | |
Streaming videos directly to/from GridFS using gridfs-stream either with mongodb-native db instance or mongoose.
Clone node-cheat direct_upload_gridfs, run node app
followed by npm install express mongodb gridfs-stream
.
Specific not work "start" FILE FOR VIDEO
I have 70 gigabytes of video and I'll extract a fragment of a size of 10 megabytes
Example:
- fs.createReadStream('sample.txt', {start: 50 000 000, end: 60 000 000})
2.res.writeHead(206, {
'Content-disposition': 'filename=streamxx.mp4',
'Accept-Ranges': 'bytes',
'Content-Type': "video/mp4",
'Content-Range': 'bytes ' + start + '-' + end + '/' + GridFile.length,//GridFile.length, // start = 50 000 000
'Content-Length': chunksize
}); - GridFile.seek(start, function () { // start = 50 000 000
or Other
These examples can extract part of the video file, but can not play in this part of the HTML5 Video tag . I tried FFMPEG and its superstructure but still can not play. As this fragmetn play or what or how to modify it ??
This is a grate help for me on my projects. Thanks you.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to steam only a part of the file do
fs.createReadStream('sample.txt', {start: 90, end: 99})
;ah, but i have no idea if GridStream has support for that...