Media Grouping

Description

Analyze a batch of images or videos and group similar assets. Results include asset URLs and group assignments in submission order.

Version

0.0.2

Media Requirements

Submit images or videos using URLs that the service can download.

Request URL

Production environment: https://openapi.meitu.com
Task submission endpoint: https://openapi.meitu.com/api/v1/sdk/sync/push

HTTP Method

POST

Content-Type: application/json

Authentication

Use AK/SK signing.

Request Parameters

RequiredParameterTypeDescription
Yestaskstring/v1/Cluster_Salon/497372
Yestask_typestringFixed value: formula.
Yesinit_imagesobject[]Assets to group.
YesparamsstringAlgorithm parameters encoded as a JSON string.
Nosync_timeoutintegerThe example uses -1 to submit without waiting for results.

init_images

RequiredParameterTypeDescription
YesurlstringImage or video URL.
—profileobjectAsset properties.

profile

RequiredParameterTypeDescription
—media_profilesobjectThe example sets media_data_type to url.
Nomedia_extraobjectAdditional asset information. The asset ID is taken from asset_id, then name, then the filename in the URL.

params

In the example, the decoded params JSON contains the parameter object.

parameter

RequiredParameterTypeDefaultDescription
Noservice_typestringhairService type. Use hair for hair salon media.
Nopipelinestringv6_productionGrouping mode. Supported values: v6_production and v5_final.
Nolimitinteger0Maximum number of assets to process. Must be non-negative. 0 means no limit.
Noframes——Shared frame sample count for v6_production. Omit to use the service default.
Nodry_runbooleanfalseSet to true to plan the run without extracting features or assigning groups.
Norsp_media_typestringurlResponse media type. Use url.

Request Example

Submit two videos for grouping. Replace the asset URLs before calling the API.

{
  "task": "/v1/Cluster_Salon/497372",
  "task_type": "formula",
  "init_images": [
    {
      "url": "https://example.com/input/clip-01.mp4",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    },
    {
      "url": "https://example.com/input/clip-02.mp4",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    }
  ],
  "params": "{\"parameter\":{\"service_type\":\"hair\",\"pipeline\":\"v6_production\",\"dry_run\":false}}",
  "sync_timeout": -1
}

Response Fields

FieldTypeDescription
---------
error_codeintegerGateway error code.
messagestringProcessing message.
data.statusintegerTask status. Read the grouping results after the task succeeds.
data.task_idstringGateway task ID used to query the task.
data.resultobjectTask result.
data.progressnumberTask progress. 1 indicates completion.

Grouping Result Fields

FieldTypeDescription
parameterobjectAlways an empty object ({}).
media_info_listobject[]Result assets, in submission order.
media_info_listobject[]Result assets in submission order.
media_info_list[].media_datastringResult asset URL.
media_info_list[].media_profiles.media_data_typestringAlways url, indicating media_data is a URL.
media_info_list[].media_profiles.media_data_typestringurl indicates that media_data is a URL.
media_info_list[].media_extra.media_profiles.media_data_typestringAsset format, such as MP4 or JPG.
media_info_list[].media_extra.media_profiles.media_data_group_id—Asset group ID, starting at 1. Omitted when dry_run=true.
media_info_list[].media_extra.media_profiles.media_data_group_id—Group ID, starting at 1. Omitted when dry_run=true.

Response Example

Response Examples

Algorithm Result Example

Successful Response

Both assets have group ID 1, so they belong to the same group. Response Status: 200

Content-Type: application/json; charset=utf-8

This example shows grouping results in data.result.data. Both assets have group ID 1, so they belong to the same group. Read group IDs from the individual media items, not the URL list.

