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<String> 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;
}
/**
*
* @De ion: videoInputPath:Mp4资源文件
* @De ion: mp3InputPath:Mp3资源文件
* @De ion: seconds:截取 mp3 0到N秒 生成mp4背景音乐
* @De ion: 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<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);
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;
}
/**
*
* @De ion montageInputPath:源文件路径
* @De ion montageStart:开始时间
* @De ion montageEnd:结束时间点
* @De ion 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<String> 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();
}
}
}
继续阅读与本文标签相同的文章
上一篇 :
比特币离你很近-比特币地址及生成
-
汇编(四)字的存储、DS和[address]、字的传送、mov、add、sub指令、数据段
2026-05-19栏目: 教程
-
elasticsearch之索引管理API(Index management)
2026-05-19栏目: 教程
-
简单介绍几种Java后台开发常用框架组合
2026-05-19栏目: 教程
-
<丰田发布了LQ EV概念车>。丰田全新的概念车配备了AI代理和自动驾驶功能,这是丰田美国公司研究员开发的,首次的公开亮相将在本月23日。在2017年CES消费车展上丰田曾展示了 Concept-Ai i概念车
2026-05-19栏目: 教程
-
Sysweld笔记:利用稳态算法加速算法模拟焊接过程的残余应力
2026-05-19栏目: 教程
