0

I have a node.js script that uses ffmpeg to convert mp4 downloaded from YT to mp3 and save to Amazon S3. Uploading using the serverless framework. The "ffmpeg" file is included in the main directory (with .yml), downloaded from here: https://johnvansickle.com/ffmpeg/

The code:

'use strict' process.env.PATH = process.env.PATH + ':/tmp/' process.env['FFMPEG_PATH'] = '/tmp/ffmpeg'; const BIN_PATH = process.env['LAMBDA_TASK_ROOT'] process.env['PATH'] = process.env['PATH'] + ':' + BIN_PATH; module.exports.download_mp3 = function (event, context, callback) { require('child_process').exec('cp /var/task/ffmpeg /tmp/.; chmod 755 /tmp/ffmpeg;', function (error, stdout, stderr) { if (error) { console.log('An error occured', error); callback(null, null) } else { var ffmpeg = require('ffmpeg'); const aws = require('aws-sdk') const s3 = new aws.S3() const ytdl = require('ytdl-core'); function uploadFromStream(s3) { const stream = require('stream') var pass = new stream.PassThrough(); var params = {Bucket: "some-bucket", Key: "some-key", Body: pass}; s3.upload(params, function(err, data) { console.log(err, data); }); console.log("Should be finished") callback(null) } let stream = ytdl("some-video-id", { quality: 'highestaudio', filter: 'audioonly' }); ffmpeg(stream) .audioBitrate(128) .format('mp3') .on('error', (err) => console.error(err)) .pipe(uploadFromStream(s3), { end: true }); }}) } 

When triggered, the function writes an error in logs:

2019-01-04T14:50:54.525Z 21da4d49-1030-11e9-b901-0dc32b691a16 /var/task/ffmpeg:1 (function (exports, require, module, __filename, __dirname) { ELF ^ SyntaxError: Invalid or unexpected token at createScript (vm.js:80:10) at Object.runInThisContext (vm.js:139:10) at Module._compile (module.js:616:28) at Object.Module._extensions..js (module.js:663:10) at Module.load (module.js:565:32) at tryModuleLoad (module.js:505:12) at Function.Module._load (module.js:497:3) at Module.require (module.js:596:17) at require (internal/module.js:11:18) at /var/task/download.js:17:18 

It's, most definetely, an error in the "ffmpeg" file I've mentioned above (link provided). But I don't know what's the exact issue, I followed the first answer here: https://stackoverflow.com/questions/47882810/lambda-not-connecting-to-ffmpeg to include the ffmpeg build.

1
  • that's hard to debug without having access to your environment. You could have messed up at various places. What does a console.dir(require.resolve( 'ffmpeg')) right before the require('ffmpeg') show for you? Commented Jan 4, 2019 at 15:25

1 Answer 1

2

It is not an error in the ffmpeg binary you downloaded. The ELF- which is the unexpected token - means, that one of your require statement loads a binary instead of a JavaScript file or module (what is ELF - something line feed?)

The origin of the error - as the stack trace says - at /download.js:17:18 which is var ffmpeg = require('ffmpeg'), so the problematic require statement is your require('ffmpeg')

The reason why require loads the ffmpeg instead of the ffmpeg modules, is that the binary on the one hand lies in one of the places where require looks for the modules, and is found before the ffmpeg modules.

Sign up to request clarification or add additional context in comments.

2 Comments

The problem was, that I've copied some code without really looking into it. The script was importing "ffmpeg", while it shoud've imported "fluent-ffmpeg", instead.
@almarc But the original problem still persists, node either searches for modules at a place where it should not, or you have a copy of the ffpmeg binary at a place where it should not be.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.