获取音频的时长
/**
* 获取视频文件的时长
* @param ffmpegPath 是ffmpeg软件存放的目录,sourceFile是目标文件
* @return
*/
public String duration(String ffmpegPath,String sourceFile){
List<String> duration = new ArrayList<String>();
String ffmpegroot = ffmpegPath+"/ffmpeg";
duration.add(ffmpegroot);
duration.add("-i");
duration.add(sourceFile);
ProcessBuilder pb = new ProcessBuilder();
pb.command(duration);
pb.redirectErrorStream(true);
InputStream is = null;
BufferedReader br = null;
try {
Process p = pb.start();
is = p.getInputStream();
StringBuffer outS = new StringBuffer("");
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = br.readLine();
while(line!=null){
outS.append(line);
line = br.readLine();
}
String out = outS.toString();
int index = out.indexOf("Duration:");
int end = out.indexOf(",", index);
if(index>=0){
String result = out.substring(index+10, end);
return result;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
br.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
音频的原时长:00:00:25.60
获取音频的多少秒(和上面那个方法一块使用)
//参数格式:"00:00:10.68"
public float getTimelen(String timelen){
float min=0.0f;
String strs[] = timelen.split(":");
if (strs[0].compareTo("0") > 0) {
min+=Integer.valueOf(strs[0])*60*60;//秒
}
if(strs[1].compareTo("0")>0){
min+=Integer.valueOf(strs[1])*60;
}
if(strs[2].compareTo("0")>0){
min+=Float.valueOf(strs[2]);
}
return min;
}
截取音频的指定时长
/**
* 截取音频的 从front-duration秒
* @param ffmpegPath ffmpeg程序的路径
* @param sourcePath 源文件
* @param targetPath 需要生成的文件
* @param front 从多少秒开始截取
* @param duration 一共截取多长时间
* @throws Exception
*/
public static void wavCut(String ffmpegPath,String sourcePath, String targetPath,double front,double duration) throws Exception {
List<String> wavToPcm = new ArrayList<String>();
wavToPcm.add(ffmpegPath+"/ffmpeg");
wavToPcm.add("-i");
wavToPcm.add(sourcePath);
wavToPcm.add("-ss");
wavToPcm.add("00:00:0"+front);
wavToPcm.add("-t");
wavToPcm.add(duration+"");
wavToPcm.add("-y");
wavToPcm.add(targetPath);
ProcessBuilder builder = new ProcessBuilder();
builder.command(wavToPcm);
for(String str:wavToPcm){
System.out.print(str+" ");
}
System.out.println();
builder.redirectErrorStream(true);
try {
Process process=builder.start();
int a=process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch(InterruptedException ie){
ie.printStackTrace();
}
}
tip注意
ffmpeg截取一段视频中一段视频
ffmpeg -i ./plutopr.mp4 -vcodec copy -acodec copy -ss 00:00:10 -to 00:00:15 ./cutout1.mp4 -y
-ss time_off set the start time offset 设置从视频的哪个时间点开始截取,上文从视频的第10s开始截取
-to 截到视频的哪个时间点结束。上文到视频的第15s结束。截出的视频共5s.
如果用-t 表示截取多长的时间如 上文-to 换位-t则是截取从视频的第10s开始,截取15s时长的视频。即截出来的视频共15s.
注意的地方是:
如果将-ss放在-i ./plutopr.mp4后面则-to的作用就没了,跟-t一样的效果了,变成了截取多长视频。一定要注意-ss的位置。
参数解析
-vcodec copy表示使用跟原视频一样的视频编解码器。
-acodec copy表示使用跟原视频一样的音频编解码器。
-i 表示源视频文件
-y 表示如果输出文件已存在则覆盖。
----------------
音频格式转换 ---转8位
public static void changeWavToRightWav(String ffmpegPath,String sourcePath, String targetPath) throws Exception {
List<String> wavToWav = new ArrayList<String>();
wavToWav.add(ffmpegPath+"/ffmpeg");
wavToWav.add("-i");
wavToWav.add(sourcePath);
wavToWav.add("-ar");
wavToWav.add("8000");
wavToWav.add("-ac");
wavToWav.add("1");
wavToWav.add("-acodec");
wavToWav.add("pcm_alaw");
wavToWav.add("-y");
wavToWav.add(targetPath);
ProcessBuilder builder = new ProcessBuilder();
builder.command(wavToWav);
builder.redirectErrorStream(true);
final Process process=builder.start();
//处理InputStream的线程
new Thread()
{
@Override
public void run()
{
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
try
{
while((line = in.readLine()) != null)
{
System.out.println("output: " + line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}.start();
new Thread()
{
@Override
public void run()
{
BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
try
{
while((line = err.readLine()) != null)
{
System.out.println("err: " + line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
err.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}.start();
int a=process.waitFor();
}
arm转wav
public static void changeAmrToWav(String ffmpegPath,String sourcePath, String targetPath) throws Exception {
List<String> wavToPcm = new ArrayList<String>();
wavToPcm.add(ffmpegPath+"/ffmpeg");
wavToPcm.add("-y");
wavToPcm.add("-i");
wavToPcm.add(sourcePath);
wavToPcm.add("-acodec");
wavToPcm.add("pcm_alaw");
wavToPcm.add(targetPath);
ProcessBuilder builder = new ProcessBuilder();
builder.command(wavToPcm);
builder.redirectErrorStream(true);
final Process process=builder.start();
//处理InputStream的线程
new Thread()
{
@Override
public void run()
{
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
try
{
while((line = in.readLine()) != null)
{
System.out.println("output: " + line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}.start();
new Thread()
{
@Override
public void run()
{
BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
try
{
while((line = err.readLine()) != null)
{
System.out.println("err: " + line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
err.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}.start();
int a=process.waitFor();
}
得到amr的时长
/**
* 得到amr的时长
*
* @param file
* @return
* @throws IOException
*/
public static long getAmrDuration(File file) throws IOException {
long duration = -1;
int[] packedSize = { 12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0 };
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "rw");
long length = file.length();//文件的长度
int pos = 6;//设置初始位置
int frameCount = 0;//初始帧数
int packedPos = -1;
/////////////////////////////////////////////////////
byte[] datas = new byte[1];//初始数据值
while (pos <= length) {
randomAccessFile.seek(pos);
if (randomAccessFile.read(datas, 0, 1) != 1) {
duration = length > 0 ? ((length - 6) / 650) : 0;
break;
}
packedPos = (datas[0] >> 3) & 0x0F;
pos += packedSize[packedPos] + 1;
frameCount++;
}
/////////////////////////////////////////////////////
duration += frameCount * 20;//帧数*20
} finally {
if (randomAccessFile != null) {
randomAccessFile.close();
}
}
return duration;
}
获取音频速率
/**
* 功能:获取音频速率
* @param file
* @return
* @throws Exception
*/
public static Integer getWavRate(File file) throws Exception{
FileInputStream fis = new FileInputStream(file);
BufferedInputStream buffer = new BufferedInputStream(fis);
AudioInputStream ain = AudioSystem.getAudioInputStream(buffer);
AudioFormat format=ain.getFormat();
Float frameRate = format.getFrameRate();
return frameRate!=null?frameRate.intValue():0;
}
wav转pcm
public static void changeWavToPcm(String ffmpegPath,String sourcePath, String targetPath) throws Exception {
List<String> wavToPcm = new ArrayList<String>();
wavToPcm.add(ffmpegPath+"/ffmpeg");
wavToPcm.add("-y");
wavToPcm.add("-i");
wavToPcm.add(sourcePath);
wavToPcm.add("-acodec");
wavToPcm.add("pcm_s16le");
wavToPcm.add("-f");
wavToPcm.add("s16le");
wavToPcm.add("-ac");
wavToPcm.add("1");
wavToPcm.add("-ar");
wavToPcm.add("8000");
wavToPcm.add(targetPath);
ProcessBuilder builder = new ProcessBuilder();
builder.command(wavToPcm);
builder.redirectErrorStream(true);
final Process process=builder.start();
//处理InputStream的线程
new Thread()
{
@Override
public void run()
{
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
try
{
while((line = in.readLine()) != null)
{
System.out.println("output: " + line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}.start();
new Thread()
{
@Override
public void run()
{
BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String line = null;
try
{
while((line = err.readLine()) != null)
{
System.out.println("err: " + line);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
err.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}.start();
process.waitFor();
}
获取视频的第一秒截图
package com.lzh.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
/**
*
* @Description: 获取视频的信息
*/
public class FetchVideoCover {
// 视频路径
private String ffmpegEXE;
public void getCover(String videoInputPath, String coverOutputPath) throws IOException, InterruptedException {
// ffmpeg.exe -ss 00:00:01 -i spring.mp4 -vframes 1 bb.jpg
List<String> command = new java.util.ArrayList<String>();
command.add(ffmpegEXE);
// 指定截取第1秒
command.add("-ss");
command.add("00:00:01");
command.add("-y");
command.add("-i");
command.add(videoInputPath);
command.add("-vframes");
command.add("1");
command.add(coverOutputPath);
for (String c : command) {
System.out.print(c + " ");
}
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = "";
while ( (line = br.readLine()) != null ) {
}
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}
public String getFfmpegEXE() {
return ffmpegEXE;
}
public void setFfmpegEXE(String ffmpegEXE) {
this.ffmpegEXE = ffmpegEXE;
}
public FetchVideoCover() {
super();
}
public FetchVideoCover(String ffmpegEXE) {
this.ffmpegEXE = ffmpegEXE;
}
public static void main(String[] args) {
// 获取视频信息。
FetchVideoCover videoInfo = new FetchVideoCover("c:\\ffmpeg\\bin\\ffmpeg.exe");
try {
videoInfo.getCover("c:\\北京北京.avi","c:\\北京.jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
}
视频和mp3合并
package com.lzh.utils;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class MergeVideoMp3 {
private String ffmpegEXE;
public MergeVideoMp3(String ffmpegEXE) {
super();
this.ffmpegEXE = ffmpegEXE;
}
public void convertor(String videoInputPath, String mp3InputPath,
double seconds, String videoOutputPath) throws Exception {
// ffmpeg.exe -i 苏州大裤衩.mp4 -i bgm.mp3 -t 7 -y 新的视频.mp4
List<String> command = new ArrayList<>();
command.add(ffmpegEXE);
command.add("-i");
command.add(videoInputPath);
command.add("-i");
command.add(mp3InputPath);
command.add("-t");
command.add(String.valueOf(seconds));
command.add("-y");
command.add(videoOutputPath);
for (String c : command) {
System.out.print(c + " ");
}
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader = new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
String line = "";
while ( (line = br.readLine()) != null ) {
}
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
}
public static void main(String[] args) {
MergeVideoMp3 ffmpeg = new MergeVideoMp3("C:\\ffmpeg\\bin\\ffmpeg.exe");
try {
ffmpeg.convertor("C:\\苏州大裤衩.mp4", "C:\\music.mp3", 7.1, "C:\\这是通过java生产的视频.mp4");
} catch (Exception e) {
e.printStackTrace();
}
}
}