如需转载请注明出处:https://blog.csdn.net/qq_29350001/article/details/78214267

既然已经可以通过 RTSP 获取h264 裸流了。那么通过 FFmpeg 将其保存到文件中怎么做呢?

一、首先RTSP获取 h264 裸流

我们上面两篇文章主要讲的是通过 rtsp://Your ip:554/stream_chn0.h265 播放H.265视频流。

PS:我刚试了一下,我的 FFmpeg 程序暂时不支持 h265 ...   之前编译的时候,只提供了 x264没有x265

如果感兴趣参看下面两篇文章添加。

参看:使用VS2015添加对ffmpeg添加h265 支持。

参看:ffmpeg 编码H265和H264对比

再结合之前讲的,FFmepg 再学习系列,应该是没问题的。不过好久没有弄了,早忘了了。

那现在没有可以播放的 H.264 视频流了啊,怎么办?

有办法之前讲过一篇文章,参看:LIVE555再学习 -- VLC搭建RTSP服务器(转) 用VLC搭建一个不就完了。

当然还可以直接用 live555,参看:LIVE555再学习 -- live555实现RTSP直播服务器  (推荐)

二、FFmpeg 将H.264 裸流保存到文件

这个也好说,之前有讲到,参看:FFmpeg再学习 -- SDL 环境搭建和视频显示

将其改改就可以了。

具体代码如下:

参看:利用ffmpeg将RTSP传输的h264原始码流保存到文件中

#include "stdafx.h"#include <stdio.h>  #define __STDC_CONSTANT_MACROS  #ifdef _WIN32
//Windows
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "SDL2/SDL.h"
};
#else
//Linux...
#ifdef __cplusplus
extern "C"
{
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <SDL2/SDL.h>
#ifdef __cplusplus
};
#endif
#endif  int main(int argc, char* argv[])
{AVFormatContext *pFormatCtx;int             i, videoindex;AVCodecContext  *pCodecCtx;AVCodec         *pCodec;AVFrame *pFrame, *pFrameYUV;uint8_t *out_buffer;AVPacket *packet;int ret, got_picture;struct SwsContext *img_convert_ctx;// 改成你自己的 URLchar filepath[] = "rtsp://192.168.2.xx:8554/1"; av_register_all();avformat_network_init();pFormatCtx = avformat_alloc_context();if (avformat_open_input(&pFormatCtx, filepath, NULL, NULL) != 0)打开网络流或文件流  {printf("Couldn't open input stream.\n");return -1;}if (avformat_find_stream_info(pFormatCtx, NULL)<0){printf("Couldn't find stream information.\n");return -1;}videoindex = -1;for (i = 0; i<pFormatCtx->nb_streams; i++)if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){videoindex = i;break;}if (videoindex == -1){printf("Didn't find a video stream.\n");return -1;}pCodecCtx = pFormatCtx->streams[videoindex]->codec;pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL){printf("Codec not found.\n");return -1;}if (avcodec_open2(pCodecCtx, pCodec, NULL)<0){printf("Could not open codec.\n");return -1;}pFrame = av_frame_alloc();pFrameYUV = av_frame_alloc();out_buffer = (uint8_t *)av_malloc(avpicture_get_size(PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height));avpicture_fill((AVPicture *)pFrameYUV, out_buffer, PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);//Output Info---输出一些文件(RTSP)信息  printf("---------------- File Information ---------------\n");av_dump_format(pFormatCtx, 0, filepath, 0);printf("-------------------------------------------------\n");img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,pCodecCtx->width, pCodecCtx->height, PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);packet = (AVPacket *)av_malloc(sizeof(AVPacket));FILE *fpSave;if ((fpSave = fopen("geth264.h264", "ab")) == NULL) //h264保存的文件名  return 0;for (;;){//------------------------------  if (av_read_frame(pFormatCtx, packet) >= 0){if (packet->stream_index == videoindex){fwrite(packet->data, 1, packet->size, fpSave);//写数据到文件中  }av_free_packet(packet);}}//--------------  av_frame_free(&pFrameYUV);av_frame_free(&pFrame);avcodec_close(pCodecCtx);avformat_close_input(&pFormatCtx);return 0;
}

调试结果显示如下:

生成 geth264.h264 文件,可播放。

三、工程下载

下载:利用FFmpeg 将 rtsp 获取H264裸流并保存到文件中 工程

思考,这里就有两个问题未完成,一个就是怎么将 H265的裸流保存到文件,再有怎么保存成其他格式比如MP4。

