This topic provides code examples for General-purpose Video Face Fusion in popular programming languages for various scenarios.
-
For real-time assistance, start an online consultation.
-
If you have questions about API access or usage for the Alibaba Cloud Vision AI Platform, join our DingTalk group (ID: 23109592) to contact us.
Overview
For an overview of the General-purpose Video Face Fusion feature and its request parameters, see General-purpose Video Face Fusion.
Install the SDK package
For information about the SDK dependency packages for popular programming languages, see SDK overview.
Configure environment variables
Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables.
An Alibaba Cloud account has full access to all API operations. We recommend that you use a RAM user for API calls or routine O&M. For more information, see Create a RAM user.
Do not save your AccessKey ID or AccessKey Secret in project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised.
Configure environment variables on Linux and macOS
Open a terminal in IntelliJ IDEA.
Run the following commands to configure the environment variables.
Replace
<access_key_id>with the AccessKey ID of your RAM user and<access_key_secret>with the AccessKey Secret of your RAM user. If you need to configure more permissions, see Control access permissions using a RAM policy.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>
Configure environment variables on Windows
Create a new environment variable file, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared. Then, restart the Windows operating system. The following example uses Windows 10.Open File Explorer, right-click This PC, and then select Properties.
In the left-side navigation pane, click Advanced system settings.
On the Advanced tab of the System Properties dialog box, click Environment Variables.
In the Environment Variables dialog box, click New.
In the New System Variable dialog box, add the environment variables
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRET, and set them to the AccessKey ID and AccessKey Secret that you have prepared.Restart the Windows operating system for the configurations to take effect.
Sample code
The General-purpose Video Face Fusion service only supports OSS image and video URLs in the Shanghai region (cn-shanghai).
For web or browser-based applications, do not make direct API calls from the frontend. Instead, upload files to a backend server and call the API from the backend using a URL or local file stream.
If you do not have an OSS bucket in the Shanghai region, see the Local file or URL section for sample code that uses the Advance API to upload files by using a file stream.
OSS file in the Shanghai region
For sample code on querying asynchronous task results, see Query asynchronous task results.
/*
Import the dependency package
<!-- https://mvnrepository.com/artifact/com.aliyun/videoenhan20200320 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>videoenhan20200320</artifactId>
<version>${aliyun.videoenhan.version}</version>
</dependency>
*/
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.videoenhan20200320.models.MergeVideoFaceResponse;
public class MergeVideoFace {
public static com.aliyun.videoenhan20200320.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object com.aliyun.teaopenapi.models.Config.
This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The domain name of the service.
config.endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
return new com.aliyun.videoenhan20200320.Client(config);
}
public static void main(String[] args) throws Exception {
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.videoenhan20200320.Client client = MergeVideoFace.createClient(accessKeyId, accessKeySecret);
com.aliyun.videoenhan20200320.models.MergeVideoFaceRequest mergeVideoFaceRequest = new com.aliyun.videoenhan20200320.models.MergeVideoFaceRequest()
.setVideoURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4")
.setReferenceURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
MergeVideoFaceResponse mergeVideoFaceResponse = client.mergeVideoFaceWithOptions(mergeVideoFaceRequest, runtime);
// Obtain the entire response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(mergeVideoFaceResponse)));
// Obtain a specific field.
System.out.println(mergeVideoFaceResponse.getBody());
} catch (TeaException teaException) {
// Obtain the complete error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// Obtain a specific field.
System.out.println(teaException.getCode());
}
}
}# -*- coding: utf-8 -*-
# Import the dependency package.
# pip install alibabacloud_videoenhan20200320
import os
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
from alibabacloud_videoenhan20200320.client import Client
from alibabacloud_videoenhan20200320.models import MergeVideoFaceRequest
config = Config(
# For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
# Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The domain name of the service.
endpoint='videoenhan.cn-shanghai.aliyuncs.com',
# The region that corresponds to the domain name.
region_id='cn-shanghai'
)
merge_video_face_request = MergeVideoFaceRequest(
video_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4',
reference_url='http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png'
)
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.merge_video_face_with_options(merge_video_face_request, runtime)
# Obtain the entire response.
print(response.body)
except Exception as error:
# Obtain the complete error message.
print(error)
# Obtain a specific field.
print(error.code)<?php
// Install the dependency package.
//composer require alibabacloud/videoenhan-20200320
use AlibabaCloud\SDK\Videoenhan\V20200320\Videoenhan;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Videoenhan\V20200320\Models\MergeVideoFaceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class MergeVideoFace {
/**
* Initialize the client using an AccessKey pair.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Videoenhan Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object Darabonba\OpenApi\Models\Config.
// This object stores configurations such as your accessKeyId, accessKeySecret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The domain name of the service.
$config->endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
return new Videoenhan($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
$mergeVideoFaceRequest = new MergeVideoFaceRequest([
"videoURL" => "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4",
"referenceURL" => "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png"
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->mergeVideoFaceWithOptions($mergeVideoFaceRequest, $runtime);
# Obtain the entire response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Obtain the complete error message.
echo Utils::toJSONString($exception);
# Obtain a specific field.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// The $argv parameter is reserved and does not require modification.
MergeVideoFace::main(array_slice($argv, 1));// Install the dependency package.
// npm install @alicloud/videoenhan20200320
const VideoenhanClient = require('@alicloud/videoenhan20200320');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
let config = new OpenapiClient.Config({
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The domain name of the service.
config.endpoint = `videoenhan.cn-shanghai.aliyuncs.com`;
const client = new VideoenhanClient.default(config);
let mergeVideoFaceRequest = new VideoenhanClient.MergeVideoFaceRequest({
referenceURL: "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png",
videoURL: "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4",
});
let runtime = new TeaUtil.RuntimeOptions({});
client.mergeVideoFaceWithOptions(mergeVideoFaceRequest, runtime)
.then(function (mergeVideoFaceResponse) {
// Obtain the entire response.
console.log(mergeVideoFaceResponse);
// Obtain a specific field.
console.log(mergeVideoFaceResponse.body.data);
}, function (error) {
// Obtain the complete error message.
console.log(error);
// Obtain a specific field.
console.log(error.data.Code);
})/**
This code depends on github.com/alibabacloud-go/videoenhan-20200320/v3.
We recommend that you run the `go mod tidy` command to install the dependency.
*/
package main
import (
"fmt"
"os"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
videoenhan20200320 "github.com/alibabacloud-go/videoenhan-20200320/v3/client"
)
func main() {
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object &openapi.Config. This object stores configurations such as your AccessKeyId, AccessKeySecret, and Endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The domain name of the service.
config.Endpoint = tea.String("videoenhan.cn-shanghai.aliyuncs.com")
client, err := videoenhan20200320.NewClient(config)
if err != nil {
panic(err)
}
mergeVideoFaceRequest := &videoenhan20200320.MergeVideoFaceRequest{
ReferenceURL: tea.String("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png"),
VideoURL: tea.String("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4"),
}
runtime := &util.RuntimeOptions{}
mergeVideoFaceResponse, err := client.MergeVideoFaceWithOptions(mergeVideoFaceRequest, runtime)
if err != nil {
// Obtain the complete error message.
fmt.Println(err.Error())
} else {
// Obtain the entire response.
fmt.Println(mergeVideoFaceResponse)
}
}// Install the dependency package.
// dotnet add package AlibabaCloud.SDK.Videoenhan20200320
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using AlibabaCloud.SDK.Videoenhan20200320.Models;
using Tea;
using Tea.Utils;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initialize the client using an AccessKey pair.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Videoenhan20200320.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
// The domain name of the service.
config.Endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Videoenhan20200320.Client(config);
}
public static void Main(string[] args)
{
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
AlibabaCloud.SDK.Videoenhan20200320.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
MergeVideoFaceRequest mergeVideoFaceRequest = new MergeVideoFaceRequest
{
ReferenceURL = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png",
VideoURL = "http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4",
};
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
MergeVideoFaceResponse mergeVideoFaceResponse = client.MergeVideoFaceWithOptions(mergeVideoFaceRequest, runtime);
// Obtain the entire response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(mergeVideoFaceResponse.Body));
// Obtain a specific field.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(mergeVideoFaceResponse.Body.Data));
}
catch (TeaException error)
{
// If an error occurs, print the error message.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// If an error occurs, print the error message.
Console.WriteLine(error.Message);
}
}
}
}Local file or URL
For sample code on querying asynchronous task results, see Query asynchronous task results.
/*
Import the dependency package
<!-- https://mvnrepository.com/artifact/com.aliyun/videoenhan20200320 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>videoenhan20200320</artifactId>
<version>${aliyun.videoenhan.version}</version>
</dependency>
*/
import com.aliyun.tea.TeaException;
import com.aliyun.tea.TeaModel;
import com.aliyun.videoenhan20200320.models.MergeVideoFaceResponse;
import java.io.InputStream;
import java.net.URL;
public class MergeVideoFace {
public static com.aliyun.videoenhan20200320.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
/*
Initialize the configuration object com.aliyun.teaopenapi.models.Config.
This object stores configurations such as your AccessKey ID, AccessKey Secret, and endpoint.
*/
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
.setAccessKeyId(accessKeyId)
.setAccessKeySecret(accessKeySecret);
// The domain name of the service.
config.endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
return new com.aliyun.videoenhan20200320.Client(config);
}
public static void main(String[] args) throws Exception {
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
com.aliyun.videoenhan20200320.Client client = MergeVideoFace.createClient(accessKeyId, accessKeySecret);
// Scenario 1: Use a local file.
// InputStream inputStream1 = new FileInputStream(new File("/tmp/MergeVideoFace.mp4"));
// InputStream inputStream2 = new FileInputStream(new File("/tmp/MergeVideoFace-pic.png"));
// Scenario 2: Use an accessible URL.
URL url1 = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4");
URL url2 = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png");
InputStream inputStream1 = url1.openConnection().getInputStream();
InputStream inputStream2 = url2.openConnection().getInputStream();
com.aliyun.videoenhan20200320.models.MergeVideoFaceAdvanceRequest mergeVideoFaceAdvanceRequest = new com.aliyun.videoenhan20200320.models.MergeVideoFaceAdvanceRequest()
.setVideoURLObject(inputStream1)
.setReferenceURLObject(inputStream2);
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
MergeVideoFaceResponse mergeVideoFaceAdvanceResponse = client.mergeVideoFaceAdvance(mergeVideoFaceAdvanceRequest, runtime);
// Obtain the entire response.
System.out.println(com.aliyun.teautil.Common.toJSONString(TeaModel.buildMap(mergeVideoFaceAdvanceResponse)));
// Obtain a specific field.
System.out.println(mergeVideoFaceAdvanceResponse.getBody());
} catch (TeaException teaException) {
// Obtain the complete error message.
System.out.println(com.aliyun.teautil.Common.toJSONString(teaException));
// Obtain a specific field.
System.out.println(teaException.getCode());
}
}
}# -*- coding: utf-8 -*-
# Import the dependency package.
# pip install alibabacloud_videoenhan20200320
import os
import io
from urllib.request import urlopen
from alibabacloud_tea_openapi.models import Config
from alibabacloud_tea_util.models import RuntimeOptions
from alibabacloud_videoenhan20200320.client import Client
from alibabacloud_videoenhan20200320.models import MergeVideoFaceAdvanceRequest
config = Config(
# For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
# If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
# Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
access_key_id=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID'),
access_key_secret=os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET'),
# The domain name of the service.
endpoint='videoenhan.cn-shanghai.aliyuncs.com',
# The region that corresponds to the domain name.
region_id='cn-shanghai'
)
# Scenario 1: The file is stored locally.
#imgVideoURL = open(r'/tmp/MergeVideoFace1.mp4', 'rb')
#imgReferenceURL = open(r'/tmp/MergeVideoFace-pic1.png', 'rb')
# Scenario 2: Use an accessible URL.
urlVideoURL = 'http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4'
imgVideoURL = io.BytesIO(urlopen(urlVideoURL).read())
urlReferenceURL = 'http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png'
imgReferenceURL = io.BytesIO(urlopen(urlReferenceURL).read())
merge_video_face_request = MergeVideoFaceAdvanceRequest()
merge_video_face_request.video_urlobject = imgVideoURL
merge_video_face_request.reference_urlobject = imgReferenceURL
runtime = RuntimeOptions()
try:
# Initialize the client.
client = Client(config)
response = client.merge_video_face_advance(merge_video_face_request, runtime)
# Obtain the entire response.
print(response.body)
except Exception as error:
# Obtain the complete error message.
print(error)
# Obtain a specific field.
print(error.code)<?php
// Install the dependency package.
//composer require alibabacloud/videoenhan-20200320
use AlibabaCloud\SDK\Videoenhan\V20200320\Videoenhan;
use \Exception;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Videoenhan\V20200320\Models\MergeVideoFaceAdvanceRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use GuzzleHttp\Psr7\Stream;
class MergeVideoFaceAdvance {
/**
* Initialize the client using an AccessKey pair.
* @param string $accessKeyId
* @param string $accessKeySecret
* @return Videoenhan Client
*/
public static function createClient($accessKeyId, $accessKeySecret){
// Initialize the configuration object Darabonba\OpenApi\Models\Config.
// This object stores configurations such as your accessKeyId, accessKeySecret, and endpoint.
$config = new Config([
"accessKeyId" => $accessKeyId,
"accessKeySecret" => $accessKeySecret
]);
// The domain name of the service.
$config->endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
return new Videoenhan($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKeySecret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
$client = self::createClient($accessKeyId, $accessKeySecret);
// Scenario 1: Use a local file.
$file0 = fopen('/tmp/MergeVideoFace1.mp4', 'rb');
$stream0 = new Stream($file0);
// Scenario 2: Use an accessible URL.
$file1 = fopen('http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png', 'rb');
$stream1 = new Stream($file1);
$mergeVideoFaceAdvanceRequest = new MergeVideoFaceAdvanceRequest([
"videoURLObject" => $stream0,
"referenceURLObject" => $stream1
]);
$runtime = new RuntimeOptions([]);
try {
$resp = $client->mergeVideoFaceAdvance($mergeVideoFaceAdvanceRequest, $runtime);
# Obtain the entire response.
echo Utils::toJSONString($resp->body);
} catch (Exception $exception) {
# Obtain the complete error message.
echo Utils::toJSONString($exception);
# Obtain a specific field.
echo $exception->getCode();
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
// The $argv parameter is reserved and does not require modification.
MergeVideoFaceAdvance::main(array_slice($argv, 1));// Install the dependency package.
// npm install @alicloud/videoenhan20200320
const VideoenhanClient = require('@alicloud/videoenhan20200320');
const OpenapiClient = require('@alicloud/openapi-client');
const TeaUtil = require('@alicloud/tea-util');
const fs = require('fs');
const http = require('http');
const https = require('https');
let config = new OpenapiClient.Config({
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
accessKeyId: process.env.ALIBABA_CLOUD_ACCESS_KEY_ID,
accessKeySecret: process.env.ALIBABA_CLOUD_ACCESS_KEY_SECRET
});
// The domain name of the service.
config.endpoint = `videoenhan.cn-shanghai.aliyuncs.com`;
const client = new VideoenhanClient.default(config);
const getResponse = function (httpClient, url) {
return new Promise((resolve, reject) => {
httpClient.get(url, function (response) {
resolve(response);
})
})
}
const request = async function () {
try {
let mergeVideoFaceAdvanceRequest = new VideoenhanClient.MergeVideoFaceAdvanceRequest();
// Scenario 1: Use a local file.
// const fileStream1 = fs.createReadStream('/tmp/MergeVideoFace1.mp4');
// const fileStream2 = fs.createReadStream('/tmp/MergeVideoFace-pic1.png');
// mergeVideoFaceAdvanceRequest.videoURLObject = fileStream1;
// mergeVideoFaceAdvanceRequest.referenceURLObject = fileStream2;
// Scenario 2: Use an accessible URL.
const url1 = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4");
const httpClient1 = (url1.protocol == "https:") ? https : http;
const url2 = new URL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png");
const httpClient2 = (url2.protocol == "https:") ? https : http;
mergeVideoFaceAdvanceRequest.videoURLObject = await getResponse(httpClient1, url1);
mergeVideoFaceAdvanceRequest.referenceURLObject = await getResponse(httpClient2, url2);
let runtime = new TeaUtil.RuntimeOptions({ });
client.mergeVideoFaceAdvance(mergeVideoFaceAdvanceRequest, runtime)
.then(function(mergeVideoFaceResponse) {
// Obtain the entire response.
console.log(mergeVideoFaceResponse);
// Obtain a specific field.
console.log(mergeVideoFaceResponse.body.data);
}, function(error) {
// Obtain the complete error message.
console.log(error);
// Obtain a specific field.
console.log(error.data.Code);
})
} catch (error) {
console.log(error);
}
}();/**
This code depends on github.com/alibabacloud-go/videoenhan-20200320/v3.
We recommend that you run the `go mod tidy` command to install the dependency.
*/
package main
import (
"fmt"
"os"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
videoenhan20200320 "github.com/alibabacloud-go/videoenhan-20200320/v3/client"
"net/http"
)
func main() {
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
accessKeyId := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")
accessKeySecret := os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
// Initialize the configuration object &openapi.Config. This object stores configurations such as your AccessKeyId, AccessKeySecret, and Endpoint.
config := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret)
}
// The domain name of the service.
config.Endpoint = tea.String("videoenhan.cn-shanghai.aliyuncs.com")
client, err := videoenhan20200320.NewClient(config)
if err != nil {
panic(err)
}
// Scenario 1: Use a local file.
//file0, err := os.Open("/tmp/MergeVideoFace.mp4")
//if err != nil {
// fmt.Println("can not open file", err)
// panic(err)
//}
//file1, err := os.Open("/tmp/MergeVideoFace-pic.png")
//if err != nil {
// fmt.Println("can not open file", err)
// panic(err)
//}
//mergeVideoFaceAdvanceRequest := &videoenhan20200320.MergeVideoFaceAdvanceRequest{
// ReferenceURLObject: file0,
// VideoURLObject: file1,
//}
// Scenario 2: Use an accessible URL.
httpClient := http.Client{}
file0, _ := httpClient.Get("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png")
file1, _ := httpClient.Get("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4")
mergeVideoFaceAdvanceRequest := &videoenhan20200320.MergeVideoFaceAdvanceRequest{
ReferenceURLObject: file0.Body,
VideoURLObject: file1.Body,
}
runtime := &util.RuntimeOptions{}
mergeVideoFaceAdvanceResponse, err := client.MergeVideoFaceAdvance(mergeVideoFaceAdvanceRequest, runtime)
if err != nil {
// Obtain the complete error message.
fmt.Println(err.Error())
} else {
// Obtain the entire response.
fmt.Println(mergeVideoFaceAdvanceResponse)
}
}using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using AlibabaCloud.SDK.Videoenhan20200320.Models;
using Tea;
namespace AlibabaCloud.SDK.Sample
{
public class Sample
{
/**
* Initialize the client using an AccessKey pair.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static AlibabaCloud.SDK.Videoenhan20200320.Client CreateClient(string accessKeyId, string accessKeySecret)
{
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
{
AccessKeyId = accessKeyId,
AccessKeySecret = accessKeySecret,
};
// The domain name of the service.
config.Endpoint = "videoenhan.cn-shanghai.aliyuncs.com";
return new AlibabaCloud.SDK.Videoenhan20200320.Client(config);
}
public static void Main(string[] args)
{
// For information about how to create an AccessKey ID and an AccessKey Secret, see https://help.aliyun.com/document_detail/175144.html.
// If you use the AccessKey pair of a RAM user, you must grant the AliyunVIAPIFullAccess permission to the RAM user. For more information, see https://help.aliyun.com/document_detail/145025.html.
// Read the AccessKey ID and AccessKey Secret from environment variables. Before you run the sample code, make sure that you have configured the environment variables.
AlibabaCloud.SDK.Videoenhan20200320.Client client = CreateClient(System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
MergeVideoFaceAdvanceRequest mergeVideoFaceAdvanceRequest = new MergeVideoFaceAdvanceRequest();
// Scenario 1: Use a local file.
// System.IO.FileStream file1 = new System.IO.FileStream(@"/tmp/MergeVideoFace-pic1.png", System.IO.FileMode.Open);
// System.IO.FileStream file2 = new System.IO.FileStream(@"/tmp/MergeVideoFace1.mp4", System.IO.FileMode.Open);
// mergeVideoFaceAdvanceRequest.ReferenceURLObject = file1;
// mergeVideoFaceAdvanceRequest.VideoURLObject = file2;
// Scenario 2: Use an accessible URL.
HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace-pic1.png");
WebResponse response1 = request1.GetResponse();
Stream stream1 = response1.GetResponseStream();
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create("http://viapi-test.oss-cn-shanghai.aliyuncs.com/viapi-3.0domepic/videoenhan/MergeVideoFace/MergeVideoFace1.mp4");
WebResponse response2 = request2.GetResponse();
Stream stream2 = response2.GetResponseStream();
mergeVideoFaceAdvanceRequest.ReferenceURLObject = stream1;
mergeVideoFaceAdvanceRequest.VideoURLObject = stream2;
AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
try
{
MergeVideoFaceResponse mergeVideoFaceResponse = client.MergeVideoFaceAdvance(mergeVideoFaceAdvanceRequest, runtime);
// Obtain the entire response.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(mergeVideoFaceResponse.Body));
// Obtain a specific field.
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(mergeVideoFaceResponse.Body.Data));
}
catch (TeaException error)
{
// If an error occurs, print the error message.
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
// If an error occurs, print the error message.
Console.WriteLine(error.Message);
}
}
}
}FAQ
Why does the output video look the same as the original video (fusion effect not applied)?
When the face in the video occupies a small portion of the frame, the fusion effect may be subtle or the output may appear identical to the original. Replace the source video with one where the face is larger and more clearly visible, then retest.
Which reference image should I use for the ReferenceURL parameter — the original photo or a generated digital human image?
You can upload whichever reference image you are most satisfied with. Both the original photo and a generated digital human image are acceptable as reference images. The maximum number of reference images is 5.
The watermark generated by the API is too small to meet compliance review requirements. What should I do?
The API does not support adjusting the watermark size. You can set the AddWatermark request parameter to false to remove the default system watermark, then add a compliant identifier or custom watermark during post-processing.