javaCV将socket获取的视频流推到流媒体(RTMP)服务器

参考链接:http://blog.csdn.net/eguid_1/article/details/52678775

相信大家对JAVACV做了些了解,我在这话也不多说了,直接上代码:

public class Client1 {


/**
* Socket客户端
*/
public static void main(String[] args) throws Exception {
try {
int count=0;
while(true){
// "GET /ipcam/jpeg.cgi HTTP/1.1\r\n\r\nAuthorization: Basic YWRtaW46OTk5OQ====\r\n\r\n"
// 创建Socket对象
Socket socket = new Socket("192.168.0.80", 8080);
// 根据输入输出流和服务端连接
OutputStream outputStream = socket.getOutputStream();// 获取一个输出流,向服务端发送信息
// "GET /ipcam/jpeg.cgi HTTP/1.1\r\nAuthorization: Basic YWRtaW46OTk5OQ====\r\n\r\n"
outputStream
.write("GET /ipcam/avc.cgi HTTP/1.1\r\nAuthorization: Basic YWRtaW46OTk5OQ====\r\n\r\n"
.getBytes());
outputStream.flush();
socket.shutdownOutput();// 关闭输出流
InputStream inputStream = socket.getInputStream();// 获取一个输入流,接收服务端的信息
String outputFile = "rtmp://localhost:1935/live/test2";
Player.frameRecord(inputStream, outputFile, 1);
count++;
System.out.println("================================"+count+"次数");
socket.close();
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
}

public class Player {


/** 
     * 按帧录制视频 
     *  
     * @param inputFile-该地址可以是网络直播/录播地址,也可以是远程/本地文件路径 
     * @param outputFile 
     *            -该地址只能是文件地址,如果使用该方法推送流媒体服务器会报错,原因是没有设置编码格式 
     * @throws FrameGrabber.Exception 
     * @throws FrameRecorder.Exception 
     * @throws org.bytedeco.javacv.FrameRecorder.Exception 
     */  
    public static void frameRecord(InputStream inputFile, String outputFile, int audioChannel)  
            throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {
            boolean isStart=true;//该变量建议设置为全局控制变量,用于控制录制结束  
        // 获取视频源  
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile); 
        grabber.setOption("rtsp_transport", "tcp");
        // 流媒体输出地址,分辨率(长,高),是否录制音频(0:不录制/1:录制)  
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, 1280, 720, audioChannel); 
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
        recorder.setFormat("flv");
        // 开始取视频源  
        recordByFrame(grabber, recorder, isStart);
    }

 private static void recordByFrame(FFmpegFrameGrabber grabber, FFmpegFrameRecorder recorder, Boolean status)  
            throws Exception, org.bytedeco.javacv.FrameRecorder.Exception {  
        try {//建议在线程中使用该方法  
            grabber.start();  
            recorder.start();  
            Frame frame =null ;  
            while (status&& (frame = grabber.grabFrame()) != null) {  
                recorder.record(frame);  
            }  
            recorder.stop();  
            grabber.stop();  
        } finally {  
            if (grabber != null) {  
                grabber.stop();  
            }  
        }  
    }    
}


JAR下载链接:http://download.csdn.net/download/xiaojie11800/10202284

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值