window下使用ffmpeg进行读取rtsp数据保存到本地中

目的:读取远程 rtsp的数据保存到本地,生成一个mp4文件。

ffmpeg下载地址:https://ffmpeg.zeranoe.com/builds/

ffmpeg工具:ffmpeg4.0.2,位于解压后的bin/ffmpeg.exe

直接上代码:

 


import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Scanner;

public class TestWinFFRtsp implements Runnable{
	InputStream in ; // 正常输出流
	boolean stop ; // 结束进程
	int maxFrameCount = 2000;
	Process pro ;
	public TestWinFFRtsp(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://184.72.239.149/vod/mp4://BigBuckBunny_175k.mov";
		String dest = "E:/tmp/201810/testff/bbb3.mp4";
		convert(rtspUrl, dest);
		
		System.out.println("###end###");
	}

	public static void convert(String src,String dest)throws Exception{
		String ffcmdpath = "F:\\lee\\apps\\ffmpeg20181005\\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());
		// 输出内容
		TestWinFFRtsp twffIn = new TestWinFFRtsp(process, process.getInputStream());
		TestWinFFRtsp twffInErr = new TestWinFFRtsp(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());
					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("###读取线程结束啦###");
	}
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值