保存到MP4文件代码如下:

#include "stdafx.h"
#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavfilter/avfilter.h>
#include <libavutil/avutil.h>
#include <libswscale/swscale.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifdef __cplusplus
}
#endifAVFormatContext *i_fmt_ctx;
AVStream *i_video_stream;
AVFormatContext *o_fmt_ctx;
AVStream *o_video_stream;int _tmain(int argc, char **argv)
{avcodec_register_all();av_register_all();avformat_network_init();/* should set to NULL so that avformat_open_input() allocate a new one */i_fmt_ctx = NULL;char rtspUrl[] = "rtsp://192.168.2.xx:8554/H264unicast";const char *filename = "1.mp4";if (avformat_open_input(&i_fmt_ctx, rtspUrl, NULL, NULL) != 0){fprintf(stderr, "could not open input file\n");return -1;}if (avformat_find_stream_info(i_fmt_ctx, NULL)<0){fprintf(stderr, "could not find stream info\n");return -1;}//av_dump_format(i_fmt_ctx, 0, argv[1], 0);/* find first video stream */for (unsigned i = 0; i<i_fmt_ctx->nb_streams; i++){if (i_fmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){i_video_stream = i_fmt_ctx->streams[i];break;}}if (i_video_stream == NULL){fprintf(stderr, "didn't find any video stream\n");return -1;}avformat_alloc_output_context2(&o_fmt_ctx, NULL, NULL, filename);/** since all input files are supposed to be identical (framerate, dimension, color format, ...)* we can safely set output codec values from first input file*/o_video_stream = avformat_new_stream(o_fmt_ctx, NULL);{AVCodecContext *c;c = o_video_stream->codec;c->bit_rate = 400000;c->codec_id = i_video_stream->codec->codec_id;c->codec_type = i_video_stream->codec->codec_type;c->time_base.num = i_video_stream->time_base.num;c->time_base.den = i_video_stream->time_base.den;fprintf(stderr, "time_base.num = %d time_base.den = %d\n", c->time_base.num, c->time_base.den);c->width = i_video_stream->codec->width;c->height = i_video_stream->codec->height;c->pix_fmt = i_video_stream->codec->pix_fmt;printf("%d %d %d", c->width, c->height, c->pix_fmt);c->flags = i_video_stream->codec->flags;c->flags |= CODEC_FLAG_GLOBAL_HEADER;c->me_range = i_video_stream->codec->me_range;c->max_qdiff = i_video_stream->codec->max_qdiff;c->qmin = i_video_stream->codec->qmin;c->qmax = i_video_stream->codec->qmax;c->qcompress = i_video_stream->codec->qcompress;}avio_open(&o_fmt_ctx->pb, filename, AVIO_FLAG_WRITE);avformat_write_header(o_fmt_ctx, NULL);int last_pts = 0;int last_dts = 0;int64_t pts, dts;while (1){AVPacket i_pkt;av_init_packet(&i_pkt);i_pkt.size = 0;i_pkt.data = NULL;if (av_read_frame(i_fmt_ctx, &i_pkt) <0)break;/** pts and dts should increase monotonically* pts should be >= dts*/i_pkt.flags |= AV_PKT_FLAG_KEY;pts = i_pkt.pts;i_pkt.pts += last_pts;dts = i_pkt.dts;i_pkt.dts += last_dts;i_pkt.stream_index = 0;//printf("%lld %lld\n", i_pkt.pts, i_pkt.dts);static int num = 1;printf("frame %d\n", num++);av_interleaved_write_frame(o_fmt_ctx, &i_pkt);//av_free_packet(&i_pkt);//av_init_packet(&i_pkt);}last_dts += dts;last_pts += pts;avformat_close_input(&i_fmt_ctx);av_write_trailer(o_fmt_ctx);avcodec_close(o_fmt_ctx->streams[0]->codec);av_freep(&o_fmt_ctx->streams[0]->codec);av_freep(&o_fmt_ctx->streams[0]);avio_close(o_fmt_ctx->pb);av_free(o_fmt_ctx);return 0;
}

如需转载请注明出处:https://blog.csdn.net/qq_29350001/article/details/78214267

