java中媒体文件的处理大多数用到的是jave,但是太久没有维护更新,linux支持方面不太友好,我们可以自己封装ffmpeg进行一个媒体文件的处理工作。
下载
官网可以下载源码文件自己编译成linux和windows系统可执行的文件,但是也提供了编译好的文件(当然是其平台编译好的,你也可以自己去做这件事)。
官网:https://ffmpeg.org/
linux
进入后根据自己的linux内核下载对应版本就可以了,右边是稳定的版本
Windows
Windows平台同样也提供了下载可执行exe文件的地方,可自行选择下载
命令行使用
Windows下载后可以得到这三个可执行文件,用到的是ffmpeg和ffprobe这两个文件,具体作用,图片可见。
linux平台下载后得到内容相同。
下载演示在Windows的使用方法:
ffprobe使用
进入到解压目录后我们就可以使用命令直接查看媒体文件的基本信息了,
通过此命令我们可以获取媒体文件的基本信息,比如说时长得信息,下边我会举例使用Jave方式获取时长信息、
查看MP4信息:ffprobe C:\Users\intel\Videos\703242828760279.mp4
查看MP3信息:ffprobe C:\Users\intel\Videos\hahaha.mp3
查看amr信息:ffprobe ffprobe C:\Users\intel\Videos\21141620.amr
ffmpeg使用
首先下边的使用方法都是最基本的,可以在官网查询具体的参数使用方法。在此不做过多示例。
amr文件转成MP3
ffmpeg -i C:\Users\intel\Videos\21141620.amr -y C:\Users\intel\Videos\21141621.mp3
-y:覆盖原文件
mp4转成avi
ffmpeg -i C:\Users\intel\Videos\703242828760279.mp4 -y C:\Users\intel\Videos\703242828760280.avi
JAVA中使用
在Java中我们可以利用ffmpeg将媒体文件进行转换,我使用场景是将amr语音文件转换成mp3格式,以供H5能够播放。
下边是提供的工具类,实现了amr转Mp3、获取媒体文件的时长。
package com.bananalab.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 媒体工具类
*
* @Author 李文龙
* @Date 2021/8/3 16:54
* @Version 1.0
*/
public class MediaUtils {
public void amr2Mp3(File source) {
String ffmpegPath = ffmpegPath();
File target = null;
try {
target = File.createTempFile("yetiapp", ".mp3");
} catch (IOException e) {
e.printStackTrace();
}
if (null == target) {
return;
}
List<String> command = new ArrayList<>();
command.add(ffmpegPath);
command.add("-i");
command.add(source.getAbsolutePath());
command.add("-y");
command.add(target.getAbsolutePath());
//启动进程
Process process = start(command);
printRes(process);
}
/**
* 获取长度
*
* @param file 文件
* @return 时长
*/
public long calcuateDuration(File file) {
String ffprobePath = ffprobePath();
List<String> command = new ArrayList<>();
command.add(ffprobePath);
command.add(file.getAbsolutePath());
//启动进程
Process start = start(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(start.getInputStream()));
return duration(bufferedReader);
}
private static long duration(BufferedReader reader) {
Pattern p1 = Pattern.compile("^\\s*Input #0, (\\w+).+$\\s*", 2);
Pattern p2 = Pattern.compile("^\\s*Duration: (\\d\\d):(\\d\\d):(\\d\\d)\\.(\\d).*$", 2);
Pattern p3 = Pattern.compile("^\\s*Stream #\\S+: ((?:Audio)|(?:Video)|(?:Data)): (.*)\\s*$", 2);
while (true) {
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line == null) {
break;
}
System.out.println(line);
Matcher m = p2.matcher(line);
if (m.matches()) {
long hours = Integer.parseInt(m.group(1));
long minutes = Integer.parseInt(m.group(2));
long seconds = Integer.parseInt(m.group(3));
long dec = Integer.parseInt(m.group(4));
return dec * 100L + seconds * 1000L + minutes * 60L * 1000L + hours * 60L * 60L * 1000L;
}
}
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
private String ffmpegPath() {
String ffmpeg = "/usr/local/ffmpeg/ffmpeg.exe";
if (!isWindows()) {
ffmpeg = "/usr/local/ffmpeg/ffmpeg";
}
return ffmpeg;
}
private String ffprobePath() {
String ffprobe = "/usr/local/ffmpeg/ffprobe.exe";
if (!isWindows()) {
ffprobe = "/usr/local/ffmpeg/ffprobe";
}
return ffprobe;
}
private Process start(List<String> command) {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(command);
//将标准输入流和错误输入流合并,通过标准输入流读取信息
processBuilder.redirectErrorStream(true);
//启动进程
Process start = null;
try {
start = processBuilder.start();
} catch (IOException e) {
e.printStackTrace();
}
return start;
}
private void printRes(Process process) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (true) {
String line = null;
try {
line = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line == null) {
break;
}
System.out.println(line);
}
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean isWindows() {
String os = System.getProperty("os.name").toLowerCase();
return os.contains("windows");
}
}
。