Converting a video to a GIF in Python
You will create a video converter using FFmpeg
To create an application:
- Get your cloud ready.
- Prepare the resources.
- Create an API function.
- Create a converter function.
- Create a trigger.
- Test the application.
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Go to the management console
and log in to Yandex Cloud or create an account if you do not have one yet. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVE
orTRIAL_ACTIVE
status. If you do not have a billing account, create one.
If you have an active billing account, you can go to the cloud page
Learn more about clouds and folders.
Required paid resources
The infrastructure support cost includes:
- Fee for invoking functions (see Yandex Cloud Functions pricing).
- Fee for querying the database (see Yandex Managed Service for YDB pricing).
- Fee for storing data in a bucket (see Yandex Object Storage pricing).
Prepare the resources
-
Download
the archive with the files required to create a video converter. -
Create a service account named
ffmpeg-sa
and assign the following roles to it:ymq.reader
ymq.writer
lockbox.payloadViewer
storage.viewer
storage.uploader
ydb.admin
functions.functionInvoker
-
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 with Key ID from the previous step for value.SECRET_ACCESS_KEY
as the key with Your secret key from the previous step for 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:
- Name:
tasks
. - Table type: Document table.
- Columns: One column titled
task_id
withString
for type. Set the Partition key attribute.
- Name:
-
Create a bucket with restricted access in Yandex Object Storage.
Create an API function
The function implements an API which you can use to perform the following actions:
convert
: Provide a video for conversion. 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 library in it:boto3
-
Create a file named
index.py
and paste into it the contents of theffmpeg-api.py
file from the archive. -
Specify the following parameters:
- Runtime environment:
python37
- Entry point:
index.handle_api
- Timeout, sec:
5
- Service account:
ffmpeg-sa
- Runtime environment:
-
Add these environment variables:
DOCAPI_ENDPOINT
: Endpoint from the database configuration.SECRET_ID
: Yandex Lockbox secret ID.YMQ_QUEUE_URL
: 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 larger than 70 MB. 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 archive named
src.zip
with the following files:-
requirements.txt
file:boto3 requests
-
The
index.py
file with the contents offfmpeg-converter.py
from the archive. -
The FFmpeg executable file. On the FFmpeg official website
, download the 64-bit FFmpeg archive from the Linux Static Builds section and make the file executable by running thechmod +x ffmpeg
command.
-
-
Upload the
src.zip
archive to the bucket you created earlier. -
Create a function version:
-
Specify the following parameters:
- Runtime environment:
python37
- Method:
Object Storage
upload method - Bucket: Name of the bucket you created earlier.
- Object:
src.zip
- Entry point:
index.handle_process_event
- Timeout, sec:
600
- Memory:
2048 MB
- Service account:
ffmpeg-sa
- Runtime environment:
-
Add these environment variables:
DOCAPI_ENDPOINT
: Endpoint from the database configuration.SECRET_ID
: Yandex Lockbox secret ID.YMQ_QUEUE_URL
: Message Queue queue URL.S3_BUCKET
: Name of the bucket you created earlier.
-
Create a trigger
The message queue is processed using a trigger for Message Queue. It invokes the converter function when messages get into converter-queue
.
- In the management console
, select the folder where you want to create a trigger. - Select Cloud Functions.
- Go to the Triggers tab.
- Click Create trigger.
- Under Basic settings:
- Enter a name for the trigger:
ffmpeg-trigger
. - In the Type field, select
Message Queue
.
- Enter a name for the trigger:
- Under Message Queue message settings, select the
converter-queue
message queue and theffmpeg-sa
service account with permissions to read from it. - Under Function settings:
- Select the function for the trigger to invoke:
ffmpeg-converter
. - Specify the function version tag:
$latest
. - Specify the service account that will be used to invoke the function:
ffmpeg-sa
.
- Select the function for the trigger to invoke:
- Click Create trigger.
Test the application
Create a task
-
In the management console
, select the folder containing theffmpeg-api
function. -
Select Cloud Functions.
-
Select the
ffmpeg-api
function. -
Go to the Testing tab.
-
In the Payload field, enter:
{"action":"convert", "src_url":"<link_to_video>"}
Where
<link_to_video>
is a link to an MP4 video file saved to Yandex Disk . -
Click Run test.
-
You will see the task ID in the Function output field:
{ "task_id": "c4269ceb-8d3a-40fe-95f0-84cf********" }
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 make it into the queue and get processed. To do this, view the queue statistics.
- In the management console
, select the folder containingconverter-queue
. - Select Message Queue.
- Select the
converter-queue
queue. - Under General information, you can see how many messages are in the queue and how many are being processed.
- 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 containing theffmpeg-converter
function. - Select Cloud Functions.
- Select the
ffmpeg-converter
function. - 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 containing theffmpeg-api
function. -
Select Cloud Functions.
-
Select the
ffmpeg-api
function. -
Go to the Testing tab.
-
In the Payload field, enter the following request:
{"action":"get_task_status", "task_id":"<job_ID>"}
-
Click Run test.
-
If video conversion to GIF is not completed, the Function output field returns:
{ "ready": false }
Otherwise, you will get a link to the GIF file:
{ "ready": true, "gif_url": "https://storage.yandexcloud.net/<bucket_name>/1b4db1a6-f2b2-4b1c-b662-37f7********.gif?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=qxLftbbZ91U695ysemyZ%2F202********ru-central1%2Fs3%2Faws4_request&X-Amz-Date=20210831T110351Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=f4a5fe7848274a09be5b221fbf8a9f6f2b385708cfa351861a4e69df********" }
How to delete the resources you created
To shut down the infrastructure and stop paying for the resources you created: