package com.example.mediodownload.utils;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
*/
public class Ma4FmpegByTs {
public static void main(String[] args) {
String sourceFile = "D:\\chicz_m3u8\\03.mp4";
String destFile = "D:\\chicz_m3u8\\03.ts";
String m3u8File = "D:\\chicz_m3u8\\index.m3u8";
String tsFile = "D:\\chicz_m3u8\\index_%04d.ts";// 4个占位符
Integer volume = 10;// 长度
String osName = System.getProperty("os.name");
URL resource;
// 根据系统名称获取ffmpeg,classpath下的路径
if (osName.toLowerCase().contains("windows")) {
resource = Ma4FmpegByTs.class.getResource("/ffmpeg/bin/ffmpeg.exe");
} else {
resource = Ma4FmpegByTs.class.getResource("/ffmpeg/bin/ffmpeg");
}
// 获取执行命令,音量和比特率可以不传
List<String> toTs = getFfmpegCommand(resource.getPath(), sourceFile, destFile);
// 获取执行命令,音量和比特率可以不传
List<String> toMoreTs = getFfmpegCommand(resource.getPath(), destFile, m3u8File, volume, tsFile);
List<String> mp4ToMoreTs = getFfmpegCommand(resource.getPath(), sourceFile, destFile, m3u8File, volume, tsFile);
// 执行转换
try {
boolean process = process(mp4ToMoreTs);
System.out.println(process);
// process = process(toMoreTs);
// System.out.println(process);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* mp4文件切片为多个ts文件
*
* @param ffmpegPath
* @param sourceFile
* @param destFile
* @param m3u8File
* @param volume
* @param tsFile
* @return
*/
private static List<String> getFfmpegCommand(String ffmpegPath, String sourceFile, String destFile, String m3u8File, Integer volume, String tsFile) {
List<String> command = new ArrayList<String>();
command.add(ffmpegPath);
command.add("-i");
command.add(sourceFile);
command.add("-f");
command.add("segment");
command.add("-segment_time");
command.add(volume + "");
command.add("-segment_format");
command.add("mpegts");
command.add("-segment_list");
command.add(m3u8File);
command.add("-c");
command.add("copy");
command.add("-bsf:v");
command.add("h264_mp4toannexb");
command.add("-map");
command.add("0");
command.add(tsFile);
return command;
}
/**
* mp4文件转ts文件
*
* @param ffmpegPath
* @param sourceFile
* @param destFile
* @return
*/
private static List<String> getFfmpegCommand(String ffmpegPath, String sourceFile, String destFile) {
List<String> command = new ArrayList<String>();
command.add(ffmpegPath);
command.add("-i");
command.add(sourceFile);
command.add("-vcodec");
command.add("copy");
command.add("-acodec");
command.add("copy");
command.add("-vbsf");
command.add("h264_mp4toannexb");
command.add(destFile);
return command;
}
/**
* ts文件切片为多个ts文件
*
* @param ffmpegPath
* @param destFile
* @param m3u8File
* @param volume
* @param tsFile
* @return
*/
private static List<String> getFfmpegCommand(String ffmpegPath, String destFile, String m3u8File, Integer volume, String tsFile) {
List<String> command = new ArrayList<String>();
command.add(ffmpegPath);
command.add("-i");
command.add(destFile);
command.add("-c");
command.add("copy");
command.add("-map");
command.add("0");
command.add("-f");
command.add("segment");
command.add("-segment_list");
command.add(m3u8File);
command.add("-segment_time");
command.add(volume + "");
command.add(tsFile);
return command;
}
/**
*
* @param ffmpegCommand
* @return
*/
private static boolean process(List<String> ffmpegCommand) throws Exception {
try {
if (null == ffmpegCommand || ffmpegCommand.size() == 0) {
return false;
}
Process videoProcess = new ProcessBuilder(ffmpegCommand).redirectErrorStream(true).start();
new PrintStream(videoProcess.getErrorStream()).start();
new PrintStream(videoProcess.getInputStream()).start();
int exitCode = videoProcess.waitFor();
if (exitCode == 1) {
return false;
}
return true;
} catch (Exception e) {
throw new Exception("file uploadfailed", e);
}
}
/**
* 文件读取
*/
private static class PrintStream extends Thread {
InputStream inputStream = null;
PrintStream(InputStream is) {
inputStream = is;
}
@Override
public void run() {
try {
while (this != null) {
int ch = inputStream.read();
if (ch != -1) {
System.out.print((char) ch);
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}