Converting a video to a GIF in Python
In this scenario, you'll create a video converter using FFmpeg and Yandex Message Queue.
To create an application:
- Prepare your cloud
- Create resources
- Create an API function
- Create a converter function
- Create a trigger
- Test the application
Prepare your cloud
-
Go to the management console. Then log in to Yandex Cloud or sign up if you don't have an account yet.
-
On the billing page, make sure you have a billing account activated and its status is
ACTIVE
orTRIAL_ACTIVE
. If you don't have a billing account, create one. -
On the cloud page, create or select a folder for a new service.
Required paid resources
The infrastructure support cost for this scenario includes:
- A fee for function calls (see Yandex Cloud Functions pricing).
- A fee for querying the database (see Yandex Managed Service for YDB pricing).
- A fee for storing data in a bucket (see Yandex Object Storage pricing).
Create resources
-
Download the archive with the files needed to create a video converter.
-
Create a service account named
ffmpeg-sa
and assign it the following roles:ymq.reader
ymq.writer
lockbox.payloadViewer
storage.viewer
storage.uploader
ydb.admin
serverless.functions.invoker
-
Create a static key for the service account. Save the Key ID and Your secret key.
-
Create a secret named
ffmpeg-sa-secret
in Yandex Lockbox. Under Version, specify:ACCESS_KEY_ID
as the key and Key ID from the previous step as the value.SECRET_ACCESS_KEY
as the key and Your secret key from the previous step as the value.
Save the secret ID from the Information about secret section.
-
Create a message queue named
converter-queue
in Yandex Message Queue. Save the queue URL from the General information section. -
Create a YDB database in Serverless mode. Save the Endpoint from the Document API endpoint section.
-
Create a table in the database:
- Table name:
tasks
. - Table type: Document table.
- Columns: One column with the name
task_id
and theString
type. Set the Partition key attribute.
- Table name:
-
Create a bucket named
converter-bucket
in Yandex Object Storage.
Create an API function
The function implements an API which you can use to perform the following actions:
convert
: transfer a video to convert. The function writes the task to thetasks
table using the Document API.get_task_status
: get the task status. The function checks whether the task is completed and returns a link to a GIF file.
-
Create a function named
ffmpeg-api
. -
Create a function version:
-
Create a file named
requirements.txt
and specify the following library in it:boto3
-
Create a file named
index.py
and paste the contents offfmpeg-api.py
from the archive into it. -
Indicate the following:
- Runtime environment:
python37
. - Entry point:
index.handle_api
. - Timeout: 5 seconds.
- Service account:
ffmpeg-sa
.
- Runtime environment:
-
Add environment variables:
DOCAPI_ENDPOINT
: The endpoint from the database configuration.SECRET_ID
: The Yandex Lockbox secret ID.YMQ_QUEUE_URL
: The Message Queue queue URL.
-
Create a converter function
A converter function is run by a trigger. It performs video processing and registers the execution result in the tasks
table.
Video conversion is done using the FFmpeg utility. The FFmpeg executable file is more than 70 MB in size. To upload it along with the function code, create a ZIP archive and upload it via Object Storage. Learn more about code upload formats.
-
Create a function named
ffmpeg-converter
. -
Create an
src.zip
archive with the following files:- The
requirements.txt
file:boto3 requests
- The
index.py
file with the contents offfmpeg-converter.py
from the archive. - The FFmpeg executable file. To get it, go to the FFmpeg official website, the Linux Static Builds section, and download the archive with the 64-bit FFmpeg version.
- The
-
Upload
src.zip
toconverter-bucket
. -
Create a function version:
-
Indicate the following:
- Upload method:
Object Storage
. - Bucket:
converter-bucket
. - Object:
src.zip
. - Runtime environment:
python37
. - Entry point:
index.handle_process_event
. - Timeout: 600 seconds.
- Amount of RAM: 2048 MB.
- Service account:
ffmpeg-sa
.
- Upload method:
-
Add environment variables:
DOCAPI_ENDPOINT
: The endpoint from the database configuration.SECRET_ID
: The Yandex Lockbox secret ID.YMQ_QUEUE_URL
: The Message Queue queue URL.S3_BUCKET
:converter-bucket
.
-
Create a trigger
A message queue is handled using a trigger for Message Queue. It invokes the converter function when messages arrive in converter-queue
.
- In the management console, go to the folder where you want to create a trigger.
- Open Cloud Functions.
- Go to the Triggers tab.
- Click Create trigger.
- Under Basic parameters:
- Name the trigger
ffmpeg-trigger
. - In the Type field, select Message Queue.
- Name the trigger
- Under Message Queue settings, select
converter-queue
andffmpeg-sa
with rights to read messages from it. - Under Function settings:
- Select the function to be invoked by the trigger:
ffmpeg-converter
. - Specify the function version tag:
$latest
. - Specify the service account to be used to invoke the function:
ffmpeg-sa
.
- Select the function to be invoked by the trigger:
- Click Create trigger.
Test the application
Create a task
- In the management console, select the folder with the
ffmpeg-api
function. - Open Cloud Functions.
- Select
ffmpeg-api
. - Go to the Testing tab.
- In the Input field, enter:
{"action":"convert", "src_url":"<link to the video>"}
- Click Run test.
- You'll see the task ID in the Function output field:
{ "task_id": "c4269ceb-8d3a-40fe-95f0-84cf16e8c17f" }
View the queue statistics
After the task is created, the number of messages in the queue increases by one and a trigger fires. Make sure that messages arrive in the queue and are handled. To do this, view the queue statistics.
- In the management console, select the folder with
converter-queue
. - Open Message Queue.
- Select
converter-queue
. - Under General information, you can see the number of enqueued messages and those being handled.
- Go to Monitoring. View the Overall queue stats charts.
View the function logs
The trigger should invoke the converter function for each message in the queue. To make sure the function is invoked, check its logs.
- In the management console, select the folder with the
ffmpeg-converter
function. - Open Cloud Functions.
- Select
ffmpeg-converter
. - Go to the Logs tab and specify the period to view them for.
Get a link to a GIF file
-
In the management console, select the folder with the
ffmpeg-api
function. -
Open Cloud Functions.
-
Select
ffmpeg-api
. -
Go to the Testing tab.
-
In the Input field, enter the following request:
{"action":"get_task_status", "task_id":"<task id>"}
-
Click Run test.
-
If video conversion to GIF is not completed, the Function output field returns:
{ "ready": false }
Otherwise, you'll get a link to the GIF file:
{ "ready": true, "gif_url": "https://storage.yandexcloud.net/converter-bucket/1b4db1a6-f2b2-4b1c-b662-37f7a62e6e2e.gif?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=qxLftbbZ91U695ysemyZ%2F20210831%2Fru-central1%2Fs3%2Faws4_request&X-Amz-Date=20210831T110351Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=f4a5fe7848274a09be5b221fbf8a9f6f2b385708cfa351861a4e69df4ee4183c" }