Converting a video to a GIF in Python
You will create a video converter using FFmpeg
To create an application:
- Prepare your cloud.
- 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.
Prepare your cloud
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:
- 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 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.
- 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
: 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. -
Specify the following parameters:
- Runtime environment:
python37
- Entry point:
index.handle_api
- Timeout, sec:
5
- Service account:
ffmpeg-sa
- Runtime environment:
-
Add 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
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. 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 previously created bucket. -
Create a function version:
-
Specify the following parameters:
- Runtime environment:
python37
- Method:
Object Storage
upload method - Bucket: Name of the previously created bucket
- Object:
src.zip
- Entry point:
index.handle_process_event
- Timeout, sec:
600
- Memory:
2048 MB
- Service account:
ffmpeg-sa
- Runtime environment:
-
Add environment variables:
DOCAPI_ENDPOINT
: Endpoint from the database configurationSECRET_ID
: Yandex Lockbox secret IDYMQ_QUEUE_URL
: Message Queue queue URLS3_BUCKET
: Name of the previously created 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
, select the folder where you want to create a trigger. - Select Cloud Functions.
- Go to the Triggers tab.
- Click Create trigger.
- Under Basic settings:
- Name the trigger
ffmpeg-trigger
. - In the Type field, select
Message Queue
.
- Name the trigger
- Under Message Queue message settings, select
converter-queue
and theffmpeg-sa
service account with the permissions to read messages from the queue. - 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 theffmpeg-api
function. -
Select Cloud Functions.
-
Select
ffmpeg-api
. -
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 arrive in the queue and are handled. To do this, view the queue statistics.
- In the management console
, select the folder withconverter-queue
. - Select 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 theffmpeg-converter
function. - Select 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 theffmpeg-api
function. -
Select Cloud Functions.
-
Select
ffmpeg-api
. -
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: