您的位置:首页 > 其它

使用FFmpeg如何转发一个RTSP视频流

2017-06-02 17:04 183 查看
版权声明:转载请说明出处:http://www.cnblogs.com/renhui/p/6930221.html

转发RTSP流,这类需求一般出现于转发一些摄像头采集视频,并在摄像头上做RTSPServer,然后通过转发的设备将视频内容转发出去。或者是直接拉取网络上的一些RTSP服务器的内容流,然后进行转发。

如果转发设备是Windows,则需要做的事情,就是在Windows上安装FFmpeg,配置好环境后,直接执行类似下面的命令即可(地址需要替换成你需要的地址):

ffmpeg-irtsp://localhost/live-ccopy-fflvrtmp://server/live/h264Stream


如果需要在Android设备上转发RTSP流,则需要用到JavaCV。相关介绍可以参考:JavaCV初体验

核心逻辑如下:

longstartTimestamp=0;
FrameGrabbergrabber=FFmpegFrameGrabber.createDefault(inputPath);
try{
  grabber.start();
}catch(Exceptione){
try{
  grabber.restart();
}catch(Exceptione1){
throwe;
  }
}
OpenCVFrameConverter.ToIplImageconverter=newOpenCVFrameConverter.ToIplImage();
Framegrabframe=grabber.grab();
IplImagegrabbedImage=null;
if(grabframe!=null){
  Log.e(TAG,"hasfetchedfirstframe");
grabbedImage=converter.convert(grabframe);
}else{
Log.e(TAG,"notfetchedfirstframe");
}
FrameRecorderrecorder=FrameRecorder.createDefault(outputPath,640,360);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);//avcodec.AV_CODEC_ID_H264
recorder.setFormat("flv");
recorder.setFrameRate(25);
recorder.setGopSize(10);try{
  recorder.start();
}catch(FrameRecorder.Exceptione){
  try{
Log.e(TAG,"recorderstartfailed,trytorestartrecorder...");
Log.e(TAG,"closerecorder...");
recorder.stop();//停止录制器的执行状态
Log.e(TAG,"restartrecorder...");
recorder.start();//开启录制器
  }catch(FrameRecorder.Exceptione1){
  throwe;
}
}

Log.e(TAG,"startpushstream");
while((grabframe=grabber.grab())!=null&&push_stream){
  grabbedImage=converter.convert(grabframe);
FramerotatedFrame=converter.convert(grabbedImage);
  if(startTimestamp==0){
  startTimestamp=System.currentTimeMillis();
}
  
recorder.setTimestamp(1000*(System.currentTimeMillis()-startTimestamp));//时间戳
if(rotatedFrame!=null){
  recorder.record(rotatedFrame);
}
}
Log.e(TAG,"hasstoppushstream");
recorder.stop();
recorder.release();
grabber.stop();


最重要的两个对象为:FFmpegFrameGrabber和FrameRecorder,其中FFmpegFrameGrabber负责逐帧解码,FrameRecorder负责逐帧编码。


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