Skip to content

Instantly share code, notes, and snippets.

@cmboros

cmboros/init.js Secret

Last active April 1, 2016 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmboros/68dca23e4f498b6baa1c157bf7226398 to your computer and use it in GitHub Desktop.
Save cmboros/68dca23e4f498b6baa1c157bf7226398 to your computer and use it in GitHub Desktop.
Capture Video
if (Meteor.isCordova) {
Meteor.startup(function () {
pictureSource = navigator.camera.PictureSourceType;
destinationType = navigator.camera.DestinationType;
mediaType = navigator.camera.MediaType;
});
}
var uploader = new Slingshot.Upload('myFileUploads');
// counter starts at 0
Session.setDefault('uploadProgress', 0);
Template.templateVideoCapture.helpers({
progress: function () {
return Math.round(uploader.progress() * 100);
}
});
Template.templateVideoCapture.events({
'click .cordovaVideoRecord': function(){
if (Meteor.isCordova) {
navigator.device.capture.captureVideo(videoCaptureSuccess, videoCaptureError, {limit:1});
}
},
'click .cordovaVideoUpload': function(){
if (Meteor.isCordova) {
getPhoto(pictureSource.PHOTOLIBRARY);
}
},
});
if (Meteor.isCordova) {
// video
function videoCaptureSuccess(mediaFiles) {
// Wrap this below in a ~100 ms timeout on Android if
// you just recorded the video using the capture plugin.
// For some reason it is not available immediately in the file system.
setTimeout(function(){
var file = mediaFiles[0].fullPath;
var videoFileName = Random.id(); // I suggest a uuid
VideoEditor.transcodeVideo(
videoTranscodeSuccess,
videoTranscodeError,
{
fileUri: file,
outputFileName: videoFileName,
quality: 100,
outputFileType: VideoEditorOptions.OutputFileType.MPEG4,
optimizeForNetworkUse: VideoEditorOptions.OptimizeForNetworkUse.YES,
saveToLibrary: false
}
);
VideoEditor.createThumbnail(
createThumbnailSuccess,
createThumbnailError,
{
fileUri: file,
outputFileName: videoFileName
}
);
}, 100);
}
function videoTranscodeSuccess(result) {
// result is the path to the transcoded video on the device
console.log('videoTranscodeSuccess, result: ' + result);
var file = '/local-filesystem' + result;
var name = 'Video Name';
var _id = Session.get('pageId');
getBlobURL(file, "video/mp4", function(url, blob){
$('.progress').addClass('active');
// Change to upload to video Library.
uploader.send(blob, function (error, downloadUrl) {
var fileUrl = downloadUrl;
// video thumb upload
var file = localStorage.getItem('videoThumbnail');
$('.progress').removeClass('active');
getBlobURL(file, "image/jpeg", function(url, blob){
$('.progress').addClass('active');
uploader.send(blob, function (error, downloadUrl) {
// insert to Database
});
});
});
});
}
function createThumbnailSuccess(result) {
// result is the path to the jpeg image on the device
console.log('createThumbnailSuccess, result: ' + result);
// var i = "<img src='/local-filesystem" + result + "'>";
// document.querySelector("#imgArea").innerHTML = i;
$('.preview.video video').attr('poster', '/local-filesystem' + result);
var file = '/local-filesystem' + result;
localStorage.setItem('videoThumbnail', file);
}
// error
var videoCaptureError = function(error) {
navigator.notification.alert('ERROR:' + error.message, null, "Capture Error");
}
function createThumbnailError(err) {
console.log('createThumbnailError, err: ' + err);
}
function videoTranscodeError(err) {
console.log('videoTranscodeError, err: ' + err);
}
// video gallery
function capturePhoto() {
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI });
}
function getPhoto(source) {
navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
destinationType: destinationType.FILE_URI,
mediaType: mediaType.VIDEO,
sourceType: source });
}
function onPhotoURISuccess(imageURI) {
setTimeout(function(){
var file = imageURI;
var videoFileName = Random.id(); // I suggest a uuid
VideoEditor.transcodeVideo(
videoTranscodeSuccess,
videoTranscodeError,
{
fileUri: file,
outputFileName: videoFileName,
quality: 100,
outputFileType: VideoEditorOptions.OutputFileType.MPEG4,
optimizeForNetworkUse: VideoEditorOptions.OptimizeForNetworkUse.YES,
saveToLibrary: false
}
);
VideoEditor.createThumbnail(
createThumbnailSuccess,
createThumbnailError,
{
fileUri: file,
outputFileName: videoFileName
}
);
}, 100);
}
function onFail(error) {
console.log(error);
}
// helper function
function getBlobURL(url, mime, callback) {
var xhr = new XMLHttpRequest();
xhr.open("get", url);
xhr.responseType = "arraybuffer";
xhr.addEventListener("load", function() {
var arrayBufferView = new Uint8Array( this.response );
var blob = new Blob( [ arrayBufferView ], { type: mime } );
var url = window.URL.createObjectURL(blob);
callback(url, blob);
});
xhr.send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment