Created
March 31, 2017 13:05
-
-
Save gasp/ee5a5294c891fb4e424da678a4a1b7f7 to your computer and use it in GitHub Desktop.
use ms video api to stabilize a video
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
// 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