java实现amr转mp3

        由于amr格式的语音,在前端页面一般不能直接播放,需要后台转换,下面实现amr格式的语音转mp3.

       下载jave-1.0.2.jar包,创建一个maven项目,在resources目录下创建一个lib目录,把jave-1.0.2.jar包拷贝到lib目录下,在pom.xml里面依赖jave-1.0.2.jar本地包,maven不能远程下载jave-1.0.2.jar包,需要本地引入,引入代码如下:


            jack.jave
            jave
            1.0.2
            system
            ${basedir}/src/main/resources/lib/jave-1.0.2.jar
        


groupId和artifactId可以随便填写。


下面是实现的java代码如下:

package com.qwrt.station.websocket.util;

import it.sauronsoftware.jave.*;

import java.io.File;

/**
 * Created by jack on 2017/11/8.
 *
 * http://blog.csdn.net/z313731418/article/details/50218341
 * http://blog.csdn.net/z313731418/article/details/50218341
 * mvn install:install-file -Dfile=D:\programing\apache-maven-3.5.0\jave-1.0.2.jar -DgroupId=jack.jave -DartifactId=jave -Dversion=1.0.2 -Dpackaging=jar
 *
 * http://blog.csdn.net/ricciozhang/article/details/49254605
 *
 *
 */
public class ChangeFormat {
    public static void main(String[] args) throws Exception {
        String path1 = "F:\\mystudy\\pic\\1510123295814.amr";
        String path2 = "F:\\mystudy\\pic\\jack.mp3";
        changeAmrToMp3(path1, path2);
    }

    public static void changeAmrToMp3(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);

        try {
            encoder.encode(source, target, attrs);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InputFormatException e) {
            e.printStackTrace();
        } catch (EncoderException e) {
            e.printStackTrace();
        }
    }
}


如果出现不是win32程序的错误,查找:C:\Users\(计算机的用户名目录)\AppData\Local\Temp\jave-1 ,

删除jave-1文件夹即可,再次运行就好了,因为换了多个jave版本,导致的缓存吧。 


转换在linux环境下可能存在问题,可以查看网上的资料解决。

public static boolean amrtomp3(String localPath, String targetFilePath){
        try {

            System.out.println("**************  ffmpeg ****************");

            java.lang.Runtime rt = Runtime.getRuntime();
            System.out.println(System.getProperty("user.dir"));
            //System.out.println(Class.class.getClass().getResource("").getPath());
            System.out.println(ChangeFormat.class.getResource("").getPath());
            //String classUrl = ChangeFormat.class.getResource("").getPath();
            String command = "F:/mystudy/jave-1.0.2/it/sauronsoftware/jave/ffmpeg -i " + localPath + " " + targetFilePath;
            //String command = classUrl + "ffmpeg -i " + localPath + " " + targetFilePath;

            System.out.println("ffmpeg exec command = " + command);

            Process proc = rt.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);
            System.out.println("ffmpeg Process errorInfo: " + sb.toString());

            int exitVal = proc.waitFor();
            System.out.println("ffmpeg Process exitValue: " + exitVal);

            return true;
        } catch (Exception e) {
            System.out.println("ffmpeg exec cmd Exception " + e.toString());
        }
        return false;
    }




linux下实现amr格式转mp3,

主要java代码如下:

public static void amrToMP3Linux(String localPath, String targetFilePath){
        System.out.println("执行命令开始");
        String command = "ffmpeg  -i "+localPath+" "+targetFilePath;
        System.out.println("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();
            System.out.println("the exitVal is : "+exitVal);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            System.out.println("ffmpeg exec cmd Exception " + e.toString());
        }
        System.out.println("执行命令结束");
        System.out.println(runtime);
    }



有上面的代码还不行,还需下载ffmpeg,下载地址:http://download.csdn.net/download/wj903829182/10152294,然后解压到linux相应目录下,配置ffmpeg的环境变量,修改/etc/profile,

比如:

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


然后使用命令使配置文件生效:

source /etc/profile













你可能感兴趣的:(java,java,web学习的问题和解决,编程中学习整理的资料,我的代码)