将rtsp协议的网络视频流保存到本地磁盘

  • Post author:
  • Post category:其他


package array;

import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
 
public class Test implements Runnable{
	InputStream in ; // 正常输出流
	boolean stop ; // 结束进程
	int maxFrameCount = 7500;
	Process pro ;
	public Test(Process process, InputStream in ){
		this.in = in;
		this.pro = process;
	}
	public void setStop(boolean stop) {
		this.stop = stop;
	}
	public static void main(String[] args) throws Exception{
		System.out.println("###使用进程的方式进行编码###");
		// 1. 视频转换视频
		  String rtspUrl = "rtsp://admin:88596066Amc@192.168.142.64/MPEG-4/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:\\ffmpeg-20190320-0739d5c-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());
		// 输出内容
		Test twffIn = new Test(process, process.getInputStream());
		Test twffInErr = new Test(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("###读取线程结束啦###");
	}
}



版权声明:本文为yapengliu原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。