Created
September 17, 2018 14:10
-
-
Save cdgraff/c1ab87cecd2f7f52dac053fa2b1d82cc to your computer and use it in GitHub Desktop.
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
const {Storage} = require('@google-cloud/storage'); | |
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; | |
const ffmpeg = require('fluent-ffmpeg'); | |
var bucket = 'test-demo-transcode'; | |
ffmpeg.setFfmpegPath(ffmpegPath); | |
exports.transcodeVideo = (data, context) => { | |
const file = data; | |
console.log(` Bucket: ${file.bucket}`); | |
console.log(` File: ${file.name}`); | |
console.log(` Metageneration: ${file.metageneration}`); | |
console.log(` Created: ${file.timeCreated}`); | |
console.log(` Updated: ${file.updated}`); | |
const storage = new Storage(); | |
// Streams are also supported for reading and writing files. | |
var remoteWriteStream = storage.bucket(file.name) | |
.createWriteStream({ | |
metadata: { | |
metadata: file.metadata, // You may not need this, my uploads have associated metadata | |
contentType: 'video/mp4', // This could be whatever else you are transcoding to | |
}, | |
}); | |
// Open read stream to our uploaded file | |
var remoteReadStream = file.bucket(file.name).createReadStream(); | |
// Transcode | |
ffmpeg() | |
.input(remoteReadStream) | |
.outputOptions('-c:v copy') // Change these options to whatever suits your needs | |
.outputOptions('-c:a aac') | |
.outputOptions('-b:a 160k') | |
.outputOptions('-f mp4') | |
.outputOptions('-preset fast') | |
.outputOptions('-movflags frag_keyframe+empty_moov') | |
// https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/346#issuecomment-67299526 | |
.on('start', (cmdLine) => { | |
console.log('Started ffmpeg with command:', cmdLine); | |
}) | |
.on('end', () => { | |
console.log('Successfully re-encoded video.'); | |
callback(); | |
}) | |
.on('error', (err, stdout, stderr) => { | |
console.error('An error occured during encoding', err.message); | |
console.error('stdout:', stdout); | |
console.error('stderr:', stderr); | |
callback(err); | |
}) | |
.pipe(remoteWriteStream, { end: true }); // end: true, emit end event when readable stream ends | |
}; | |
function uploadFile(bucketName, filename) { | |
// [START storage_upload_file] | |
// Imports the Google Cloud client library | |
const {Storage} = require('@google-cloud/storage'); | |
// Creates a client | |
const storage = new Storage(); | |
/** | |
* TODO(developer): Uncomment the following lines before running the sample. | |
*/ | |
// const bucketName = 'Name of a bucket, e.g. my-bucket'; | |
// const filename = 'Local file to upload, e.g. ./local/path/to/file.txt'; | |
// Uploads a local file to the bucket | |
storage | |
.bucket(bucketName) | |
.upload(filename, { | |
// Support for HTTP requests made with `Accept-Encoding: gzip` | |
gzip: true, | |
metadata: { | |
// Enable long-lived HTTP caching headers | |
// Use only if the contents of the file will never change | |
// (If the contents will change, use cacheControl: 'no-cache') | |
cacheControl: 'public, max-age=31536000', | |
}, | |
}) | |
.then(() => { | |
console.log(`${filename} uploaded to ${bucketName}.`); | |
}) | |
.catch(err => { | |
console.error('ERROR:', err); | |
}); | |
// [END storage_upload_file] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment