Skip to content

Instantly share code, notes, and snippets.

@gasp
Created March 31, 2017 13:05
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 gasp/ee5a5294c891fb4e424da678a4a1b7f7 to your computer and use it in GitHub Desktop.
Save gasp/ee5a5294c891fb4e424da678a4a1b7f7 to your computer and use it in GitHub Desktop.
use ms video api to stabilize a video
// doc
// https://westus.dev.cognitive.microsoft.com/docs/services/565d6516778daf15800928d5/operations/565d6517778daf0978c45e35
const fs = require('fs');
const request = require('request'); // use npm i request
const create = (fileUrl) => {
return new Promise(function (resolve, reject) {
request({
method: 'post',
json: true,
url: 'https://westus.api.cognitive.microsoft.com/video/v1.0/stabilize',
headers: {
'Ocp-Apim-Subscription-Key': api_key,
},
body: {
url: fileUrl
}
}, (err, res, body) => {
console.log(err);
if (err) reject(err);
if (typeof res.headers['operation-location'] === 'string') {
const opeUrl = res.headers['Operation-Location'];
console.log(opeUrl);
resolve(opeUrl);
}
else {
console.log(res.headers);
console.log(body);
reject('no opeUrl in '+JSON.stringify(body));
}
});
});
};
const getStatus = (opeUrl) => {
console.log(`opening opeUrl ${opeUrl}`);
return new Promise(function (resolve, reject) {
const checkProgress = () => {
request({
method: 'get',
json: true,
url: opeUrl,
headers: {
'Ocp-Apim-Subscription-Key': api_key,
}
}, (err, res, body) => {
// console.log(err);
// console.log(res);
// console.log(body);
if (body.status === 'Running') {
wait(checkProgress);
}
if (body.status === 'Succeeded') {
console.log('success');
console.log(body.resourceLocation);
resolve(body.resourceLocation);
}
});
}
const wait = (cb) => {
return setTimeout(cb, 10000);
}
checkProgress();
});
};
const downloadResult = (resourceLocation) => {
console.log('downloading...');
request({url: resourceLocation, headers: {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': api_key,
}}).pipe(fs.createWriteStream('stabilized.mp4'));
};
// edit below
const api_key = 'xxx'; // your api key
create('http://tag.freelancis.net/2017/videos-stabilized/chest-orig.mp4') // your video <100 Mo
.then(getStatus)
.then(downloadResult)
.catch((err) => {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment