FFMpeg的下载地址
https://download.csdn.net/download/qq_41840735/15647707
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FFMpegUtil {
public static void main(String[] args) {
FFMpegUtil ffmpeg = new FFMpegUtil();
try {
ffmpeg.convertor("F:\\示例\\视频\\11.mp4", "F:\\示例\\视频\\videoOutputPath1\\test2.m3u8");
} catch (Exception e) {
e.printStackTrace();
}
}
//ffmpeg.exe的安装路径呀
private String ffmpegEXE = "C:\\Program Files\\ffmpeg\\bin\\ffmpeg.exe";
public FFMpegUtil() {
super();
}
/**
* mp4视频转m3u8
* @param videoInputPath
* @param videoOutputPath
* @return
* @throws Exception
*/
public Boolean convertor(String videoInputPath, String videoOutputPath) throws Exception {
List<String> command = new ArrayList<>();
command.add(ffmpegEXE);
command.add("-i");
command.add(videoInputPath);
//强制ts切割
command.add("-force_key_frames");
command.add("\"expr:gte(t,n_forced*1)\"");
command.add("-profile:v");
command.add("baseline");
command.add("-level");
//
command.add("3.0");
command.add("-s");
//分辨率
command.add("640x360");
command.add("-start_number");
command.add("0");
//ts的每段片长
command.add("-hls_time");
command.add("2");
command.add("-hls_list_size");
command.add("0");
command.add("-f");
command.add("hls");
command.add(videoOutputPath);
System.out.println("这是command:"+command);
for (String c : command) {
}
InputStream errorStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader br = null;
Boolean result = false;
try {
ProcessBuilder builder = new ProcessBuilder(command);
Process process = builder.start();
errorStream = process.getErrorStream();
inputStreamReader = new InputStreamReader(errorStream);
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();
}
result = true;
}catch (Exception e){
e.printStackTrace();
result = false;
}finally {
if (br != null) {
br.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (errorStream != null) {
errorStream.close();
}
return result;
}
}
/**
* 视频截图功能
* @param sourceVideoPath 需要被截图的视频路径(包含文件名和后缀名)
* @param targetParth 图片存储路径
* @return
*/
public boolean processImg(String sourceVideoPath, String targetParth) {
//先确保保存截图的文件夹存在
File TempFile = new File(targetParth);
if (TempFile.exists()) {
if (TempFile.isDirectory()) {
System.out.println("该文件夹存在。");
}else {
System.out.println("同名的文件存在,不能创建文件夹。");
}
}else {
System.out.println("文件夹不存在,创建该文件夹。");
TempFile.mkdir();
}
File fi = new File(sourceVideoPath);
String filename = fi.getName(); //获取视频文件的名称。
String filerealname = filename.substring(0, filename.lastIndexOf(".")); //获取视频名+不加后缀名 后面加.toLowerCase()转为小写
List<String> commend = new ArrayList<String>();
//第一帧: 00:00:01
//截图命令:time ffmpeg -ss 00:00:01 -i test1.flv -f image2 -y test1.jpg
commend.add(ffmpegEXE); //指定ffmpeg工具的路径
commend.add("-ss");
commend.add("00:00:01"); //1是代表第1秒的时候截图
commend.add("-i");
commend.add(sourceVideoPath); //截图的视频路径
commend.add("-f");
commend.add("image2");
commend.add("-y");
commend.add(targetParth + filerealname + ".jpg"); //生成截图xxx.jpg
//打印截图命令--zoutao
StringBuffer test = new StringBuffer();
for (int i = 0; i < commend.size(); i++) {
test.append(commend.get(i) + " ");
}
System.out.println("截图命令:"+test);
try {
/*ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p =builder.start();*/
//调用线程处理命令
ProcessBuilder builder = new ProcessBuilder();
builder.command(commend);
Process p = builder.start();
//获取进程的标准输入流
final InputStream is1 = p.getInputStream();
//获取进程的错误流
final InputStream is2 = p.getErrorStream();
//启动两个线程,一个线程负责读标准输出流,另一个负责读标准错误流
new Thread() {
public void run() {
BufferedReader br = new BufferedReader(
new InputStreamReader(is1));
try {
String lineB = null;
while ((lineB = br.readLine()) != null) {
if (lineB != null){
//System.out.println(lineB); //必须取走线程信息避免堵塞
}
}
} catch (IOException e) {
e.printStackTrace();
}
//关闭流
finally{
try {
is1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
new Thread() {
public void run() {
BufferedReader br2 = new BufferedReader(
new InputStreamReader(is2));
try {
String lineC = null;
while ((lineC = br2.readLine()) != null) {
if (lineC != null) {
//System.out.println(lineC); //必须取走线程信息避免堵塞
}
}
} catch (IOException e) {
e.printStackTrace();
}
//关闭流
finally{
try {
is2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}.start();
// 等Mencoder进程转换结束,再调用ffmepg进程非常重要!!!
p.waitFor();
System.out.println("截图进程结束");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}