I have uploaded a file using my google app engine backend to my storage bucket but now I cannot access the file to pass in into ffmpeg. I am getting this error message from the try-catch: "The input file does not exist". I can see that the file was uploaded because I checked my developer console under the storage bucket. I am using the boilerplate code provided by google but added the ffmpeg for testing. I am trying to access the path to the uploaded file using, but it is incorrect, though I am getting the bucket.name value and the blob.name value. I am using the "flex" environment for this.
const originalFilePath = `gs://${bucket.name}/${blob.name}`;
here is the full code:
const process = require('process'); // Required to mock environment variables
const express = require('express');
const helpers = require('./helpers/index');
const Multer = require('multer');
const bodyParser = require('body-parser');
const ffmpeg = require("ffmpeg"); //https://www.npmjs.com/package/ffmpeg
const {Storage} = require('@google-cloud/storage');
// Instantiate a storage client
const storage = new Storage();
const app = express();
app.set('view engine', 'pug');
app.use(bodyParser.json());
// Multer is required to process file uploads and make them available via
// req.files.
const multer = Multer({
storage: Multer.memoryStorage(),
limits: {
fileSize: 5 * 1024 * 1024, // no larger than 5mb, you can change as needed.
},
});
// A bucket is a container for objects (files).
const bucket = storage.bucket(process.env.GCLOUD_STORAGE_BUCKET);
// Display a form for uploading files.
app.get('/', (req, res) => {
res.render('form.pug');
});
// Process the file upload and upload to Google Cloud Storage.
app.post('/upload', multer.single('file'), (req, res, next) => {
if (!req.file) {
res.status(400).send('No file uploaded.');
return;
}
// Create a new blob in the bucket and upload the file data.
const blob = bucket.file(req.file.originalname);
const blobStream = blob.createWriteStream({
resumable: false,
});
blobStream.on('error', err => {
next(err);
});
blobStream.on('finish', () => {
const audioFile = helpers.replaceAllExceptNumbersAndLetters(new Date());
// this path is incorrect but I cannot find correct way to do it
const originalFilePath = `gs://${bucket.name}/${blob.name}`;
const filePathOutput = `gs://${bucket.name}/${audioFile}.mp3`;
try {
const process = new ffmpeg(originalFilePath);
process.then(function (video) {
// Callback mode
video.fnExtractSoundToMP3(filePathOutput, (error, file) => {
if (!error)
res.send(`audio file: ${file}`);
});
}, (error) => {
res.send(`process error: ${error}`);
});
} catch (e) {
res.send(`try catch error: ${JSON.stringify(e)} | bucket: ${JSON.stringify(bucket)} |
blob: : ${JSON.stringify(blob)}`);
}
});
blobStream.end(req.file.buffer);
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
module.exports = app;