[Huawei Developers] Mike-mei:
The launch of HMS Core Video Editor Kit 6.2.0 has brought two notable highlights: various AI-empowered capabilities and flexible integration methods. One method is to integrate the fundamental capability SDK, which is described below.

Preparations​

For details, please check the official document.

Code Development​

Configuring a Video Editing Project

1. Set the authentication information for your app through an API key or access token.

Use the setAccessToken method to set an access token when the app is started. The access token needs to be set only once.

MediaApplication.getInstance().setAccessToken("your access token");

Use the setApiKey method to set an API key when the app is started. The API key needs to be set only once.

MediaApplication.getInstance().setApiKey("your ApiKey");

2. Set a License ID.

This ID is used to manage your usage quotas, so ensure that the ID is unique.

MediaApplication.getInstance().setLicenseId("License ID");

3. Initialize the running environment for HuaweiVideoEditor.

When creating a video editing project, first create a HuaweiVideoEditor object and initialize its running environment. When exiting a video editing project, release the HuaweiVideoEditor object.

Create a HuaweiVideoEditor object.

HuaweiVideoEditor editor = HuaweiVideoEditor.create(getApplicationContext());

Specify the position for the preview area.

This area renders video images, which is implemented by creating SurfaceView in the fundamental capability SDK. Ensure that the preview area position on your app is specified before creating this area.

<LinearLayout
android:id="@+id/video_content_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/video_edit_main_bg_color"
android:gravity="center"
android:orientation="vertical" />
// Specify the preview area position.
LinearLayout mSdkPreviewContainer = view.findViewById(R.id.video_content_layout);

// Set the layout of the preview area.
editor.setDisplay(mSdkPreviewContainer);

Initialize the running environment. If the license verification fails, LicenseException will be thrown.

After the HuaweiVideoEditor object is created, it has not occupied any system resource. You need to manually set the time for initializing the running environment of the object. Then, necessary threads and timers will be created in the fundamental capability SDK.

try {
editor.initEnvironment();
} catch (LicenseException error) {
SmartLog.e(TAG, "initEnvironment failed: " + error.getErrorMsg());
finish();
return;
}

4. Add a video or image.

Create a video lane and add a video or image to the lane using the file path.

// Obtain the HVETimeLine object.
HVETimeLine timeline = editor.getTimeLine();

// Create a video lane.
HVEVideoLane videoLane = timeline.appendVideoLane();

// Add a video to the end of the video lane.
HVEVideoAsset videoAsset = videoLane.appendVideoAsset("test.mp4");

// Add an image to the end of the video lane.
HVEImageAsset imageAsset = videoLane.appendImageAsset("test.jpg");

5. Add audio.

Create an audio lane and add audio to the lane using the file path.

// Create an audio lane.
HVEAudioLane audioLane = timeline.appendAudioLane();

// Add an audio asset to the end of the audio lane.
HVEAudioAsset audioAsset = audioLane.appendAudioAsset("test.mp3");

6. Add a sticker and text.

Create a sticker lane and add a sticker and text to the lane. A sticker needs to be added using its file path, while the text needs to be added by specifying its content.

// Create a sticker lane.
HVEStickerLane stickerLane = timeline.appendStickerLane();

// Add a sticker to the end of the lane.
HVEStickerAsset stickerAsset = stickerLane.appendStickerAsset("test.png");

// Add text to the end of the lane.
HVEWordAsset wordAsset = stickerLane.appendWord("Input text",0,3000);

7. Add a special effect.

Special effects are classified into the external special effect and embedded special effect.

Add an external special effect to an effect lane. This special effect can be applied to multiple assets, and its duration can be adjusted.

// Create an effect lane.
HVEEffectLane effectLane = timeline.appendEffectLane();

// Create a color adjustment effect with a duration of 3000 ms. Add it to the 0 ms playback position of the lane.
HVEEffect effect = effectLane.appendEffect(new HVEEffect.Options(HVEEffect.EFFECT_COLORADJUST, "", ""), 0, 3000);

Add an embedded special effect to an asset. Such a special effect can be applied to a single asset. The special effect's duration is the same as that of the asset and cannot be adjusted.

// Create an embedded special effect of color adjustment.
HVEEffect effect = videoAsset.appendEffectUniqueOfType(new HVEEffect.Options(HVEEffect.EFFECT_COLORADJUST, "", ""), ADJUST);