{
  "error_code": 0,
  "message": "success",
  "data": {
    "status": 10,
    "task_id": "example-task-id",
    "result": {
      "id": "example-task-id",
      "urls": [
        "https://example.com/result/clip-01.mp4",
        "https://example.com/result/clip-02.mp4"
      ],
      "data": {
  "parameter": {},
  "media_info_list": [
    {
      "media_data": "https://example.com/result/clip-01.mp4",
      "media_extra": {
        "media_profiles": {
          "media_data_type": "MP4",
          "media_data_group_id": 1
        }
      },
      "media_profiles": {
        "media_data_type": "url"
        "parameter": {},
        "media_info_list": [
          {
            "media_data": "https://example.com/result/clip-01.mp4",
            "media_extra": {
              "media_profiles": {
                "media_data_type": "MP4",
                "media_data_group_id": 1
              }
            },
            "media_profiles": {
              "media_data_type": "url"
            }
          },
          {
            "media_data": "https://example.com/result/clip-02.mp4",
            "media_extra": {
              "media_profiles": {
                "media_data_type": "MP4",
                "media_data_group_id": 1
              }
            },
            "media_profiles": {
              "media_data_type": "url"
            }
          }
        ]
      }
    },
    {
      "media_data": "https://example.com/result/clip-02.mp4",
      "media_extra": {
        "media_profiles": {
          "media_data_type": "MP4",
          "media_data_group_id": 1
        }
    "progress": 1
  }
      },
      "media_profiles": {
        "media_data_type": "url"
      }
    }
  ]
}

Task Query

When to Query the Task

If the result is not ready after submission, use the gateway's data.task_id with the Task Query API. If the task is still pending after submission, pass the gateway's data.task_id to the Task Query API. A synchronous response with data.status = 9 means the wait timed out; it does not mean the task failed.

In the query response, data.status = 0 or 1 means the task is not complete, 10 means it succeeded, and 2 means it failed.

Common Error Codes

See API Error Codes.

SDK Examples

Before making a request, fill in your access and secret keys and replace the asset URLs.

Python

Python SDK

import requests
from sign_sdk import sign

url = "https://openapi.meitu.com/api/v1/sdk/sync/push"
body = r'''{
  "task": "/v1/Cluster_Salon/497372",
  "task_type": "formula",
  "init_images": [
    {
      "url": "https://example.com/input/clip-01.mp4",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    },
    {
      "url": "https://example.com/input/clip-02.mp4",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    }
  ],
  "params": "{\"parameter\":{\"service_type\":\"hair\",\"pipeline\":\"v6_production\",\"dry_run\":false}}",
  "sync_timeout": -1
}'''
headers = {sign.HeaderHost: "openapi.meitu.com", "Content-Type": "application/json"}
signer = sign.Signer("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY")
request = signer.sign(url, "POST", headers, body)
with requests.Session() as session:
    response = session.send(request, timeout=(10, 60), allow_redirects=False)
    print(response.status_code, response.text)

Go

Go SDK

package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"time"

	"github.com/mtlab/api/signer"
)

func main() {
	url := "https://openapi.meitu.com/api/v1/sdk/sync/push"
	body := `{
  "task": "/v1/Cluster_Salon/497372",
  "task_type": "formula",
  "init_images": [
    {
      "url": "https://example.com/input/clip-01.mp4",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    },
    {
      "url": "https://example.com/input/clip-02.mp4",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    }
  ],
  "params": "{\"parameter\":{\"service_type\":\"hair\",\"pipeline\":\"v6_production\",\"dry_run\":false}}",
  "sync_timeout": -1
}`
	headers := make(http.Header)
	headers.Set(signer.HeaderHost, "openapi.meitu.com")
	headers.Set("Content-Type", "application/json")
	s := signer.NewSigner("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY")
	req, err := s.Sign(url, http.MethodPost, headers, body)
	if err != nil {
		log.Fatal(err)
	}
	client := &http.Client{
		Timeout: 60 * time.Second,
		CheckRedirect: func(*http.Request, []*http.Request) error {
			return http.ErrUseLastResponse
		},
	}
	res, err := client.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
	result, err := io.ReadAll(res.Body)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.StatusCode, string(result))
}

