当然,也是从网上下载的。经过一番修改编译,运行结果正确。

  由于dts/pts,有的播放器播放速度有所差异。

  这里就直接就共享出来(也可以去下载区下载)。

  • 头文件
#ifndef __GH_RTSP_2_MP4_H__
#define __GH_RTSP_2_MP4_H__#include <stdio.h>
#include <stdlib.h>
#include <stdint.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
}
#endif#endif
  • C源码
#include <string.h>
#include <pthread.h>
#include <unistd.h>#include "gh_rtsp2mp4.h"#define RTSP "rtsp://admin:quantum7@192.168.1.144"//经过实验,这个值最好
#define PTS_VALUE 4500
#define MAX_FRAMES 300static bool g_RunningFlag = true;static int rtsp2mp4(const char* pInputFileName, const char* pOutputFileName)
{AVOutputFormat *ofmt = NULL;//Input AVFormatContext and Output AVFormatContextAVFormatContext *ifmt_ctx = NULL, *ofmt_ctx = NULL;AVPacket pkt;int ret=0, i=0;int video_index = -1;int frame_index = 0;int dts = 0;int pts = 0;av_register_all();//Networkavformat_network_init();//Inputif ((ret = avformat_open_input(&ifmt_ctx, pInputFileName, 0, 0)) < 0){printf("Could not open input file.");goto end;}if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0){printf("Failed to retrieve input stream information");goto end;}for (i = 0; i<ifmt_ctx->nb_streams; i++){if (ifmt_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){video_index = i;break;}}av_dump_format(ifmt_ctx, 0, pInputFileName, 0);//Outputavformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, pOutputFileName); //RTMPif (!ofmt_ctx){printf("Could not create output context\n");ret = AVERROR_UNKNOWN;goto end;}ofmt = ofmt_ctx->oformat;for (i = 0; i < ifmt_ctx->nb_streams; i++){//Create output AVStream according to input AVStreamAVStream *in_stream  = ifmt_ctx->streams[i];AVStream *out_stream = avformat_new_stream(ofmt_ctx, in_stream->codec->codec);if (!out_stream){printf("Failed allocating output stream\n");ret = AVERROR_UNKNOWN;goto end;}//Copy the settings of AVCodecContextret = avcodec_copy_context(out_stream->codec, in_stream->codec);if (ret < 0){printf("Failed to copy context from input to output stream codec context\n");goto end;}out_stream->codec->codec_tag = 0;if (ofmt_ctx->oformat->flags & AVFMT_GLOBALHEADER){out_stream->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;}}//Dump Format------------------av_dump_format(ofmt_ctx, 0, pOutputFileName, 1);//Open output URLif (!(ofmt->flags & AVFMT_NOFILE)){ret = avio_open(&ofmt_ctx->pb, pOutputFileName, AVIO_FLAG_WRITE);if (ret < 0){printf("Could not open output URL '%s'", pOutputFileName);goto end;}}//Write file headerret = avformat_write_header(ofmt_ctx, NULL);if (ret < 0){printf("Error occurred when opening output URL\n");goto end;}#if USE_H264BSFAVBitStreamFilterContext* h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");#endifwhile (g_RunningFlag){AVStream *in_stream, *out_stream;//Get an AVPacketret = av_read_frame(ifmt_ctx, &pkt);if (ret < 0){break;}in_stream  = ifmt_ctx->streams[pkt.stream_index];out_stream = ofmt_ctx->streams[pkt.stream_index];//Convert PTS/DTS
#if 1if (pts == 0){pts = pkt.pts;dts = pkt.dts;}else{pkt.pts += pts;pkt.dts += dts;}if (pkt.dts < pkt.pts){pkt.dts = pkt.pts;}
#elsepkt.pts += 4500*frame_index;pkt.dts += 4500*frame_index;
#endifpkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);pkt.pos = -1;if (pkt.stream_index == video_index){//printf("Receive %8d video frames from RTSP\n", frame_index);frame_index++;#if USE_H264BSFav_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);#endif}ret = av_interleaved_write_frame(ofmt_ctx, &pkt);if (ret < 0 && frame_index > 3){printf("Error muxing packet\n");break;}av_free_packet(&pkt);if (frame_index > MAX_FRAMES){break;}}#if USE_H264BSFav_bitstream_filter_close(h264bsfc);#endif//Write file trailerav_write_trailer(ofmt_ctx);end:avformat_close_input(&ifmt_ctx);/* close output */if (ofmt_ctx && !(ofmt->flags & AVFMT_NOFILE)){avio_close(ofmt_ctx->pb);}avformat_free_context(ofmt_ctx);if (ret < 0 && ret != AVERROR_EOF){printf("Error occurred.\n");return -1;}return 0;
}int main(int argc, char **argv)
{rtsp2mp4(RTSP, "rtsp.mp4");return 0;
}
  • Makefile

APP:= gh_rtsp2mp4SRCS:= gh_rtsp2mp4.cppINCS:= $(wildcard *.h)OBJS:= $(SRCS:.cpp=.o)#GST
CPPFLAGS+=  -DGST_FLAG \-I/usr/local/includeLIBS := -L /usr/lib/x86_64-linux-gnu -lavcodec -lavutil -lswresample -lavformat \-L /usr/linclude -lz -llzma -lbz2# -L /usr/local/lib -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui \LIBS += $(GLLINK)
# LIBS += -lGL -lGLU -lglut#LIBS+= `pkg-config --libs $(PKGS)`all: $(APP).o: .cpp $(INCS) Makefileg++ -g -c $@ $(CPPFLAGS) $<$(APP): $(OBJS) Makefileg++ -g -o $(APP) $(OBJS) $(LIBS)clean:rm -rf $(OBJS) $(APP)

