完整的测试代码如下:(保存至本地但不能播放)
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class Test1 implements Runnable {
InputStream in ; // 正常输出流
boolean stop ; // 结束进程
int maxFrameCount = 7500;
Process pro ;
public Test1(Process pro,InputStream in ) {
this.pro = pro;
this.in=in;
}
public boolean isStop() {
return stop;
}
public void setStop(boolean stop) {
this.stop = stop;
}
public int getMaxFrameCount() {
return maxFrameCount;
}
public void setMaxFrameCount(int maxFrameCount) {
this.maxFrameCount = maxFrameCount;
}
public static void main(String[] args)throws Exception{
System.out.println("###使用进程的方式进行编码###");
// 1. 视频转换视频
String rtspUrl = "rtsp://wangzhi:1qaz2wsx@192.168.104.251/h264/ch1/main/av_stream";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
Date now = new Date();
Date afterDate = new Date(now .getTime() + 300000);
String dest = "D:/"+sdf.format(now)+"—"+sdf.format(afterDate)+".mp4";
System.out.println("+++++++++++++++++++"+dest);
convert(rtspUrl, dest);
System.out.println("###end###");
}
public static void convert(String src,String dest)throws Exception{
String ffcmdpath = "D:\\download\\ffmpeg-4.2.2-win64-static\\bin\\ffmpeg.exe";
StringBuilder cmd = new StringBuilder();
cmd.append(ffcmdpath)
.append(" -rtsp_transport tcp ") // 使用tcp的命令,默认是udp
.append(" -i ").append(src)
// .append(" -vcodec copy ")
.append(" -vcodec h264 ")
// .append(" -acodec copy ") // 音频,不设置好像也有。
// .append(" -s 1280*720 ") // 设置分辨率,关系到视频的大小或者为 -vf scale=iw/2:ih/2
// .append(" -vf scale=iw/2:ih/2 ")
.append(" -y ") // 覆盖
.append(dest);
System.out.println("cmd="+cmd.toString());
System.out.println("###start cmd="+new Date().toLocaleString());
Process process = Runtime.getRuntime().exec(cmd.toString());
// 输出内容
Test1 twffIn = new Test1(process, process.getInputStream());
Test1 twffInErr = new Test1(process,process.getErrorStream());
Thread t = new Thread(twffIn);
Thread t1 = new Thread(twffInErr);
t.start();t1.start();
// 停止指令,10秒后停止 ,一定要发送这个,要不然视频保存不下来
//
int i = process.waitFor(); // 一定要配合2个 inputstream ,要不然会一直阻塞
System.out.println(i+"###end cmd="+new Date().toLocaleString());
twffIn.setStop(true);twffInErr.setStop(true); // 停止 线程
}
/**
* @title stopConvert
* @date 2018年10月11日
* @param process
* @return
* @description 发送停止命令,
* 但是要注意发送完成后,不一定会马上停止 。因为进程结束也是需要时间的
*/
public static boolean stopConvert(Process process ){
System.out.println("###send q cmd ");
try{
OutputStream os = process.getOutputStream();
os.write("q".getBytes());
os.flush(); // 一定要刷新
}catch(Exception err){
err.printStackTrace();
return false;
}
return true;
}
@Override
public void run() {
Scanner scanner = new Scanner(in);
while(!stop){
if(scanner.hasNext()){
String s = scanner.nextLine();
System.out.println(s);
// 判断停止录像的条件
if(s.startsWith("frame=") && s.indexOf("fps=") > 6){
String frameCountStr = s.substring(6
,s.indexOf("fps="));
// System.out.println(s.indexOf("fps=") + ",frameCountStr="+frameCountStr);
// 获得当前解析到的帧数,退出
int frameCount = Integer.parseInt(frameCountStr.trim());
System.out.println("======================"+frameCount);
if(frameCount >= maxFrameCount){
System.out.println("##maxFrameCount="+maxFrameCount +",frameCount="+frameCount);
stopConvert(pro);
}
}
}else{
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
scanner.close();
System.out.println("###读取线程结束啦###");
}
}
这个转换格式问题应该怎么解决,求大神解疑.
转载:https://blog.csdn.net/weixin_45435366/article/details/104415803
查看评论