(简单粗暴)【Java调用python】调用anaconda环境,虚拟主播结合百度文心一言生成视频

视频效果:

虚拟主播合成视频

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.文心一言回答 / 虚拟主播生成视频
百度文心一言回答
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值