8. Play a timeline.

To play a timeline, specify its start time and end time. The timeline will play from its start time to end time at a fixed frame rate, and the image and sound in the preview will play simultaneously. You can obtain the playback progress, playback pause, payback completion, and playback failure via the registered callback.

// Register the playback progress callback.
editor.setPlayCallback(callback);

// Play the complete timeline.
editor.playTimeLine(timeline.getStartTime(), timeline.getEndTime());

9. Export a video.

After the editing is complete, export a new video using the assets in the timeline via the export API. Set the export callback to listen to the export progress, export completion, or export failure, and specify the frame rate, resolution, and path for the video to be exported.

// Path for the video to be exported.
String outputPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + Constant.LOCAL_VIDEO_SAVE_PATH
+ File.separator + VideoExportActivity.getTime() + ".mp4";

// Resolution for the video to be exported.
HVEVideoProperty videoProperty = new HVEVideoProperty(1920, 1080);

// Export the video.
HVEExportManager.exportVideo(targetEditor, callback, videoProperty, outputPath);

Managing Materials

After allocating materials, use APIs provided in the on-cloud material management module to query and download a specified material. For details, please refer to the description in the official document.

Integrating an AI-Empowered Capability

The fundamental capability SDK of Video Editor Kit provides multiple AI-empowered capabilities including AI filter, track person, moving picture, and AI color, for integration into your app. For more details, please refer to the instruction in this document.

AI Filter

Lets users flexibly customize and apply a filter to their imported videos and images.

// Create an AI algorithm engine for AI filter.
HVEExclusiveFilter filterEngine = new HVEExclusiveFilter();

// Initialize the engine.
mFilterEngine.initExclusiveFilterEngine(new HVEAIInitialCallback() {
@Override
public void onProgress(int progress) {
// Initialization progress.
}

@Override
public void onSuccess() {
// The initialization is successful.
}

@Override
public void onError(int errorCode, String errorMessage) {
// The initialization failed.
}
});

// Create an AI filter of the extract type from an image, by specifying the image bitmap and filter name.
// The filter ID is returned. Using the ID, you can query all information about the filter in the database.
String effectId = mFilterEngine.createExclusiveEffect(bitmap, "AI filter 01");

// Add the filter for the first 3000 ms segment of the effect lane.
effectLane.appendEffect(new HVEEffect.Options(
HVEEffect.CUSTOM_FILTER + mSelectName, effectId, ""), 0, 3000);

Color Hair

Changes the hair color of one or more persons detected in the imported image, in just a tap. The color strength is adjustable.

// Initialize the AI algorithm for the color hair effect.
asset.initHairDyeingEngine(new HVEAIInitialCallback() {
@Override
public void onProgress(int progress) {
// Initialization progress.
}

@Override
public void onSuccess() {
// The initialization is successful.
}

@Override
public void onError(int errorCode, String errorMessage) {
// The initialization failed.
}
});

// Add the color hair effect by specifying a color and the default strength.
asset.addHairDyeingEffect(new HVEAIProcessCallback() {
@Override
public void onProgress(int progress) {
// Handling progress.
}

@Override
public void onSuccess() {
// The handling is successful.
}

@Override
public void onError(int errorCode, String errorMessage) {
// The handling failed.
}
}, colorPath, defaultStrength);

// Remove the color hair effect.
asset.removeHairDyeingEffect();

Moving Picture

Animates one or more persons in the imported image, so that they smile, nod, or more.

// Add the moving picture effect.
asset.addFaceReenactAIEffect(new HVEAIProcessCallback() {
@Override
public void onProgress(int progress) {
// Handling progress.
}

@Override
public void onSuccess() {
// The handling is successful.
}

@Override
public void onError(int errorCode, String errorMessage) {
// The handling failed.
}
});

// Remove the moving picture effect.
asset.removeFaceReenactAIEffect();

This article presents just a few features of Video Editor Kit. For more, check here.

To learn more, please visit:

>> HUAWEI Developers official website

>> Development Guide

>> Reddit to join developer discussions

>> GitHub to download the sample code

>> Stack Overflow to solve integration problems

Follow our official account for the latest HMS Core-related news and updates.