Reputation: 21
.ogv
files to .mp4
using an ffmpeg
layer on AWS Lambda.I followed a tutorial from the people at Serverless Framework to convert .mp4
's to GIF
's and that worked out great. Using the same ffmpeg static build, ( ffmpeg-git-amd64-static.tar.xz ) I set out to convert .ogv
files to .mp4
files.
So far I have had success with uploading videos to an S3 Bucket, getting a Lambda to retrieve that video, do something to the video using the ffmpeg
binary, and copy a new file to S3.
The videos that are created, will not play.
data point 1: the resultant files from the function are far too small.
The input video file is 1.3MB and the output video is only 256.0KB
data point 2: moov atom not found.
After copying the resultant video from S3 to my local machine, I try to play using ffplay
and I receive this error:
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x7fd613093400] moov atom not found
frank.mp4: Invalid data found when processing input
As far as I have been able to tell, the moov atom is suppose to contain important metadata about .mp4
files.
I used the Serverless framework to set up the AWS infrastructure.
Here are a few different ffmpeg
commands I have tried:
1st attempt:
// convert to mp4!
spawnSync(
"/opt/ffmpeg/ffmpeg",
[
"-i",
`/tmp/${record.s3.object.key}`,
"-vcodec",
"libx264",
"-acodec",
"aac",
`/tmp/${record.s3.object.key}.mp4`
],
{ stdio: "inherit" }
);
2nd attempt:
// convert to mp4!
spawnSync(
"/opt/ffmpeg/ffmpeg",
[
"-i",
`/tmp/${record.s3.object.key}`,
`/tmp/${record.s3.object.key}.mp4`
],
{ stdio: "inherit" }
);
3rd attempt:
I found this approach in a Stack Overflow question and the poster said that it worked for him.
// convert to mp4!
spawnSync(
"/opt/ffmpeg/ffmpeg",
[
'-i',
`/tmp/${record.s3.object.key}`,
'-codec:v',
'libx264',
'-profile:v',
'main',
'-preset',
'slow',
'-b:v',
'400k',
'-maxrate',
'400k',
'-bufsize',
'800k',
'-threads',
'0',
'-b:a',
'128k',
`/tmp/${record.s3.object.key}.mp4`
],
{ stdio: "inherit" }
);
Each one of these works swell on my local machine.
If the ffmpeg binary that I am using was not a popular one, ( I have seen it on multiple sites dealing in connection with transcoding on Lambda ), my guess would be that it is an issue with the layer... Perhaps.
Any insight would be greatly appreciated. Thank you.
Upvotes: 1
Views: 3151
Reputation: 75
For small video files, the lambda timeout for 15 minutes looks like a good solution.
I tried compressing a 93 MB file, and lambda function did a timeout over 15 minutes.
The solution for it was to increase the allocated memory size to the lambda function from 128MB to 2048MB.
Upvotes: 0
Reputation: 21
This was an easy, and in my case overlooked, fix involving my Lambda configuration.
The Bug
By default Lambdas have a 6 second timeout, after which they stop processing.
It appears that my transcoding function ran too long and the Lambda was being terminated before the file could finish being transcoded.
The Fix
I set the timeout for my Lambda function to the 15-minute maximum. After doing this the process worked as expected and the video was transcoded and able to play.
Upvotes: 1