为了导入依赖写的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);
}
}
}
}