package com.media.common;
public class Constants {
public static final String FFMPEG_EXE = "resource/ffmpeg/bin/ffmpeg.exe";
//可以是系统任意位置,测试方便这里对应的是 classpath:
public static final String TEST_FILE_PATH="resource/testFile";
public static final String NEW_FILE_PATH="resource/newFile";
}
package com.media.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 格式转换
* @author zheyi
*
*/
public class FFFormat {
private String ffmpegEXE;
public FFFormat(String ffmpegEXE) {
super();
this.ffmpegEXE = ffmpegEXE;
}
public void ffFormat(String oldFile , String newFile ) throws Exception {
File file = new File(newFile);
if(file.exists()) {
return;
}
if(!file.getParentFile().isDirectory()) {
file.getParentFile().mkdirs();
}
List command = new ArrayList<>();
command.add(ffmpegEXE);
command.add("-i");
command.add(oldFile);
command.add(newFile);
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader=new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
//@SuppressWarnings("unused")
@SuppressWarnings("unused")
String line = "";
while((line=br.readLine())!=null) {
}
if(br!=null) {
br.close();
}
if(inputStreamReader!=null) {
inputStreamReader.close();
}
if(errorStream!=null) {
errorStream.close();
}
}
}
package com.media.utils;
import java.io.BufferedReader;
import java.io.File;
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;
}
/**
*
* @Description: videoInputPath:Mp4资源文件
* @Description: mp3InputPath:Mp3资源文件
* @Description: seconds:截取 mp3 0到N秒 生成mp4背景音乐
* @Description: videoOutputPath:最终文件路径,绝对路径
* @throws Exception
*/
public void convertor(String videoInputPath,String mp3InputPath,double seconds ,String videoOutputPath) throws Exception{
File file = new File(videoOutputPath);
if(file.exists()) {
return;
}
if(!file.getParentFile().isDirectory()) {
file.getParentFile().mkdirs();
}
List 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);
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader=new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
//@SuppressWarnings("unused")
@SuppressWarnings("unused")
String line = "";
while((line=br.readLine())!=null) {
}
if(br!=null) {
br.close();
}
if(inputStreamReader!=null) {
inputStreamReader.close();
}
if(errorStream!=null) {
errorStream.close();
}
}
}
package com.media.utils;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* 音频视频剪辑工具类
*/
public class MontageMP3orMP4 {
private String ffmpegEXE;
public MontageMP3orMP4(String ffmpegEXE) {
super();
this.ffmpegEXE = ffmpegEXE;
}
/**
*
* @Description montageInputPath:源文件路径
* @Description montageStart:开始时间
* @Description montageEnd:结束时间点
* @Description montageOutputPath:新生成文件位置
* @throws Exception:
*/
public void montageMP3orMP4(String montageInputPath,String montageStart,String montageEnd ,String montageOutputPath) throws Exception{
File file = new File(montageOutputPath);
if(file.exists()) {
return;
}
if(!file.getParentFile().isDirectory()) {
file.getParentFile().mkdirs();
}
List command = new ArrayList<>();
command.add(ffmpegEXE);
command.add("-i");
command.add(montageInputPath);
command.add("-vcodec");
command.add("copy");
command.add("-acodec");
command.add("copy");
command.add("-ss");
command.add(montageStart);
command.add("-t");
command.add(montageEnd);
command.add(montageOutputPath);
command.add("-y");
ProcessBuilder builder = new ProcessBuilder(command);
//开始
Process process = builder.start();
InputStream errorStream = process.getErrorStream();
InputStreamReader inputStreamReader=new InputStreamReader(errorStream);
BufferedReader br = new BufferedReader(inputStreamReader);
@SuppressWarnings("unused")
String line = "";
while((line=br.readLine())!=null) {
}
if(br!=null) {
br.close();
}
if(inputStreamReader!=null) {
inputStreamReader.close();
}
if(errorStream!=null) {
errorStream.close();
}
}
}
package com.media.test;
import com.media.common.Constants;
import com.media.utils.FFFormat;
import com.media.utils.MergeVideoMp3;
import com.media.utils.MontageMP3orMP4;
/**
* 测试
* @author zheyi
*
*/
public class MyTest {
public static void main(String[] args) {
//测试前可以将newFile内文件先删掉
MyTest.montageMp3();
MyTest.montageMp4();
MyTest.mergeVideoMp3();
MyTest.fFFormat();
}
protected static synchronized void montageMp3() {
MontageMP3orMP4 ffmpeg = new MontageMP3orMP4(Constants.FFMPEG_EXE);
try {
// 剪辑mp3
// montageInputPath 源文件路径
// montageStart 开始时间
// montageEnd 结束时间点
// montageOutputPath 新生成文件位置
String montageInputPath = Constants.TEST_FILE_PATH + "/Instrumental.mp3";
String montageStart = "00:00:00";
String montageEnd = "00:00:30";
String montageOutputPath = Constants.NEW_FILE_PATH + "/montageMp3.mp3";
ffmpeg.montageMP3orMP4(montageInputPath, montageStart, montageEnd, montageOutputPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected static synchronized void montageMp4() {
MontageMP3orMP4 ffmpeg = new MontageMP3orMP4(Constants.FFMPEG_EXE);
try {
// 剪辑
// montageInputPath 源文件路径
// montageStart 开始时间
// montageEnd 结束时间点
// montageOutputPath 新生成文件位置
String montageInputPath = Constants.TEST_FILE_PATH + "/1544884675206.mp4";
String montageStart = "00:00:05";
String montageEnd = "00:00:10";
String montageOutputPath = Constants.NEW_FILE_PATH + "/montageMp4.mp4";
ffmpeg.montageMP3orMP4(montageInputPath, montageStart, montageEnd, montageOutputPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected static synchronized void mergeVideoMp3() {
MergeVideoMp3 ffmpeg = new MergeVideoMp3(Constants.FFMPEG_EXE);
try {
//合并视频本用例用于简单测试容易出问题
String videoInputPath = Constants.TEST_FILE_PATH + "/1544884675206.mp4";
String mp3InputPath = Constants.TEST_FILE_PATH + "/Instrumental.mp3";
double seconds = 11;
String videoOutputPath = Constants.NEW_FILE_PATH + "/mergeVideoMp3.mp4";
ffmpeg.convertor(videoInputPath, mp3InputPath, seconds, videoOutputPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected static synchronized void fFFormat() {
FFFormat ffmpeg = new FFFormat(Constants.FFMPEG_EXE);
try {
// 格式转换 支持多种格式
// montageInputPath 源文件格式
// montageInputPath 新文件格式
// montageOutputPath 新生成文件位置
String oldFile = Constants.TEST_FILE_PATH + "/1544884675206.mp4";
String newFile = Constants.NEW_FILE_PATH + "/new1544884675206.avi";
ffmpeg.ffFormat(oldFile, newFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}