视频效果:
虚拟主播合成视频
1.项目环境
创建anaconda环境,将虚拟主播程序文件放到环境同一个文件夹下,运行时将jar包也放在环境文件夹下运行
2.具体代码
1.整合百度文心一言,获取回答
(详见以前文章)
2.将百度答案,和虚拟主播项目结合生成视频
public static String getAnswerVideoUtil(String doctor,String user,String text) throws IOException{
System.err.println("输入用户视频名称: "+user);
System.err.println("百度回答: "+text);
String res = null;
String input = "--human ./file/input/"+doctor+".mp4 --output ../"+user+".mp4 --text "+text;
String pythonScriptPath = "E:\\ProgramData\\anaconda3\\envs\\paddle2\\general_demo.py";
try {
Process process = Runtime.getRuntime().exec("E:\\ProgramData\\anaconda3\\envs\\paddle2\\python.exe " + pythonScriptPath + " " + input);
StreamHandler errorStreamHandler = new StreamHandler(process.getErrorStream(), "ERROR");
errorStreamHandler.start();
StreamHandler outputStreamHandler = new StreamHandler(process.getInputStream(), "STDOUT");
outputStreamHandler.start();
int proc = process.waitFor();
if(proc==0){
System.out.println("成功");
res ="成功";
}else {
System.out.println("失败");
res ="失败";
}
if(process!=null){
process.destroy();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return res;
}
处理Runtime.getRuntime().exec产生的错误流及输出流
package com.example.demo.model;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 用于处理Runtime.getRuntime().exec产生的错误流及输出流
*
*/
public class StreamHandler extends Thread {
InputStream m_inputStream;
String m_type;
public StreamHandler(InputStream is, String type) {
this.m_inputStream = is;
this.m_type = type;
}
@Override
public void run() {
InputStreamReader isr = null;
BufferedReader br = null;
try {
//设置编码方式,否则输出中文时容易乱码
isr = new InputStreamReader(m_inputStream, "GBK");
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("PRINT > " + m_type + " : " + line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
br.close();
isr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
3.返回生成视频
/**
* 获取视频
*
* @param
* @return 单条数据
*/
@GetMapping(value = "/playMp4/{user}",produces ="application/json;charset=utf-8")
public void playMp4(HttpServletRequest request, HttpServletResponse response,
@PathVariable("user") String fileName){
String floderPath = "E:\\ProgramData\\anaconda3\\envs\\";
aloneVideoPlay(request,response,floderPath,fileName);
}
/**
* @description: 在线播放MP4文件
* @param: [request, floderPath 文件夹路径, fileName 文件名称, response]
**/
public static void aloneVideoPlay(HttpServletRequest request, HttpServletResponse response,String floderPath, String fileName) {
InputStream is = null;
OutputStream os = null;
fileName = fileName+".mp4";
try {
response.setContentType("video/mp4");
File file = new File(floderPath + fileName);
response.addHeader("Content-Length", "" + file.length());
is = new FileInputStream(file);
os = response.getOutputStream();
IOUtils.copy(is, os);
} catch (Exception e) {
System.err.println("播放MP4失败"+e);
} finally {
if (null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4 具体效果
问题:你吃饭了吗?
视频效果:
1.接口调用
2.文心一言回答 / 虚拟主播生成视频