0

I've written this cloud function which transcodes a MOV file to MP4 but after function runs I only see saved video 1.3kb or something but everything runs smoothly not sure what's happening. I don't see any errors generated in console but I'm sure I'm doing something wrong in code, below is the function:

const storage = require('@google-cloud/storage')();
const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path;
const ffmpeg = require('fluent-ffmpeg');

ffmpeg.setFfmpegPath(ffmpegPath);

exports.vsTranscodeVideo = (req, res) => {

const fileName = req.body.filename;
const userBucket = storage.bucket(req.body.vsuserbucket);
const uploadBucket = storage.bucket(req.body.vstempbucket);

const remoteWriteStream = userBucket.file(fileName.replace('.mov', '.mp4'))
.createWriteStream({
    metadata: {
        contentLanguage: 'en',
        contentType: 'video/mp4'
    }
  });

const remoteReadStream = uploadBucket.file(fileName).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')
  .outputFormat('mp4')
  //https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/346#issuecomment-67299526 // end: true, emit end event when readable stream ends
  .on('start', (cmdLine) => {
    console.log('Started ffmpeg with command:', cmdLine);
  })
  .on('end', () => {
    //uploadBucket.file(fileName).delete();
    console.log('Successfully re-encoded video.');
    res.status(200).send("success");
  })
  .on('error', (err, stdout, stderr) => {
    console.error('stdout:', stdout);
    console.error('stderr:', stderr);
    res.status(200).send(err.message);
  })
  .pipe(remoteWriteStream, { end: true });
};

Is there something I'm doing wrong? Any help much appreciated. Thanks

Rehan
  • 408
  • 1
  • 6
  • 17
  • hey @Rehan, to help you I need more information: - your Node version - your package.json file - how are you triggering this function - errors, logs. if there are. – Methkal Khalawi Nov 19 '19 at 15:19
  • 1
    @MethkalKhalawi Problem was resolved I was actually piping the stream directly to cloud storage instead the solution is pipe stream to GCF /tmp directory in a temporary file and the save that to cloud storage and all went smoothly. Hope I'm helping someone with this comment. and thanks for asking. – Rehan Nov 19 '19 at 19:00

0 Answers0