Trim video file on an S3 compatible bucket using open source

In this blog post I will describe how to trim a video file on an S3 compatible bucket using ffmpeg without having to download it first, process it and then upload the result.

For trimming the video we will use the open source tool ffmpeg and a script that handles uploading the result to an S3 bucket. This open source script is available as a service in Open Source Cloud.

Step 1: Login to Eyevinn Open Source Cloud

Go to www.osaas.io and login. Sign up for an account if you don’t already have one. It is free to get started and you don’t even have to enter a credit card to try this out.

Step 2: Setup access to S3 bucket

Go to the service in Open Source Cloud called “FFmpeg to S3” using the search bar on the browse page. Click on the card “FFmpeg to S3 to go to the service.

Then click on the tab named “Service secrets”

Get the S3 access key credentials from your administrator of your S3 buckets. You need at minimum the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Create a service secret for each of these credentials.

Step 3: Generate signed URL to the video to trim

Now we need to generate a signed URL for the video that you want to trim.

Copy the presigned URL to the clipboard

Step 4: Create a ffmpeg trim job

As an example we will extract the first 30 seconds of the video file and the ffmpeg command for that is:

ffmpeg -ss 0 -t 30 -c:v copy -c:a copy

Go back to the FFmpeg to S3 service page in Open Source Cloud and click on button “Create job”.

Enter the following in the settings dialog:

Name: “tutorial”
CmdLineArgs:
Replace [SIGNED_URL] from clipboard and lab-testcontent-input with the name of your bucket:

-i [SIGNED-URL] -d s3://lab-testcontent-input/tutorial-30sec.mp4 "-ss 0 -t 30 -c:v copy -c:a copy"

AwsAccessKeyId and AwsSecretAccessKey: reference to the service secrets created
Region: Location of the S3 bucket

Now press Create and wait for the job to be completed. When the job is completed you should have a file called tutorial-30sec.mp4 on the bucket you provided and 30 seconds duration.

Create a job from command line

You might want to automate or script the creation of these ffmpeg jobs and to facilitate that there is an open source SDK and command line tool for Eyevinn OSC. The command line tool is a Node.js script.

Follow the instructions on how to install Node.js if you don’t already have it installed.

Then install the CLI


% npm install -g @osaas/cli

Obtain the personal access token by going to Settings in OSC and the tab API. Here you find the personal access token that you copy to your clipboard. Set this token as an environment variable in your shell.


% export OSC_ACCESS_TOKEN=token

Now you can create the same job with the following command (replace [SIGNED-URL] and s3 bucket):


% osc create eyevinn-ffmpeg-s3 tutorialcli -o awsAccessKeyId="{{secrets.eyevinnawskeyid}}" -o awsSecretAccessKey="{{secrets.eyevinnawssecret}}" -o cmdLineArgs='-i [SIGNED-URL] -d s3://lab-testcontent-input/tutorial-30sec.mp4 "-ss 0 -t 30 -c:v copy -c:a copy"'

Conclusion

This was an example of how you can run ffmpeg to process a video file on an S3 bucket and output the result back to an S3 bucket without having to develop your own script for it as a script already existed that is open source and made available as a service in Eyevinn Open Source Cloud.

Leave a Reply

Your email address will not be published. Required fields are marked *