RTSP再学习 -- 利用FFmpeg 将 rtsp 获取H264裸流并保存到文件中相关推荐

  1. 音视频开发(17)---RTSP再学习 -- 利用FFmpeg 将 rtsp 获取H264裸流并保存到文件中

    RTSP再学习 -- 利用FFmpeg 将 rtsp 获取H264裸流并保存到文件中 https://blog.csdn.net/qq_29350001/article/details/7821426 ...

  2. ffmpeg实现将H264裸流封装成.mp4或.avi文件

    ffmpeg学习历程 由于我是移植到arm-linux环境(海思HI3521A),H264裸流直接从海思的编码模块VENC获取. H264数据流序列:    SPS, PPS, SEI, I, P, ...

  3. FFmpeg解码H264裸流并转换成opencv Mat

    感谢雷霄骅博士的在中文视频编解码的付出,http://blog.csdn.net/leixiaohua1020 最近要搞一些视频推流的事情,要解析H264裸流并且获取opencv格式的Mat数据给算法 ...

  4. RTSP再学习 -- RTSP协议分析(转载)

    最近一直在看 RTSP,但是RTSP协议是个啥?还没有搞清楚. 首先流媒体百度百科上有这样一段,从基本的名字上或多或少可以理解一下这些传输协议的区别.这很重要!! 传输协议 1.RSVP:资源预留协议 ...

  5. 利用ffmpeg录制rtsp流的方法总结(一)

    致敬雷霄骅(已逝):https://blog.csdn.net/leixiaohua1020/article/details/18893769 音频编码 编码技术 算法 编码标准 码率(kbit/s) ...

  6. RTSP再学习 -- Hi3516A RTSP实例 分析

    上一篇文章,讲到了Hi3516A通过RTSP播放H.265视频流的源码.接下来对源码分析一下. 这里推荐一个工具,参看:日常生活小技巧 -- 文件对比工具 Beyond Compare (1)首先从 ...

  7. 视频知识点(20)- H264码流如何在SPS中获取宽高信息?

    <音视频开发>系列-总览 前沿 了解H264视频编码格式的小伙伴都知道,H264编码中存在两个非常重要的参数集.没错,它们就是序列参数集(SPS)和图像参数集(PPS),而且通常情况下,P ...

  8. 利用ffmpeg 从RTSP流 取rgb图

    一,思路 拉rtsp视频流->解析出yuv frame->转成 rgb  mat 子线程 负责解析 yuc frame,转rgb mat 并存入一个队列 主线程一共一个 取图的接口,调用时 ...

  9. RTSP再学习 -- Hi3516A RTSP实例

    该程序rtsp源码结合 mpp 里 sample_venc 的视频编码样例可以用 VLC 播放 H.265 视频实时流将rtsp_server.h, sample_venc.c 这 2 个文件放到 m ...

最新文章

  1. 【知识星球】模型量化从1bit到8bit,二值到三值
  2. 《 Spring1之第二次站立会议(重发)》
  3. 【深度学习】深度学习的发展方向: 深度强化学习!
  4. loj 2542 随机游走 —— 最值反演+树上期望DP+fmt
  5. C++、python、CUDA性能分析--矩阵乘法
  6. Cython与CPython的区别
  7. ubuntu之Unable to lock the administration directory(/var/lib/dpkg/), are you root?13 Permission denie
  8. Openshift:使用Java 8在Wildfly 8.2.0上构建Spring Boot应用程序
  9. 日历对象导哪个包_微信新表情瞬间炸裂,文物表情包永恒萌呆!
  10. 入门命令5-流程跳转:goto
  11. 仿照MEMZ做一个特效程序
  12. Android-组件化开发
  13. python爬虫-urllib-handler和代理
  14. Can't create pdf file with font calibri bold 错误解决方案
  15. python 矩阵求逆
  16. 华南师范大学计算机学院图论,葛文秀 - 华南师范大学 - 数学科学学院
  17. matlab中的isreal函数,matlab线代 isreal函数检测矩阵的复数元素
  18. 计算机辅助翻译产生的目的,目的论指导下的科技文本汉英翻译实践报告--以《计算机辅助翻译》为例...
  19. 复杂美公司Chain33区块链开发
  20. ECNU计科复试机试(2019)

热门文章

  1. delphi frame 添加 create onshow 事件
  2. Tomcat 9.0.6 HostManager页面 403 Access Denied 错误
  3. Node.js 入门教程 (三):API-准备知识
  4. js操作DOM对象(节点的增删改)
  5. 深度学习代码注解(一)—— mnistdeepauto
  6. mac之brew安装卸载使用
  7. 如何实现在O(n)时间内排序,并且空间复杂度为O(1)
  8. 用c#开发微信 (16) 微活动 2 刮刮卡
  9. 手把手实现YOLOv3(三)
  10. 组会PPT20200910《大工HPT放电结果错误剖析》