Java实现视频添加图片水印并且将视频上传到云服务器返回视频的URL

Java实现视频添加图片水印并且将视频上传到云服务器返回视频的URL

实现思路:

前段时间接了一个需求就是小程序那边分享动态的时候 会分享自己的一些图片或者是视频  而我需要做的就是把客户上传的视频或者是图片都添加上水印  这样别人看到视频或者图片就可以知道这些出处

实现效果:

在这里插入图片描述

实现代码:

	/**
     * 视频添加水印
     * @param username
     * @param file
     * @return
     * @throws IOException
     */
    public static String addShipen(String username,String file) throws IOException {
        ProcessExec ps = new ProcessExec();
        HashMap<String, String> dto=new HashMap<String, String>();
        // 将文本转为图片
        String name = "水印文字 @"+username;
        // 生成图片名称
        String fileName = System.currentTimeMillis() + "suyin";
        String path = "/Users/tp/"+fileName;
        // 文字转换为无背景图片可以查看我之前写的
        // https://blog.csdn.net/m0_45245077/article/details/123385949?spm=1001.2014.3001.5501
        textToPicture(name, path);
        // ffmpeg工具需要单独安装到本地 这个网上随便都可以找到方法
        // 代表你本机上ffmpeg执行的位置
        dto.put("ffmpeg_path","/usr/local/bin/ffmpeg ");
        // 根据腾讯云url将视频保存到本地
        String txUrl = file;
        // 生成视频名称
        String spName = System.currentTimeMillis() + ".mp4";
        String bdPath = "/Users/tp/shipi/"+spName;
        // 将网络图片添加到本地可以查看我之前写的
        // https://blog.csdn.net/m0_45245077/article/details/123434411?spm=1001.2014.3001.5501
        boolean downVideo = downVideo(txUrl, bdPath);
        String execute = "";
        if (downVideo){
            // 代表你要处理的视频的位置
            dto.put("input_path","/Users/tp/shipi/" + spName);
            // 生成视频名称
            String pname = System.currentTimeMillis() + ".mp4";
            // 代表你处理完的视频存放的位置
            dto.put("video_converted_path", "/Users/tp/shipi/"+pname);
            dto.put("pname",pname);
            dto.put("bdPath",spName);
            dto.put("path",fileName);
            // 代表你加水印图片的位置
            dto.put("logo", path);
            // 执行视频添加水印方法&返回上传后的url地址
            execute = ps.execute(dto);
            System.out.println("上传服务云上的url地址:"+execute);
        }
        return execute;
    }

	/**
     * 视频添加水印
     * @param dto
     */
    public String execute(Map<String, String> dto) throws IOException {

        Image srcImg = ImageIO.read(new File(dto.get("logo")));
        BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
        int width = buffImg.getWidth();
        int height = buffImg.getHeight();
        StringBuffer waterlogo = new StringBuffer();
        waterlogo.append("-i ");
        if(null!=dto.get("input_path") && StringUtils.isNotEmpty(dto.get("input_path"))){
            waterlogo.append(dto.get("input_path"));
        }
        waterlogo.append(" -vf movie=");
        if (null!=dto.get("logo") && StringUtils.isNotEmpty(dto.get("logo"))){
            waterlogo.append(dto.get("logo"));
        }
        waterlogo.append(",scale="+width+":"+height);
        waterlogo.append("[watermark];[in][watermark]overlay=10:main_h-overlay_h-10[out] ");
        if (null!=dto.get("video_converted_path") && StringUtils.isNotEmpty(dto.get("video_converted_path"))){
            waterlogo.append(dto.get("video_converted_path"));
        }
        System.out.println(waterlogo);
        Runtime run = Runtime.getRuntime();
        String ffmegPath = null;
        if (StringUtils.isNotEmpty(dto.get("ffmpeg_path"))){
            ffmegPath = dto.get("ffmpeg_path");
        }
        String path = "";
        // 执行命
        try {
            java.lang.Process process = run.exec(ffmegPath+waterlogo);
            // 异步读取输出
            InputStream inputStream = process.getInputStream();
            InputStream errorStream = process.getErrorStream();

            BufferedReader bufferedReader = new BufferedReader( new InputStreamReader( inputStream,"gbk" ) );
            BufferedReader reader = new BufferedReader( new InputStreamReader( errorStream,"gbk" ) );
            String line = null;
            while ( ( line = bufferedReader.readLine() ) != null ) {
                System.out.println( line );
            }
            String linew = null;
            while ( ( linew = reader.readLine() ) != null ) {
                System.out.println( linew );
            }
            process.waitFor();
            // SecretId - SecretKey
            VodUploadClient client = new VodUploadClient("腾讯云的SecretId", "腾讯云的SecretKey");
            VodUploadRequest request = new VodUploadRequest();
            //任务流名称 如果不走任务流会播放器会出现 1005错
            request.setProcedure("auditAndScreenshot");
            //本地视频地址
            request.setMediaFilePath("/Users/tp/shipi/"+dto.get("pname"));
            VodUploadResponse response = client.upload("ap-shanghai", request);
            System.out.println("Upload FileId = {"+response.getFileId()+"}");
            String mediaUrl = response.getMediaUrl();
            // 腾讯云的访问路径
            path = mediaUrl;
            //根据路径删除打完水印的视频 -- 不需要删除本地视频的可以将此关闭
            File file = new File("/Users/tp/shipi/"+dto.get("pname")); 
            if(file.isFile()&&file.exists()){
                file.delete();
            }
            // 删除腾讯云下载的视频 -- 不需要删除本地视频的可以将此关闭
            File file1 = new File("/Users/tp/shipi/"+dto.get("bdPath"));
            if(file1.isFile()&&file1.exists()){
                file1.delete();
            }
            // 删除文字水印图片 -- 不需要删除的可以将此关闭
            File file2 = new File("/Users/tp/"+dto.get("path"));
            if(file2.isFile()&&file2.exists()){
                file2.delete();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }catch (Exception e) {
            // 业务方进行异常处理
            System.out.println("Upload Err = {"+e+"}");
            throw new RuntimeException("123");
        }
        return path;
    }

由于我的电脑是Mac本所以本地的路径都是类似:/Users/tp/。如果你使用的是Win电脑可以自行更换路径

实现文字转换无背景图片
实现URL下载视频到本地

ps:一个开发界的小学生,一直在学习从未敢停止

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TianYuuuuuuuuuuu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值