In this tutorial, I will show you how to convert a Youtube video to a mp3 file using AWS Elastic Transcoder. How can we do that ?
We will create a Lambda function to consume events published by S3. For any video uploaded to a bucket, S3 will invoke our Lambda function by passing event information. AWS Lambda executes the function. As the function executes, it reads the S3 event data, logs some of the event information to Amazon CloudWatch. Then, kick off a transcoding job.
Let’s start, by creating an S3 bucket to store the inputs files (videos) and the outputs files (audio) :
Next, let’s define a Transcoder pipeline. A pipeline essentially defines a queue for future transcoding jobs. To create a pipeline, we need to specify the input bucket (where the videos will be).
Note: Copy down the Pipeline ID, we will need later on
Having created a pipeline, go to the AWS Management Console, navigate to Lambda service & click on “Create a Lambda Function“, add S3 as the event source for Lambda function:
I used the following Node.JS code:
The script does the following:
- Extract the filename of the uploaded file from the event object
- Create a Transcoder job and specify the required outputs
- Launch the job
Note: you might notice in the function above is the use of presets (1351620000001–300040). It describes how to encode the given file (in this case mp3). The full list of available presets can be found in AWS Documentation.
Finally, set the pipeline id as an envrionment variable and select an IAM role with permission to access Elastic Transcoder:
Once created, upload a video file to the inputs bucket:
aws s3 cp way_down_we_go.3gp s3://slowcoder-videos/inputs/
If everything went well, you should see the file in your outputs bucket:
S3 will trigger our Lambda function. It will then execute our function. and log the S3 object name to CloudWatch Logs:
After couple of seconds (or minutes depends on the size of the video ) , you should see a new MP3 file has been generated by Elastic Transcoder job inside the outputs directory in the S3 bucket: