After Huawei Video Editing Service version 6.2.0 was launched, we have brought two major changes to you: rich and diverse AI capabilities and flexible options for integration. In order to let developers get started faster, today Xiaobian brings the specific integration method of the video editing atomic power SDK. Come and try it!

1 Development preparation

For detailed preparation steps, please refer to the official website of Huawei Developer Alliance:

https://developer.huawei.com/consumer/cn/doc/development/Media-Guides/config-agc-0000001101108580?ha_source=hms1

2 Code development

1.1 Edit the project

1.1.1 Set the authentication information of the application

Application authentication information can be set through api_key or Access Token.

  • Use the setAccessToken method to set the Access Token, which can be initialized once when the application starts, and does not need to be set multiple times.
 MediaApplication.getInstance().setAccessToken("your access token");
  • Use the setApiKey method to set api_key, which also does not need to be set multiple times.

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

1.1.2 Set the unique identification ID, namely License ID .

The license ID is a valid certificate for management and control. The developer must ensure the uniqueness of the license ID.

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

1.1.3 Initialize the Editor runtime environment

To create an editing project, first create an Editor object and initialize the runtime environment. When leaving the editing project, the Editor instance should be released.

(1) Create Editor object

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

(2) Specify the layout position of the preview window

The preview window is responsible for the rendering of video images, which is realized by creating SurfaceView inside the video editing atomic capability SDK. Before creating a window, the developer needs to specify the layout position of the preview window in the application.

 <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" />
// 指定预览窗口 
LinearLayout mSdkPreviewContainer = view.findViewById(R.id.video_content_layout);

// 设置预览窗口承载的布局 
editor.setDisplay(mSdkPreviewContainer);

(3) Initialize the running environment. If the license authentication fails, a LicenseException will be thrown.

Creating an Editor object does not occupy actual system resources. Developers need to manually select the timing of environment initialization. At this time, the video editing atomic capability SDK will create necessary threads and timers.

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

1.1.4 Adding videos and pictures

Create a video swimlane and add pictures or video clips on the swimlane through the file path.

 // 获取时间线对象 
HVETimeLine timeline = editor.getTimeLine();

// 创建视频泳道 
HVEVideoLane videoLane = timeline.appendVideoLane();

// 在视频泳道的末尾,添加视频资源 
HVEVideoAsset videoAsset = vidoeLane.appendVideoAsset("test.mp4");

// 在视频泳道的末尾,添加图片资源 
HVEImageAsset imageAsset = vidoeLane.appendImageAsset("test.jpg");

1.1.5 Add Music

Create a music swimlane and add music material on the swimlane through the file path.

 // 创建音乐泳道 

HVEAudioLane audioLane = timeline.appendAudioLane();

// 在音频泳道的末尾,创建音乐资源 

HVEAudioAsset audioAsset = audioLane.appendAudioAsset("test.mp3");

1.1.6 Adding stickers and text

 创建一条贴纸文字泳道,通过文件路径在泳道上添加贴纸和文字。其中文字需要指定文本内容。
// 创建贴纸文字泳道 
HVEStickerLane stickerLane = timeline.appendStickerLane();

// 在泳道的末尾添加贴纸 
HVEStickerAsset stickerAsset = stickerLane.appendStickerAsset("test.png");

// 在泳道的末尾添加文字 
HVEWordAsset wordAsset = stickerLane.appendWord("输入文字",0,3000);

1.1.7 Add special effects

There are two types of effects: external effects and inline effects.

Outreach effects. Added in the special effect swimlane, the time length can be adjusted arbitrarily across multiple resources.

 // 创建特效泳道 

HVEEffectLane effectLane = timeline.appendEffectLane();

// 创建一个颜色调节特效,添加到0位置,时长为3000ms

HVEEffect effect = effectLane.appendEffect(new HVEEffect.Options(HVEEffect.EFFECT_COLORADJUST, "", ""), 0, 3000);
  • Embedded effects. Adding in resources can only act on a single resource, and the length of time cannot be adjusted independently.
 // 创建一个颜色调节内嵌特效 

HVEEffect effect = videoAsset.appendEffectUniqueOfType(new HVEEffect.Options(HVEEffect.EFFECT_COLORADJUST, "", ""), ADJUST);

1.1.8 Play Timeline

The playback timeline needs to specify the start and end points. After that, the timeline will advance backward at a fixed frame rate, and the preview screen and sound will be played in sync. Through the registered playback callback, you can receive playback progress, pause, playback completion and playback failure events.

 // 注册播放进度回调 
editor.setPlayCallback(callback);

// 播放完整时间线 
editor.playTimeLine(timeline.getStartTime(), timeline.getEndTime());

1.1.9 Export

