[Yandex Cloud documentation](../../../index.md) > [Yandex Object Storage](../../index.md) > [Tutorials](../index.md) > [Converting a video to a GIF in Python](index.md) > Management console

# Converting a video to a GIF in Python using the management console

To create a [video-to-GIF conversion framework in Python](index.md) using the Yandex Cloud management console:

1. [Get your cloud ready](#before-begin).
1. [Create resources](#create-resources).
1. [Create an API function](#create-api-function).
1. [Create a converter function](#create-converter-function).
1. [Create a trigger](#create-trigger).
1. [Test the application](#test-app).

If you no longer need the resources you created, [delete them](#clear-out).


## Get your cloud ready {#before-begin}

Sign up for Yandex Cloud and create a [billing account](../../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../../billing/quickstart/index.md) and [link](../../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../../resource-manager/concepts/resources-hierarchy.md).


### Required paid resources {#paid-resources}

The infrastructure support cost includes:
* Fee for invoking [functions](../../../functions/concepts/function.md) (see [Yandex Cloud Functions pricing](../../../functions/pricing.md)).
* Fee for running queries to the [database](../../../ydb/concepts/serverless-and-dedicated.md) (see [Yandex Managed Service for YDB pricing](../../../ydb/pricing/serverless.md)).
* Fee for storing data in a [bucket](../../concepts/bucket.md) (see [Yandex Object Storage pricing](../../pricing.md)).


## Set up resources {#create-resources}

1. Clone the [repository](https://github.com/yandex-cloud-examples/yc-serverless-video-gif-converter) with configuration files:

   ```bash
   git clone https://github.com/yandex-cloud-examples/yc-serverless-video-gif-converter.git
   ```

1. [Create](../../../iam/operations/sa/create.md) a service account named `ffmpeg-sa` and [assign](../../../iam/operations/sa/assign-role-for-sa.md) the following roles to it:

   * `ymq.reader`
   * `ymq.writer`
   * `lockbox.payloadViewer`
   * `storage.viewer`
   * `storage.uploader`
   * `ydb.admin`
   * `functions.functionInvoker`

1. [Create a static key](../../../iam/operations/authentication/manage-access-keys.md#create-access-key) for the service account. Save the **Key ID** and **Your secret key**.
1. [Create a secret](../../../lockbox/operations/secret-create.md) named `ffmpeg-sa-secret` in Yandex Lockbox:

      1. Select **Secret type**: **Custom**.
      1. Set two key-value pairs:

         * `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.

1. [Create a message queue](../../../message-queue/operations/message-queue-new-queue.md) named `converter-queue` in Yandex Message Queue. Save the queue **URL** from the **General information** section.
1. [Create a YDB database](../../../ydb/quickstart.md#serverless) in `Serverless` mode. Save the **Endpoint** from the **Document API endpoint** section.
1.  [Create a table](../../../ydb/operations/schema.md#create-table)  in the database:

    * **Name**: `tasks`.
    * **Table type**: [Document table](../../../ydb/operations/schema.md#create-table).
    * **Columns**: One column named `task_id` of the `String` type. Set the [Partition key](../../../ydb/operations/schema.md#create-table) attribute.

1. [Create a bucket](../../operations/buckets/create.md) with restricted access in Yandex Object Storage.


## Create an API function {#create-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 the `tasks` table  using the [Document API](../../../ydb/docapi/tools/aws-http.md). 
* `get_task_status`: Get the task status. The function checks whether the task has been completed and returns a link to a GIF file.

{% list tabs group=instructions %}

- Management console {#console}

  1. [Create](../../../functions/operations/function/function-create.md) a function named `ffmpeg-api`.
  1. [Create](../../../functions/operations/function/version-manage.md) a function version:

     1. Create a file named `requirements.txt` and specify these libraries in it:

        ```text
        boto3
        yandexcloud
        ```

     1. Create a file named `index.py` and paste into it the contents of the `ffmpeg-api.py` file from the `ffmpeg-api.zip` archive.
     1. Specify the following settings:

        * **Runtime environment**: `python312`.
        * **Entry point**: `index.handle_api`.
        * **Timeout**: `5`.
        * **Service account**: `ffmpeg-sa`.

     1. Add these environment variables:

        * `DOCAPI_ENDPOINT`: **Endpoint** from the database configuration.
        * `SECRET_ID`: Yandex Lockbox secret **ID**.
        * `YMQ_QUEUE_URL`: Message Queue **URL**.

{% endlist %}


## Create a converter function {#create-converter-function}

A converter function is run by a trigger. It processes videos and registers the result in the `tasks` table.

The video is converted using FFmpeg. The FFmpeg executable is larger than 70 MB. To upload it along with the function code, create a ZIP archive and upload it through Object Storage. Learn more about [code upload formats](../../../functions/concepts/function.md).

{% list tabs group=instructions %}

- Management console {#console}

  1. [Create](../../../functions/operations/function/function-create.md) a function named `ffmpeg-converter`.
  1. Create an archive named `src.zip` with the following files:

     * `requirements.txt`:

       ```text
       boto3
       requests
       yandexcloud
       ```
      
     * The `index.py` file with the contents of the `ffmpeg-converter.py` file from the `src.zip` archive.
     * FFmpeg executable. On [FFmpeg's official website](http://ffmpeg.org/download.html), navigate to the **Linux Static Builds** section, download the 64-bit FFmpeg archive, and make the file executable by running the `chmod +x ffmpeg` command.

  1. [Upload](../../operations/objects/upload.md) `src.zip` to the bucket you created earlier.
  1. [Create](../../../functions/operations/function/version-manage.md) a function version:

     1. Specify the following settings:

        * **Runtime environment**: `python312`.
        * **Code source**: `Object Storage` upload method.
        * **Bucket**: Name of the bucket.
        * **Object**: `src.zip`.
        * **Entry point**: `index.handle_process_event`.
        * **Timeout**: `600`.
        * **Memory**: `2048 MB`.
        * **Service account**: `ffmpeg-sa`.

     1. Add these environment variables:

        * `DOCAPI_ENDPOINT`: **Endpoint** from the database configuration.
        * `SECRET_ID`: Yandex Lockbox secret **ID**.
        * `YMQ_QUEUE_URL`: Message Queue **URL**.
        * `S3_BUCKET`: Name of the bucket you created earlier.

{% endlist %}


## Create a trigger {#create-trigger}

A message queue is processed using a [trigger for Message Queue](../../../functions/concepts/trigger/ymq-trigger.md). It invokes the converter function as messages arrive in `converter-queue`. 

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder where you want to create a trigger.
  1. Select **Cloud Functions**.
  1. Navigate to the **Triggers** tab.
  1. Click **Create trigger**.
  1. Under **Basic settings**:
     * Name the trigger: `ffmpeg-trigger`.
     * In the **Type** field, select `Message Queue`.
  1. Under **Message Queue message settings**, select the `converter-queue` message queue and the service account named `ffmpeg-sa` with permission to read messages from it.
  1. Under **Function settings**:
     * Select the function the trigger will invoke: `ffmpeg-converter`.
     * Specify the [function version tag](../../../functions/concepts/function.md#tag): `$latest`.
     * Specify the service account that will invoke the function: `ffmpeg-sa`.
  1. Click **Create trigger**.

{% endlist %}


## Test the application {#test-app}

### Create a task {#create-task}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder containing the `ffmpeg-api` function.
  1. Select **Cloud Functions**.
  1. Select the `ffmpeg-api` function.
  1. Navigate to the **Testing** tab.
  1. In the **Payload** field, enter:

     ```json
     {"action":"convert", "src_url":"<link_to_video>"}
     ```

     Where `<link_to_video>` is a link to an [MP4](https://en.wikipedia.org/wiki/MP4_file_format) video file saved to [Yandex Disk](https://disk.yandex.com).

  1. Click **Run test**.
  1. You will see the task ID in the **Function output** field:

     ```json
     { "task_id": "c4269ceb-8d3a-40fe-95f0-84cf********" }
     ```

{% endlist %}

### View the queue statistics {#ymq-stat}

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.

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder housing `converter-queue`.
  1. Select **Message Queue**.
  1. Select `converter-queue`.
  1. Under **General information**, you can see the queued and processed message counts.
  1. Go to **Monitoring**. View the **Overall queue stats** charts.

{% endlist %}

### View the function logs {#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.

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder containing the `ffmpeg-converter` function.
  1. Select **Cloud Functions**.
  1. Select `ffmpeg-converter`.
  1. Go to the **Logs** tab and specify the period to view the logs for.

{% endlist %}

### Get a link to a GIF file {#get-link}

{% list tabs group=instructions %}

- Management console {#console}

  1. In the [management console](https://console.yandex.cloud), select the folder containing the `ffmpeg-api` function.
  1. Select **Cloud Functions**.
  1. Select the `ffmpeg-api` function.
  1. Navigate to the **Testing** tab.
  1. In the **Payload** field, enter the following request:

     ```json
     {"action":"get_task_status", "task_id":"<job_ID>"}
     ```

  1. Click **Run test**.
  1. If video conversion to GIF has not been completed, the **Function output** field will return:

     ```json
     {
         "ready": false
     }
     ```

     Otherwise, you will get a link to the GIF file:

     ```json
     {
         "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********"
     }
     ```

{% endlist %}


## How to delete the resources you created {#clear-out}

To stop paying for the resources you created:

1. [Delete](../../../message-queue/operations/message-queue-delete-queue.md) `converter-queue`.
1. [Delete](../../../ydb/operations/manage-databases.md#delete-db) the database.
1. [Delete](../../operations/objects/delete.md) all objects from the bucket.
1. [Delete](../../operations/buckets/delete.md) the bucket.
1. [Delete](../../../functions/operations/function/function-delete.md) the `ffmpeg-api` and `ffmpeg-converter` functions.
1. [Delete](../../../functions/operations/trigger/trigger-delete.md) `ffmpeg-trigger`.

#### Useful links {#see-also}

* [Converting a video to a GIF in Python using Terraform](terraform.md)