ffmpeg-aws-lambda-layer
ffmpeg-aws-lambda-layer copied to clipboard
Output video is significantly sped up
For the project I'm working on, I'm mainly just taking mp4s recorded by users in a web browser and trying to transcode them to be manageable sizes as well as playable in all device/browser combos. I was using AWS MediaConvert for this but later found out that mobile Safari records video in a "fragmented" mp4 which is not supported by MediaConvert so I'm turning to replacing MC with an ffmpeg layer.
I've deployed the ffmpeg lambda layer from the SAR and copied most of the code from the example but for some reason all the output videos are significantly sped up. When I run the same commands with my local ffmpeg, everything works fine. Any help is appreciated!
export const onVideoUpload: Handler = Sentry.AWSLambda.wrapHandler(async (event: S3Event, context: Context) => {
const EXTENSION = '.mp4',
MIME_TYPE = 'video/mp4';
const eventRecord = event.Records && event.Records[0],
inputBucket = eventRecord.s3.bucket.name,
key = eventRecord.s3.object.key,
id = context.awsRequestId,
resultKey = key.replace(/\.[^.]+$/, EXTENSION),
workdir = os.tmpdir(),
inputFile = path.join(workdir, id + path.extname(key)),
outputFile = path.join(workdir, 'out_' + id + EXTENSION);
console.log('converting', inputBucket, key, 'using', inputFile);
// ffmpeg -i 2254.webm -c copy output.webm
// ffmpeg -i input.mp4 -b:v 1M -b:a 192k output.avi
return downloadFileFromS3(inputBucket, key, inputFile)
.then(() => childProcess(
'/opt/bin/ffmpeg',
['-i', inputFile, '-b:v', '1M', '-b:a', '192k', '-nostdin', outputFile],
{env: process.env, cwd: workdir}
))
.then(() => uploadFileToS3(process.env['DestinationBucket']!, resultKey, outputFile, MIME_TYPE));
});
maybe try without the explicit bitrate (-b:v)?
Updated the function to
return downloadFileFromS3(inputBucket, key, inputFile)
.then(() => childProcess(
'/opt/bin/ffmpeg',
['-i', inputFile, '-nostdin', outputFile],
{env: process.env, cwd: workdir}
))
.then(() => uploadFileToS3(process.env['DestinationBucket']!, resultKey, outputFile, MIME_TYPE));
and I still have the same result: https://user-images.githubusercontent.com/3442109/129050132-b7da97c1-217a-4631-9919-68606fd9c6d5.mp4
Original video: https://user-images.githubusercontent.com/3442109/129050443-78463553-4fda-43a7-81ce-6bf04aa36e1d.mp4