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

    利用ffmpeg将RTSP传输的h264原始码流保存到文件中,没有做任何处理,直接将h264码流保存到文件中。

    其中测试的RTSP地址是网上公开的一个 rtsp流媒体测试地址

下面是程序:

/**
*作者:HJL
*最后更新:2015.7.18
*利用ffmpeg将RTSP传输的h264原始码流保存到文件中
*未加任何效果,不显示
**/


#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;
	//下面是公共的RTSP测试地址
	char filepath[]="rtsp://218.204.223.237:554/live/1/0547424F573B085C/gsfp90ef4k0a6iap.sdp";

	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;
}

控制台打印的信息如下:



最后保存的h264码流的文件用h264播放器打开后如下:




整个VS2012工程文件可以在这里下载。


参考文章:http://blog.csdn.net/leixiaohua1020/article/details/38868499






FFmpeg是一个非常强大的开源工具集,用于处理音视频数据,包括解码、编码、转码、处理、过滤等。RTSP(Real Time Streaming Protocol)是一种网络媒体传输控制协议,常用于控制媒体服务器上的媒体数据的回放。H264是一种视频编码标准,广泛应用于视频压缩,由于其高压缩比和良好的视频质量,被广泛用于直播和视频存储。 使用FFmpeg进行RTSPH264,通常是将摄像头或其他视频源的实时视频数据编码成H264格式,并通过RTSP协议传输媒体服务器或者接收端。这个过程可以简单分为以下几个步骤: 1. 捕获视频源:首先,需要使用FFmpeg的视频捕获功能,可以是通过摄像头、网络摄像头或者其他视频输入设备获取原始视频。 2. 视频编码:将捕获到的原始视频使用H264编码器进行压缩编码。 3. 推:编码后的视频数据通过RTSP协议推送到指定的服务器或播放器。 一个基本的FFmpeg命令行示例来实现这个功能可能如下所示: ``` ffmpeg -i input -vcodec libx264 -acodec copy -f rtsp rtsp://server_address:port/stream_name ``` 其: - `-i input` 指定输入文件或设备,例如 `video.mp4` 或 `/dev/video0`。 - `-vcodec libx264` 指定视频编码器为H264。 - `-acodec copy` 表示音频编码不变,直接拷贝(如果有的话)。 - `-f rtsp` 指定输出格式为RTSP。 - `rtsp://server_address:port/stream_name` 指定RTSP的服务器地址和名称。 使用FFmpeg进行RTSP时需要注意的是,网络状况、编码参数和服务器配置都会影响推的稳定性和视频质量。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值