amr转成mp3步骤


为了导入依赖写的pom文件,把jar转成pom文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.sauronsoftware.it</groupId>
    <artifactId>jave</artifactId>
    <version>1.0.2</version>
    <packaging>jar</packaging>

</project>

mvn install:install-file -Dfile=jave-1.0.2.jar -DgroupId=com.sauronsoftware.it  -DartifactId=jave -Dversion=1.0.2  -Dpackaging=jar -DgeneratePom=true

包完整路径
其中,命令中指定的groupId等都与pom文件保持一致。

下载一个http://ffmpeg.org/download.html这个放在linux上的指定路径

jave的下载路径http://www.sauronsoftware.it/projects/jave/download.php?PHPSESSID=qfp2bnie6jhl3mn6elf8o73lq5

在linux中直接使用ffmpeg在window中需要用到jave

具体使用

package com.ld.service.itoperations.utils;

import java.io.File;
import java.io.IOException;

import org.springframework.stereotype.Component;

import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncodingAttributes;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class AudioUtils {

    /**
     * 将一个amr文件转换成mp3文件
     * @param amrFileName         源amr文件所在位置
     * @param mp3FileName      目标文件MP3所在位置
     * @param pathAudio          FFmpeg所在linux中的位置
     * @throws IOException
     */
    public static void amr2mp3(String amrFileName, String mp3FileName, String pathAudio) throws IOException {
        String os = System.getProperties().getProperty("os.name").toLowerCase();
        File source = new File(amrFileName);
        if (os.startsWith("win")) {
            File target = new File(mp3FileName);
            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 (Exception e) {
                log.error("convert amr to mp3 error", e);
            }
            // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除(window)
            if (source.exists() && source.isFile()) {
                try {
                    source.delete();
                }
                catch (Exception e) {
                    log.error("convert amr to mp3 error {}", e);
                }
            }
        }
        else {
            String command = pathAudio + "/ffmpeg -i " + amrFileName + " " + mp3FileName;
            try {
                Runtime.getRuntime().exec(new String[] { "sh", "-c", command });
                //文件删除(linux系统中)
                command = "rm -rf "+ amrFileName;
                Runtime.getRuntime().exec(new String[] { "sh", "-c", command });
            }
            catch (IOException e) {
                log.error("convert amr to mp3 error", e);
            }
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值