有些可简单命令实现的功能可以调起一条系统命令搞定,无需写太多复杂代码。
下面是一个很简单的demo,参数:流地址或者文件地址,录制多少秒
FileUtil.judeDirExists:检查文件夹没有创建,网上一大堆自己找代码
DateUtils.dateToStr:日期格式转换,可以换个别的地址,想跑demo自己写个日期工具。
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.javacv.FrameGrabber.Exception;
public class RecordMedia {
public static final String yyyyMMdd = "yyyyMMdd";
public static final String Suffix = ".";
public static final int cutterCount = 10;
public static final String timeout = "10000000";
public static final String FitterFormat = "mp4";
public static final String M3u8Format = "ts";
public static String record(String input,int recodeDuration)
throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {
StringBuffer output = new StringBuffer();
output.append("./media/").append(DateUtils.dateToStr(new Date(), yyyyMMdd)).append("/record").append("/");
String outputPath = output.toString();
FileUtil.judeDirExists(new File(outputPath));
String outputFile = outputPath +System.currentTimeMillis()+ Suffix + FitterFormat;
String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
ProcessBuilder pb = new ProcessBuilder(ffmpeg, "-i", input, "-t", String.valueOf(recodeDuration), "-c", "copy",
outputFile);
try {
pb.inheritIO().start().waitFor();
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return outputFile;
}
public static void main(String[] args) throws InterruptedException {
try {
record("http://223.110.245.159/ott.js.chinamobile.com/PLTV/3/224/3221225838/index.m3u8",10);
} catch (Exception | org.bytedeco.javacv.FrameRecorder.Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}