可编译运行:调用ffmpeg接口,将RTSP流保存为MP4的C代码相关推荐

  1. 将RTSP流保存为本地TS文件

    1.功能:将RTSP流保存为本地TS文件 2.存在问题: 保存mp4文件播放不了,还未解决-希望路过的大佬帮忙瞅瞅 _ 3.流程: 0)初始化:并注册所有的解封装器.封装器和协议,初始化网络库: 1) ...

  2. java调用ffmpeg把rtsp视频流保存为MP4文件

    前言:最近需要把rtsp的视频流保存为MP4文件(就是录制直播流).刚开始用的javacv的FFmpegFrameGrabber和FFmpegFrameRecorder,但是声音流和视频流无法调整,声 ...

  3. C# 调用ffmpeg把rtsp视频流保存为MP4文件

    在C#中 使用Process 执行ffmpeg命令把RTSP保存为MP4 Task.Run(() => {string ffmpegPath = @"J:\ffmpeg\ffmpeg. ...

  4. ffmpeg抓取rtsp流并保存_详细解析RTSP框架和数据包分析(1)

    0.引言 本文主要讲解RTSP框架和抓取RTSP数据包,进行详细分析.可以阅读以下几篇文章,能够帮助你更详细理解. 手把手搭建RTSP流媒体服务器 HLS实战之Wireshark抓包分析 HTTP实战 ...

  5. ffmpeg 硬件解码rtsp流_树莓派使用硬件加速视频转码

    现在随着智能设备普及以及宽带的升级,越来越的的视频素材在不断的产生.无论是我们自己拍摄的视频,还是从网上收集来的电影.电视剧,并不是全部都值得我们保存最高清的版本.打个比方,比如你下载了一个 1080 ...

  6. ffmpeg推送rtsp流或者视频文件到rtsp服务器

    1.推送rtsp视频流 ffmpeg -i rtsp://admin:admin1234@192.168.1.64:554/h264/ch1/sub/av_stream -codec copy -f ...

  7. ffplay flv mp4 转_C#调用FFmpeg将flv视频格式转换成mp4格式

    代码分享:using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; usin ...

  8. yolov3 python接口_darknetyolov3调用Python接口来检测和保存批处理图像,DarknetYoloV3,python,进行,批量,图片,并...

    将主函数替换即可. # 将检测结果绘制到照片上并且保存 if __name__ == "__main__": #os.environ["CUDA_VISIBLE_DEVI ...

  9. ffmpeg + opencv 把摄像头画面保存为mp4文件

    根据 https://stackoverflow.com/questions/46444474/c-ffmpeg-create-mp4-file 修改 运行环境为Ubuntu 20.10 #inclu ...

  10. Android编译调用FFmpeg API,自己写方法,编译so库

    作者:谭东 时间:2017年9月19日 环境:Windows 8.1专业版 NDK版本:android-ndk-r14b FFmpeg版本:FFmpeg 3.0.2 "Einstein&qu ...

最新文章

  1. centos ezhttp mysql_CentOS安装mysq
  2. 预告:Javascript全栈开发的系列文章
  3. JBOSS配置系统应用的端口号
  4. (50)补充内容:SSDT HOOK 模板
  5. 类文件结构-method-init和类文件结构-method-main
  6. python中xpath_Python爬虫之Xpath语法
  7. spring boot访问templates目录下的html静态页面
  8. HashSet 和 TreeSet 及 Map 集合的简单方法,含程序。
  9. mysql varchar 效率_由MySQL中char和varchar效率想到的
  10. 20.校准相机——介绍,使用已知点进行校正,直接线性校准均匀性第1部分,直接线性校准均匀性第2部分_1
  11. Tomcat报错 严重: A child container failed during start
  12. 华为matepadpro可以用云电脑吗_放弃台式机,改用华为云电脑,再手机投屏到显示器,这样可行吗?...
  13. 布谷鸟算法浅谈与简单应用
  14. centos7.4 编译 找不到 -lGL
  15. C#等语言、反射机制实现简单 智能语音人机交互(2)-- 皓月
  16. cad lisp 二次抛物线_用Cad画二次抛物线
  17. (1)Proteus 8.9上的STM32仿真系列(HAL)---流水灯
  18. sql server数据库错误数据恢复(数据库连接失效,无法附加查询)
  19. docxtpl 学习笔记
  20. PXE实现批量安装部署(win与linux系统)

热门文章

  1. 关于启动PPT时,出现错误对话框的问题的解决.
  2. nginx location总结
  3. 文件上传到部署服务器(添加附件)
  4. 在 Ubuntu 中用 UFW 配置防火墙
  5. ajax+springmvc返回中文乱码的解决办法
  6. 用react native 做的一个推酷客户端
  7. 《Javascript秘密花园》学习笔记(下)
  8. ASCII、Unicode、GBK和UTF-8字符编码的区别联系[转]
  9. 听飞狐聊JavaScript设计模式系列12
  10. jetty快速入门与嵌入使用 jetty