After editing, generate a new video from the resources on the timeline through the export interface. Then set the export callback, you can monitor the export progress, completion, and failure events, and specify the frame rate, resolution, and generation path of the exported video.

 // 导出视频路径 
String outputPath = 
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)                    
                    + File.separator + Constant.LOCAL_VIDEO_SAVE_PATH                    
                    + File.separator + VideoExportActivity.getTime() + ".mp4";    
 
// 导出分辨率 
HVEVideoProperty videoProperty = new HVEVideoProperty(1920, 1080);

// 导出视频 
HVEExportManager.exportVideo(targetEditor, callback, videoProperty, outputPath);

1.2 Project draft

Through the HVEProjectManager manager, you can query the local draft list, and copy, delete, and rename the drafts.

1.2.1 Save draft

 // 将editor保存到本地 
editor.saveProject();

1.2.2 Restoring drafts

 // 通过草稿ID,创建Editor实例 
HuaweiVideoEditor editor = HuaweiVideoEditor.create(getApplicationContext(), projectId);

1.3 Material Management

After AGC arranges the material column, use the interface provided by the material management module to query and download the specified material. For specific steps, please refer to:
https://developer.huawei.com/consumer/cn/doc/development/Media-Guides/material_management-0000001166392852?ha_source=hms1

1.4 AI Algorithm Integration

The Video Editing Atomic Ability SDK provides multiple AI algorithms such as exclusive filters, character tracking, portrait resurrection, and AI coloring. Developers can freely choose to integrate. For details on the integration steps of each AI capability, please refer to:
https://developer.huawei.com/consumer/cn/doc/development/Media-Guides/ai_algorithm_integration-0000001166552824?ha_source=hms1

1.4.1 Exclusive Filters

Support user-defined filters, apply custom filter effects to input videos and images, and support diverse image beautification needs of scenes.

Exclusive filter display.gif

 // 创建专属滤镜算法引擎
HVEExclusiveFilter filterEngine = new HVEExclusiveFilter();

// 初始化专属滤镜算法引擎
mFilterEngine.initExclusiveFilterEngine(new HVEAIInitialCallback() {
        @Override
        public void onProgress(int progress) {
        // 初始化进度回调
        }

        @Override
        public void onSuccess() {
            // 初始化成功
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            // 初始化失败
        }
    });

// 创建单图滤镜,指定一个Bitmap和滤镜的名字
// 返回滤镜ID,通过此ID,可以在数据库中查询滤镜相关的所有信息
String effectId = mFilterEngine.createExclusiveEffect(bitmap, "自定义滤镜01");

// 将滤镜添加到特效泳道中,起始位置0, 时长3000ms
effectLane.appendEffect(new HVEEffect.Options(
    HVEEffect.CUSTOM_FILTER + mSelectName, effectId, ""), 0, 3000);

1.4.2 One-click hair coloring

Input a photo of a single person or a group of people, detect the person and realize one-click hair dyeing based on the reference color card style, and the dyeing degree can be adjusted by pulling.

One-click hair coloring.gif

 // 一键染发AI算法初始化
asset.initHairDyeingEngine(new HVEAIInitialCallback() {
        @Override
        public void onProgress(int progress) {
        // 初始化进度
        }

        @Override
        public void onSuccess() {
        // 初始化成功
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
        // 初始化失败
    }
   });

// 添加一键染发特效,指定色卡和默认强度。
asset.addHairDyeingEffect(new HVEAIProcessCallback() {
        @Override
        public void onProgress(int progress) {
            // 一键染发处理进度。
        }

        @Override
        public void onSuccess() {
            // 一键染发处理成功
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
            // 一键染发处理失败
        }
    }, colorPath, defaultStrength);

// 移除一键染发特效
asset.removeHairDyeingEffect();

1.4.3 Dynamic Photos

Enter a single or multi-person photo, and drive the characters in the photo to smile, nod, etc., to achieve dynamic photo effects.

dynamic photo.gif

 // 添加动态照片特效
asset.addFaceReenactAIEffect(new HVEAIProcessCallback() {
        @Override
        public void onProgress(int progress) {
        // 动态照片处理进度
        }

        @Override
        public void onSuccess() {
        // 动态照片处理成功
        }

        @Override
        public void onError(int errorCode, String errorMessage) {
        // 动态照片处理失败
        }
    });

// 移除动态照片特效
asset.removeFaceReenactAIEffect();

Learn more details>>

Visit the official website of Huawei Developer Alliance
Get development guidance documents
Huawei Mobile Services Open Source Warehouse Address: GitHub , Gitee

Follow us to know the latest technical information of HMS Core for the first time~


HarmonyOS_SDK
596 声望11.7k 粉丝

HarmonyOS SDK通过将HarmonyOS系统级能力对外开放,支撑开发者高效打造更纯净、更智能、更精致、更易用的鸿蒙原生应用,和开发者共同成长。