当前位置:网站首页>ffmpeg通过rtsp获取h264码流
ffmpeg通过rtsp获取h264码流
2022-08-09 15:43:00 【weixin_45090728】
ffmpeg通过rtsp获取h264码流
处理过程
通过VLC播放器将本地文件进行rtsp推流,使用ffmpeg打开URL地址获取h264。
VLC rtsp推流
点击“媒体”,选择“流”
点击“添加”选择需要推流的文件,然后点击下面的“串流”
点击“下一步”,选择“RTSP",再点击”添加“,设置端口号和名称,默认的ip是电脑本机ip
点击”下一个“,设置推流格式为h264
然后点击“下一个”,点击“流”后即可推流
测试的话,可以使用VLC媒体中的打开网络串流进行播放,若能成功播放则推流成功。
ffmpeg获取rtsp
获取其实很简单的,只给出代码,packet结构体中的data和size就是一帧h264数据(头部包含NALU)
extern "C"{
#include <stdio.h>
#include <signal.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavutil/imgutils.h>
}
static volatile int flag_quit=0;
void signal_quit_handler(int sig)
{
flag_quit=1;
signal(SIGINT,SIG_DFL);
}
int rtsp_get_h264_test()
{
signal(SIGINT,signal_quit_handler);
AVFormatContext* format_context;
int video_index;
AVPacket *packet;
char file_path[]="rtsp://192.168.1.7:8554/live";
avformat_network_init();
format_context=avformat_alloc_context();
AVDictionary* options=NULL;
av_dict_set(&options,"buffer_size","102400",0);
av_dict_set(&options,"max_delay","500000",0);
av_dict_set(&options,"timeout","20000000",0);
av_dict_set(&options,"rtsp_transport","udp",0);
if(avformat_open_input(&format_context,file_path,NULL,&options)<0)
{
printf("Couldn't open input stream.\n");
return -1;
}
printf("Open stream success!\n");
if(avformat_find_stream_info(format_context,NULL)<0)
{
printf("Couldn't find stream information.\n");
return -1;
}
video_index=-1;
video_index=av_find_best_stream(format_context,AVMEDIA_TYPE_VIDEO,-1,-1,NULL,0);
if(video_index==-1)
{
printf("Couldn't find a stream video.\n");
return -1;
}
packet=av_packet_alloc();
while(!flag_quit)
{
if(av_read_frame(format_context,packet)>=0)
{
if(packet->stream_index==video_index){
printf("packet size %d\n",packet->size);
for(int i=0;i<8;i++)
{
printf("%d ",packet->data[i]);
}
printf("\n");
}
}
}
av_packet_free(&packet);
avformat_close_input(&format_context);
return 0;
}
int main(int argc, char **argv) {
printf("printf ffmpeg configuration: ");
printf("%s\n", avcodec_configuration());
rtsp_get_h264_test();
return 0;
}
其它
- 使用tcp打开的话会提示不支持
- 打开流后结束程序时最好关闭,不然会影响下一次的使用
边栏推荐
猜你喜欢
随机推荐
ESP8266-Arduino编程实例-MQ-5液化天然气传感器驱动
STM32课设-智能物联网家居系统(UCOSIII+STEMWIN)
@AllArgsConstructor 和 @NoArgsConstructor
【燃】是时候展现真正的实力了!一文看懂2022华为开发者大赛技术亮点
2022钉钉杯A题思路及代码:银行卡电信诈骗危险预测
5. Visualizing Geospatial Data
网络——数字数据编码
Swagger2 knife4j NullPointerException 空指针问题
Apple Developer Account Apply for D-U-N-S Number
uniapp 项目搭建
Codeforces Round # 806 (Div. 4) | | precipitation) bloodbath wudaokou
【服务器数据恢复】SAN LUN映射出错导致文件系统数据丢失的数据恢复案例
分布式恢复【进阶篇】
Foreword: About the author Dr. Wu Qiusheng and an introduction to the book
shopee引流方式有哪些,商家如何为自己店铺做引流?
August 9, 2022: Build .NET apps in C# -- use the Visual Studio Code debugger to interactively debug .NET apps (won't, fail)
margin:auto实现盒子水平垂直居中
Optimization of a piece of JDBC code (Part 1)
2022年深圳杯数学建模A题代码思路-- 破除“尖叫效应”与“回声室效应”,走出“信息茧房”
Knowledge Bits - How to Write a Project Summary