zoukankan      html  css  js  c++  java
  • 微信客服开发之语音聊天amr语音文件转换为mp3

    一 问题起因

      在做微信客服开发时,遇到了一个问题 就是微信语音的格式是amr格式  一般的浏览器还解析不了这类语音文件,必须要将它转换为mp3格式;

    二 问题解决

      我在网上找了一些资料,说的不全,在此总结补充一下;

      1.需要下载第三方jar 下载JAVE 1.0.2

        http://www.sauronsoftware.it/projects/jave/download.php

      2.引入pom依赖

        

            <dependency>
                <groupId>it.sauronsoftware.jave</groupId>
                <artifactId>jave</artifactId>
                <version>1.0.2</version>
            </dependency>

      3.mvn 打包项目 会报错 然后你把 jave-1.0.2.jar放到maven的依赖仓库中 

    三 代码

      

    package com.hmzj.callcenterim.utils;
    
    
    import it.sauronsoftware.jave.*;
    import lombok.extern.slf4j.Slf4j;
    
    import java.io.*;
    
    @Slf4j
    public class ChangeAudioFormat {
    
        public static void main(String[] args) throws Exception {
            String path1 = "C:\Users\pc\Desktop\amr.js-master\assets\female.amr";
            String path2 = "C:\Users\pc\Desktop\amr.js-master\assets\female.mp3";
            changeToMp3(path1, path2);
        }
    
        /**
         * windows版 执行此方法
         * 
         * @param sourcePath
         * @param targetPath
         */
        public static void changeToMp3(String sourcePath, String targetPath) {
            File source = new File(sourcePath);
            File target = new File(targetPath);
            AudioAttributes audio = new AudioAttributes();
            Encoder encoder = new Encoder();
    
            audio.setCodec("libmp3lame");
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("mp3");
            attrs.setAudioAttributes(audio);
            log.debug("正在转换微信语音amr");
            try {
                encoder.encode(source, target, attrs);
                log.debug("正在转换微信语音");
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
                log.debug(e.toString());
            } catch (InputFormatException e) {
                e.printStackTrace();
                log.debug(e.toString());
            } catch (EncoderException e) {
                e.printStackTrace();
                log.debug(e.toString());
            }
        }
    
        /**
         * linux执行此方法
         * 
         * @param localPath
         * @param targetFilePath
         */
        public static void amrToMP3Linux(String localPath, String targetFilePath){
            log.debug("执行命令开始");
            String command = "ffmpeg  -i "+localPath+" "+targetFilePath;
            log.debug("the command is : "+command);
            Runtime runtime = Runtime.getRuntime();
            try {
                Process proc = runtime.exec(command);
                InputStream stderr = proc.getErrorStream();
                InputStreamReader isr = new InputStreamReader(stderr);
                BufferedReader br = new BufferedReader(isr);
                String line = null;
                StringBuffer sb = new StringBuffer();
                while ((line = br.readLine()) != null)
                    sb.append(line);
                int exitVal = proc.waitFor();
                log.debug("the exitVal is : "+exitVal);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                log.debug("ffmpeg exec cmd Exception " + e.toString());
            }
            log.debug("执行命令结束");
            log.debug(runtime.toString());
        }
    
    }

    注意windows上可使用 changeToMp3 这个方法 但linux上此方法不行 必须执行 amrToMP3Linux 此方法

     四  在linux上安装ffmpeg

      下载站点:http://ffmpeg.org/download.html

     因为我的服务器是64位 所以下载解压这个

     

    利用ftp将解压后的文件夹放到服务器上

     赋予权限  

       # chmod +x /usr/local/ffmpeg-4.0.2-64bit-static/ffmpeg

    配置环境变量

      vi etc/profile

      export FFMPEG_HOME=/usr/local/ffmpeg-4.0.2-64bit-static/
      export PATH=$FFMPEG_HOME:$PATH

    保存并退出 

      source etc/profile

     然后在服务器上执行上面的linux方法  看有没有文件生成,并且看能不能用

  • 相关阅读:
    关于UltraISO打开iso文件后只有部分文件问题
    CollectionViewController 集合视图
    SDWebImage, 关于第三方异步加载图片的一些方法
    NSFileManager(文件管理类)
    UIReview(UI总结)
    sqlite( 轻量级数据库)
    DataPersistence(数据持久化: 文件读写, NSUserDefault, 归档)
    第三方(SDWebImage, 网络类AFNetWorking)
    网络编程(GET, POST)
    数据解析(SAX, JSON)
  • 原文地址:https://www.cnblogs.com/pangyangqi/p/9663232.html
Copyright © 2011-2022 走看看