无人直播领域,虚拟主播的需求持续上升,如何迅速实现虚拟主播的直播功能,已成为众人关注的焦点。接下来,我们将详细阐述其开发步骤。
必备能力解析
GPT4.0虚拟人直播具备两项基本功能。首先,它通过GPT4.0接口实现,目前openAI提供的是GPT3.5接口,可以直接使用。其次,虚拟人具备交互功能,bing在新版Edge浏览器中加入了GPT4.0体验,同时也有开源库进行二次封装,只需提供Cookie即可体验。在Edge浏览器中,要获取Cookie,请按F12键进入开发者工具,切换到应用程序标签页,然后在左侧的Cookie列表中找到并查看_U的值。
npm i @waylaidwanderer/chatgpt-api
import { BingAIClient } from '@waylaidwanderer/chatgpt-api';
export class BingGPT {
/*
* http_proxy, apiKey
**/
constructor(http_proxy, userCookie) {
this.api = this.init(http_proxy, userCookie);
}
init(http_proxy, userCookie) {
console.log(http_proxy, userCookie)
const options = {
// Necessary for some people in different countries, e.g. China (https://cn.bing.com)
host: 'https://www.bing.com',
// "_U" cookie from bing.com
userToken: userCookie,
// If the above doesn't work, provide all your cookies as a string instead
cookies: '',
// A proxy string like "http://:"
proxy: http_proxy,
// (Optional) Set to true to enable `console.debug()` logging
debug: false,
};
return new BingAIClient(options);
}
//调用chatpgt
chat(text, cb) {
var res=""
var that = this;
console.log("正在向bing发送提问", text )
this.api.sendMessage(text, {
toneStyle: 'balanced',
onProgress: (token) => {
if(token.length==2 && token.charCodeAt(0)==55357&&token.charCodeAt(1)==56842){
cb(true, res);
}
res+=token;
}
});
}
}
接口选择考量
GPT4.0依赖浏览器,其非官方API不够稳定。因此,我们选择了即构Avatar。这个软件内置了文本驱动功能,能够将GPT的回复文字朗读出来,并且具备口型匹配功能。通过简单几行代码,我们就能定制虚拟人的“皮肤”,使用起来非常便捷。不过,使用即构Avatar需要进行权限认证和引擎初始化等步骤。文末的代码附件可供参考。
体验功能拓展
npm install chatgpt
npm install https-proxy-agent node-fetch
官方提供的演示版本源代码仅包含基础资源,而构Avatar在捏脸功能上同样出色。有兴趣的朋友可以访问官方网站下载并体验app,亲自体验它的强大之处。该软件能够为虚拟人物塑造出丰富多彩的形象,满足各种直播场合的需求。
import { ChatGPTAPI } from "chatgpt";
import proxy from "https-proxy-agent";
import nodeFetch from "node-fetch";
export class ChatGPT {
constructor(http_proxy, apiKey) {
this.api = this.init(http_proxy, apiKey);
this.conversationId = null;
this.ParentMessageId = null;
}
init(http_proxy, apiKey) {
console.log(http_proxy, apiKey)
return new ChatGPTAPI({
apiKey: apiKey,
fetch: (url, options = {}) => {
const defaultOptions = {
agent: proxy(http_proxy),
};
const mergedOptions = {
...defaultOptions,
...options,
};
return nodeFetch(url, mergedOptions);
},
});
}
//调用chatpgt
chat(text, cb) {
let that = this
console.log("正在向ChatGPT发送提问:", text)
that.api.sendMessage(text, {
conversationId: that.ConversationId,
parentMessageId: that.ParentMessageId
}).then(
function (res) {
that.ConversationId = res.conversationId
that.ParentMessageId = res.id
cb && cb(true, res.text)
}
).catch(function (err) {
console.log(err)
cb && cb(false, err);
});
}
}
平台推流手段
在B站、抖音、快手等第三方平台上进行虚拟直播时,需要使用官方提供的推流工具,比如直播伴侣。但是,官方并未提供获取直播间评论或弹幕的实时接口,若要实现这一功能,需要借助一些技术方法,这里就不详细说明了。
自建平台思路
private void setCharacter(User user) {
String sex = ZegoCharacterHelper.MODEL_ID_MALE;
if (!user.isMan) sex = ZegoCharacterHelper.MODEL_ID_FEMALE;
// 创建 helper 简化调用
// base.bundle 是头模, human.bundle 是全身人模
mCharacterHelper = new ZegoCharacterHelper(FileUtils.getPhonePath(mApp, "human.bundle", "assets"));
mCharacterHelper.setExtendPackagePath(FileUtils.getPhonePath(mApp, "Packages", "assets"));
// 设置形象配置
mCharacterHelper.setDefaultAvatar(sex);
// 角色上屏, 必须在 UI 线程, 必须设置过avatar形象后才可调用(用 setDefaultAvatar 或者 setAvatarJson 都可以)
mCharacterHelper.setCharacterView(user.avatarView, () -> {
});
mCharacterHelper.setViewport(ZegoAvatarViewState.half);
//设置头发、衣服等
mCharacterHelper.setPackage("ZEGO_Girl_Hair_0001");
mCharacterHelper.setPackage("ZEGO_Girl_Tshirt_0001_0002");
mCharacterHelper.setPackage("facepaint5");
mCharacterHelper.setPackage("irises2");
initTextApi();
updateUser(user);
}
不依赖第三方平台,自行构建直播平台同样可行。通过即构的实时音视频技术RTC,主播可以实时传输手机画面,观众也能同步接收图像。此前,基于抖音平台开发的虚拟人直播演示,B站、快手等平台采用的原理相似。虚拟人通过用户端的固定算法进行渲染,与主播现场拍摄的画面存在一定差异。
public void playText(String text) {
if (mTextApi == null) return;
mTextApi.playTextExpression(text);
}
成本优势方案
即构ZIM具备全面的即时通讯功能,涵盖一对一聊天、多人群聊以及房间内交流,可根据需求灵活搭配。在虚拟直播场景中,文字信息实时传输至各终端,再本地生成画面,这种方式成本最低。直播间可利用房间管理功能,而在即构IM的房间中,用户可以通过弹幕功能发送和接收文字信息。
看完这段,大家对虚拟人直播的制作步骤是否已经明了?接下来,你打算将这些建议用于哪些无人直播的场合?欢迎在评论区留言交流!别忘了点赞和转发这篇文章。