Converting a video to a GIF in Python
You will create a video converter using FFmpeg
To create an application:
- Get your cloud ready.
- Create 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 in Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or register a new account. - 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 and link a cloud to it.
If you have an active billing account, you can navigate 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).
Create resources
-
Download
the archive with the files required 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
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 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:
- Name:
tasks
. - Table type: Document table.
- Columns: One column named
task_id
of theString
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 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 has been 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 this library in it:boto3
-
Create a file named
index.py
and paste the contents offfmpeg-api.py
from the archive into it. -
Specify the following settings:
- Runtime environment:
python37
- Entry point:
index.handle_api
- Timeout:
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
: URL of the Message Queue-enabled queue.
-
Create a converter function
A converter function is run by a trigger. It processes videos and registers the result in the tasks
table.
Video conversion is based on the FFmpeg utility. The FFmpeg executable 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
src.zip
archive 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. Go to the FFmpeg official website
, navigate to the Linux Static Builds section, download the archive with the 64-bit FFmpeg version, and make the file executable by running thechmod +x ffmpeg
command.
-
-
Upload
src.zip
to the bucket you created earlier. -
Create a function version:
-
Specify the following settings:
- 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:
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
: URL of the Message Queue-enabled queue.S3_BUCKET
: Name of the bucket you created earlier.
-
Create a trigger
A message queue is handled using a trigger for Message Queue. It invokes the converter function as messages arrive in converter-queue
.
- In the management console
, select the folder where you want to create a trigger. - Select Cloud Functions.
- Navigate 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 message queue named
converter-queue
and theffmpeg-sa
service account with rights to read messages 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 which will 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. -
Navigate 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 messages arrive in the queue and are handled. To do this, view the queue statistics.
- In the management console
, select the folder housingconverter-queue
. - Select Message Queue.
- Select the
converter-queue
queue. - Under General information, you can see the number of enqueued messages and those being handled.
- Navigate 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. - Navigate to the Logs tab and specify the period over which you want to view them.
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. -
Navigate 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 has not been completed, the Function output field will return:
{ "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: