Converting a video to a GIF in Python using the management console
To create a video-to-GIF conversion framework in Python using the Yandex Cloud management console:
- 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 for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. 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 create or select a folder for your infrastructure on the cloud page
Learn more about clouds and folders here.
Required paid resources
The infrastructure support cost includes:
- Fee for invoking functions (see Yandex Cloud Functions pricing).
- Fee for running queries to the database (see Yandex Managed Service for YDB pricing).
- Fee for storing data in a bucket (see Yandex Object Storage pricing).
Create resources
-
Clone the repository
with configuration files:git clone https://github.com/yandex-cloud-examples/yc-serverless-video-gif-converter.git -
Create a service account named
ffmpeg-saand assign it the following roles:ymq.readerymq.writerlockbox.payloadViewerstorage.viewerstorage.uploaderydb.adminfunctions.functionInvoker
-
Create a static key for the service account. Save the Key ID and Your secret key.
-
Create a secret named
ffmpeg-sa-secretin Yandex Lockbox:-
Select Secret type: Custom.
-
Under Version, specify:
ACCESS_KEY_IDas the key and Key ID from the previous step as the value.SECRET_ACCESS_KEYas 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-queuein Yandex Message Queue. Save the queue URL from the General information section. -
Create a YDB database in
Serverlessmode. 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_idof theStringtype. 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 thetaskstable 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.txtand specify these libraries in it:boto3 yandexcloud -
Create a file named
index.pyand paste into it the contents of theffmpeg-api.pyfile from theffmpeg-api.ziparchive. -
Specify the following parameters:
- Runtime environment:
python312. - 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.ziparchive with the following files:-
requirements.txtfile:boto3 requests yandexcloud -
The
index.pyfile with the contents of theffmpeg-converter.pyfile from thesrc.ziparchive. -
The FFmpeg executable. On FFmpeg's official website
, navigate to the Linux Static Builds section, download the 64-bit FFmpeg archive, and make the file executable by running thechmod +x ffmpegcommand.
-
-
Upload
src.zipto the bucket you created earlier. -
Create a function version:
-
Specify the following parameters:
- Runtime environment:
python312. - Code source:
Object Storageupload 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
converter-queuemessage queue and the service account namedffmpeg-sawith permission 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-apifunction. -
Select Cloud Functions.
-
Select the
ffmpeg-apifunction. -
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
Once the task is created, the queued message count will increase by one and a trigger will fire. 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
converter-queue. - Under General information, you can see the queued and processed message counts.
- 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-converterfunction. - Select Cloud Functions.
- Select
ffmpeg-converter. - Go to the Logs tab and specify the period to view the logs for.
Get a link to a GIF file
-
In the management console
, select the folder containing theffmpeg-apifunction. -
Select Cloud Functions.
-
Select the
ffmpeg-apifunction. -
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 stop paying for the resources you created:
- Delete
converter-queue. - Delete the database.
- Delete all objects from the bucket.
- Delete the bucket.
- Delete the
ffmpeg-apiandffmpeg-converterfunctions. - Delete the
ffmpeg-triggertrigger.