This workflow automates the video edition process by leveraging Amazon Rekognition for detecting segments and Amazon MediaConvert for removing segments.
Learn more about this workflow at Step Functions workflows collection: << Add the live URL here >>
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
- Create an AWS account if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
- Git Installed
- AWS Serverless Application Model (AWS SAM) installed
-
Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/step-functions-workflows-collection
-
Change directory to the pattern directory:
cd step-functions-workflows-collection/segment-detection-workflow
-
From the command line, use AWS SAM to build and deploy the AWS resources for the workflow as specified in the template.yaml file:
sam build sam deploy --guided --capabilities CAPABILITY_NAMED_IAM
-
During the prompts:
- Enter a stack name
- Enter the desired AWS Region (some services may only be available in us-east-1)
- Allow SAM CLI to create IAM roles with the required permissions.
Once you have run
sam deploy --guided
mode once and saved arguments to a configuration file (samconfig.toml), you can usesam deploy
in future to use these defaults. -
Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
This workflow automates the video edition process by leveraging Amazon Rekognition for detecting segments, and Amazon MediaConvert for removing segments. As input you will use a video file with SMPTE color bars at the beginning, and as output you will get a transcoded clean video file without SMPTE color bars. For the input you will use a video created for the demo. Once you deploy via SAM, note the S3 bucket where you have to upload the video. The output S3 bucket should be the same.
Workflow starts by calling Amazon Rekognition for Segment Detection. The time taken for the Segment Detection Job will vary depending on the length of the video, so the workflow implements loop with Wait and Choice states. Between the Wait and Choice states, and every 60 seconds, the workflow calls the GetSegmentDetection API of Amazon Rekognition to confirm the status of the job. If the job didn't finish, the workflow stays in the loop. If the job finished, the workflow passes the segments to MediaConvert for processing.
Finally, MediaCovert received the segments as input (one of them is the SMPTE color bars at the beginning of the video), and the S3 video, to then triggering the transcoding job. During the transcoding job, MediaConvert removes the SMPTE color bars. MediaConvert CreateJob is a synchronous call following the Run a Job (.sync) pattern in AWS Step Functions, which means you don't need to implement any loop to get the job status (as we did with Amazon Rekognition); Step Functions handles that with MediaConvert for you.
Note: The loop implementation for Amazon Rekognition Segment Detection Job, can be implemented differently with custom code and Lambda functions, but for simplification we use simple Wait and Choice states to maintain all low-code. Additionally, in a production environment, you should also handle failures from the Segment Detection Job.
Once your have deployed the state machine, you should trigger it and see it in action. For that:
-
Go to the Amazon S3 console in you AWS Account.
-
Identify the S3 bucket created via SAM (look at the SAM output), and upload the video stored in the folder ./resources of your cloned repo.
-
Go to the AWS Step Functions console in you AWS Account.
-
Identify the State Machine. You should see a state machine with a name that matches to the ARN in the output of the sam deploy command.
-
Click on the state machine name and then click on Start Execution in the top-right of your screen.
-
In the following window, introduce the input of the state machine. Substitute the bucket name (don't use s3://). It should look like the below json.
{ "Input": { "Bucket": "{S3 BUCKET NAME CREATED VIA SAM}", "Key": "source_video.mp4" }, "Output": { "Bucket": "{S3 BUCKET NAME CREATED VIA SAM}", "Key": "output/" } }
-
Click on Start Execution
-
It will take few minutes, but once the workflow finished, you should see an image like the below.
- At this point you can go to the output S3 bucket to watch the video edited, without the SMPTE color bars at the beginning.
- Empty the S3 bucket created via SAM
- Delete the stack
sam delete
Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0