無(wú)人直播領(lǐng)域,虛擬主播的需求持續(xù)上升,如何迅速實(shí)現(xiàn)虛擬主播的直播功能,已成為眾人關(guān)注的焦點(diǎn)。接下來(lái),我們將詳細(xì)闡述其開(kāi)發(fā)步驟。
必備能力解析
GPT4.0虛擬人直播具備兩項(xiàng)基本功能。首先,它通過(guò)GPT4.0接口實(shí)現(xiàn),目前openAI提供的是GPT3.5接口,可以直接使用。其次,虛擬人具備交互功能,bing在新版Edge瀏覽器中加入了GPT4.0體驗(yàn),同時(shí)也有開(kāi)源庫(kù)進(jìn)行二次封裝,只需提供Cookie即可體驗(yàn)。在Edge瀏覽器中,要獲取Cookie,請(qǐng)按F12鍵進(jìn)入開(kāi)發(fā)者工具,切換到應(yīng)用程序標(biāo)簽頁(yè),然后在左側(cè)的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);
}
//調(diào)用chatpgt
chat(text, cb) {
var res=""
var that = this;
console.log("正在向bing發(fā)送提問(wèn)", 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依賴(lài)瀏覽器,其非官方API不夠穩(wěn)定。因此,我們選擇了即構(gòu)Avatar。這個(gè)軟件內(nèi)置了文本驅(qū)動(dòng)功能,能夠?qū)PT的回復(fù)文字朗讀出來(lái),并且具備口型匹配功能。通過(guò)簡(jiǎn)單幾行代碼,我們就能定制虛擬人的“皮膚”,使用起來(lái)非常便捷。不過(guò),使用即構(gòu)Avatar需要進(jìn)行權(quán)限認(rèn)證和引擎初始化等步驟。文末的代碼附件可供參考。
體驗(yàn)功能拓展
npm install chatgpt
npm install https-proxy-agent node-fetch
官方提供的演示版本源代碼僅包含基礎(chǔ)資源,而構(gòu)Avatar在捏臉功能上同樣出色。有興趣的朋友可以訪問(wèn)官方網(wǎng)站下載并體驗(yàn)app,親自體驗(yàn)它的強(qiáng)大之處。該軟件能夠?yàn)樘摂M人物塑造出豐富多彩的形象,滿足各種直播場(chǎng)合的需求。
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);
},
});
}
//調(diào)用chatpgt
chat(text, cb) {
let that = this
console.log("正在向ChatGPT發(fā)送提問(wèn):", 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);
});
}
}
平臺(tái)推流手段
在B站、抖音、快手等第三方平臺(tái)上進(jìn)行虛擬直播時(shí),需要使用官方提供的推流工具,比如直播伴侶。但是,官方并未提供獲取直播間評(píng)論或彈幕的實(shí)時(shí)接口,若要實(shí)現(xiàn)這一功能,需要借助一些技術(shù)方法,這里就不詳細(xì)說(shuō)明了。
自建平臺(tái)思路
private void setCharacter(User user) {
String sex = ZegoCharacterHelper.MODEL_ID_MALE;
if (!user.isMan) sex = ZegoCharacterHelper.MODEL_ID_FEMALE;
// 創(chuàng)建 helper 簡(jiǎn)化調(diào)用
// base.bundle 是頭模, human.bundle 是全身人模
mCharacterHelper = new ZegoCharacterHelper(FileUtils.getPhonePath(mApp, "human.bundle", "assets"));
mCharacterHelper.setExtendPackagePath(FileUtils.getPhonePath(mApp, "Packages", "assets"));
// 設(shè)置形象配置
mCharacterHelper.setDefaultAvatar(sex);
// 角色上屏, 必須在 UI 線程, 必須設(shè)置過(guò)avatar形象后才可調(diào)用(用 setDefaultAvatar 或者 setAvatarJson 都可以)
mCharacterHelper.setCharacterView(user.avatarView, () -> {
});
mCharacterHelper.setViewport(ZegoAvatarViewState.half);
//設(shè)置頭發(fā)、衣服等
mCharacterHelper.setPackage("ZEGO_Girl_Hair_0001");
mCharacterHelper.setPackage("ZEGO_Girl_Tshirt_0001_0002");
mCharacterHelper.setPackage("facepaint5");
mCharacterHelper.setPackage("irises2");
initTextApi();
updateUser(user);
}
不依賴(lài)第三方平臺(tái),自行構(gòu)建直播平臺(tái)同樣可行。通過(guò)即構(gòu)的實(shí)時(shí)音視頻技術(shù)RTC,主播可以實(shí)時(shí)傳輸手機(jī)畫(huà)面,觀眾也能同步接收?qǐng)D像。此前,基于抖音平臺(tái)開(kāi)發(fā)的虛擬人直播演示,B站、快手等平臺(tái)采用的原理相似。虛擬人通過(guò)用戶端的固定算法進(jìn)行渲染,與主播現(xiàn)場(chǎng)拍攝的畫(huà)面存在一定差異。
public void playText(String text) {
if (mTextApi == null) return;
mTextApi.playTextExpression(text);
}
成本優(yōu)勢(shì)方案
即構(gòu)ZIM具備全面的即時(shí)通訊功能,涵蓋一對(duì)一聊天、多人群聊以及房間內(nèi)交流,可根據(jù)需求靈活搭配。在虛擬直播場(chǎng)景中,文字信息實(shí)時(shí)傳輸至各終端,再本地生成畫(huà)面,這種方式成本最低。直播間可利用房間管理功能,而在即構(gòu)IM的房間中,用戶可以通過(guò)彈幕功能發(fā)送和接收文字信息。
看完這段,大家對(duì)虛擬人直播的制作步驟是否已經(jīng)明了?接下來(lái),你打算將這些建議用于哪些無(wú)人直播的場(chǎng)合?歡迎在評(píng)論區(qū)留言交流!別忘了點(diǎn)贊和轉(zhuǎn)發(fā)這篇文章。