linux+java+ffmpeg 实现音视频转码技术

linux+java+ffmpeg 实现音视频转码技术

 执行命令 
 1.安装lame-3.1.0
                tar -zvxf  lame-3.1.0.tar.gz       //解压 
                cd lame-3.1                        //进入解压后的文件
                ./configure                        //配置
                make                               //编译
                make install                       //安装

//下载地址   http://download.csdn.net/download/qq_33563609/10107483

2.安装yasm   
                tar -zvxf  yasm-1.3.0.tar.gz       
                cd yasm-1.3                       
                ./configure                       
                make                              
                make install                     

//下载地址  http://download.csdn.net/download/qq_33563609/10107517


 3.安装ffmpeg  
                tar -zvxf  ffmpeg-3.4.tar.gz
                cd ffmpeg-3.4
                ./configure --enable-shared --enable-libmp3lame                      
                make                               
                make install                       
//下载地址  http://download.csdn.net/download/qq_33563609/10107527

 4.全部安装后  执行   
                ffmpeg  --version  查看是否安装成功
                正常情况下会报错 找不到 lame的启动路径  错误代码  0  或者 57  等等
                在文件路径   /etc/ld.so.conf   这个文件 编辑这个文件 
                此文件默认内容是   include ld.so.conf.d/*.conf
                在文件中 换行加入  /usr/local/lib  这段路径然后保存  并执行/sbin/ldconfig 来更新当前修改
                注意 编辑文件应在root权限下, su 回车 输入密码,密码不显示  权限由$符号变为#代表权限改变了

                在执行  
                ffmpeg  -version  查看是否安装成功

5.执行        
                ffmepg -i input.wav -f mp3 -acodec libmp3lame -y output.mp3
                           输入文件                               输出文件
                           路径名称                               路径名称

6.maven中导入jar包   jar包为  jave-1.0.2.jar
                maven本地导入方法为   cmd 命令
                mvn install:install-file 
                    -Dfile=E:\jave-1.0.2.jar    //包的输入路径 
                    -DgroupId=jave 
                    -DartifactId=jave 
                    -Dversion=1.0.2 
                    -Dpackaging=jar 
                //执行完成后 jar 会放入maven 仓库中  maven/repository/jave/jave/1.0.2/
                pom.xml文件配置为
                <dependency>
                    <groupId>jave</groupId>
                    <artifactId>jave</artifactId>
                    <version>1.0.2</version>
                 </dependency>
//下载地址             http://download.csdn.net/download/qq_33563609/10107555

7.java代码    方案一
 /*  sources      输入文件的路径和名称
       *      desFileName  输出文件的路径和名称
       */
    public static File execute(String sources, String desFileName)
            throws Exception {
            File source=new File(sources);
            File target = new File(desFileName);
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("libmp3lame");               //编解码器
            audio.setBitRate(new Integer(192000));      //音频比率 MP3默认是128000kb  比特率   96000 128000 160000 192000 256000 384000 512000 1024000 1500000 2048000 4096000
            audio.setChannels(new Integer(2));          //声道
            audio.setSamplingRate(new Integer(48000));  //采样率
            //audio.setVolume(16);                      //音量
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setFormat("mp3");
            attrs.setAudioAttributes(audio);
            Encoder encoder = new Encoder();
            encoder.encode(source, target, attrs);
            return target;
 }   

8.java代码    方案二
public static void MavToMp3(String sources,String desFileName){
            List<String> commend = new ArrayList<String>();
            commend.add("/usr/local/bin/ffmpeg");
            commend.add("-i");
            commend.add(sources);
            commend.add("-f");
            commend.add("mp3");
            commend.add("-acodec");
            commend.add("libmp3lame");
            commend.add("-y");
            commend.add(desFileName);
            StringBuffer test=new StringBuffer();
            for(int i=0;i<commend.size();i++)
                    test.append(commend.get(i)+" ");
            System.out.println(test);
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            try {
                    builder.redirectErrorStream(true);
                    builder.start();
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
            }
            System.out.println("音频转换成功");
 }      

猜你喜欢

转载自blog.csdn.net/qq_33563609/article/details/78469071