PHP

PHP SDK

<?php
require_once __DIR__ . '/signer.php';

$url = 'https://openapi.meitu.com/api/v1/sdk/sync/push';
$body = <<<'JSON'
{
  "task": "/v1/Cluster_Salon/497372",
  "task_type": "formula",
  "init_images": [
    {
      "url": "https://example.com/input/clip-01.mp4",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    },
    {
      "url": "https://example.com/input/clip-02.mp4",
      "profile": {
        "media_profiles": {
          "media_data_type": "url"
        }
      }
    }
  ],
  "params": "{\"parameter\":{\"service_type\":\"hair\",\"pipeline\":\"v6_production\",\"dry_run\":false}}",
  "sync_timeout": -1
}
JSON;
$headers = ['Host' => 'openapi.meitu.com', 'Content-Type' => 'application/json'];
$signer = new Signer('YOUR_ACCESS_KEY', 'YOUR_SECRET_KEY');
$curl = $signer->sign($url, 'POST', $headers, $body);
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false,
    CURLOPT_CONNECTTIMEOUT => 10,
    CURLOPT_TIMEOUT => 60,
    CURLOPT_FOLLOWLOCATION => false,
    CURLOPT_SSL_VERIFYPEER => true,
    CURLOPT_SSL_VERIFYHOST => 2,
]);
$result = curl_exec($curl);
if ($result === false) {
    $error = curl_error($curl);
    curl_close($curl);
    throw new RuntimeException($error);
}
echo curl_getinfo($curl, CURLINFO_HTTP_CODE), PHP_EOL, $result, PHP_EOL;
curl_close($curl);

Java

Java SDK

package com.meitu.openai.common;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) throws Exception {
        String url = "https://openapi.meitu.com/api/v1/sdk/sync/push";
        String body = "{\n" +
                "  \"task\": \"/v1/Cluster_Salon/497372\",\n" +
                "  \"task_type\": \"formula\",\n" +
                "  \"init_images\": [\n" +
                "    {\n" +
                "      \"url\": \"https://example.com/input/clip-01.mp4\",\n" +
                "      \"profile\": {\n" +
                "        \"media_profiles\": {\n" +
                "          \"media_data_type\": \"url\"\n" +
                "        }\n" +
                "      }\n" +
                "    },\n" +
                "    {\n" +
                "      \"url\": \"https://example.com/input/clip-02.mp4\",\n" +
                "      \"profile\": {\n" +
                "        \"media_profiles\": {\n" +
                "          \"media_data_type\": \"url\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  ],\n" +
                "  \"params\": \"{\\\"parameter\\\":{\\\"service_type\\\":\\\"hair\\\",\\\"pipeline\\\":\\\"v6_production\\\",\\\"dry_run\\\":false}}\",\n" +
                "  \"sync_timeout\": -1\n" +
                "}";
        Map<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/json");
        headers.put(Signer.HeaderHost, "openapi.meitu.com");
        Signer signer = new Signer("YOUR_ACCESS_KEY", "YOUR_SECRET_KEY");
        Map<String, String> signed = signer.sign(url, "POST", headers, body);
        HttpURLConnection connection = (HttpURLConnection) URI.create(url).toURL().openConnection();
        try {
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(10000);
            connection.setReadTimeout(60000);
            connection.setInstanceFollowRedirects(false);
            signed.forEach(connection::setRequestProperty);
            connection.setDoOutput(true);
            try (OutputStream out = connection.getOutputStream()) {
                out.write(body.getBytes(StandardCharsets.UTF_8));
            }
            int status = connection.getResponseCode();
            try (InputStream in = status >= 400 ? connection.getErrorStream() : connection.getInputStream()) {
                String result = in == null ? "" : new String(in.readAllBytes(), StandardCharsets.UTF_8);
                System.out.println(status + " " + result);
            }
        } finally {
            connection.disconnect();
        }
    }
}