//FFMPEG抓取摄像头数据并保存100帧上传至rtmp服务器
//编译指令: gcc b.c -o a.out -Iinclude -L/home/sambashare/ffmpeg-for-debugging/lib -lavcodec -lavformat -lavdevice -lavutil -lswscale
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "libavformat/avformat.h"
#include "libavcodec/avcodec.h"
#include "libavdevice/avdevice.h"
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
char* input_name= "video4linux2";
char* file_name = "/dev/video0";
char* out_file = "11yuv420.yuv";//使用专用软件打开时如果打开时的格式与实际不符合会出错(按照错误的方式解读)
static AVFormatContext *ifmt_ctx;
static AVFormatContext *ofmt_ctx;
typedef struct StreamContext {
AVCodecContext *dec_ctx;
AVCodecContext *enc_ctx;
} StreamContext;
static StreamContext *stream_ctx = NULL;
const char* jpeg_file = "cuc_view_encode.jpg";
unsigned int g_stream_index = -1;
void release_frame(AVFrame *frame)
{
if(frame){
av_frame_unref(frame);
av_frame_free(&frame);
}
}
void release_packet(AVPacket *packet)
{
if(packet){
av_packet_unref(packet);
av_packet_free(&packet);
}
}
//打开输入文件并获取其中各个码流的stream_ctx[?]->dec_ctx
int open_input_file()
{
int i = 0;
int ret = -1;
int videoindex = -1;
ifmt_ctx = NULL;//里面有?个码流--对应stream_ctx[?]
if((ret = avformat_open_input (&ifmt_ctx, file_name, NULL, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");return ret;}
//av_dump_format(ifmt_ctx,0,file_name,0);
if((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) {
av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");return;}
stream_ctx = av_mallocz_array(ifmt_ctx->nb_streams, sizeof(*stream_ctx));//申请连续的多个结构体
if (!stream_ctx){
return AVERROR(ENOMEM);}
//获取stream_ctx[?]->dec_ctx
for (i = 0; i < ifmt_ctx->nb_streams; i++)//获取stream_ctx[?]->dec_ctx
{
AVStream *stream = ifmt_ctx->streams[i];
AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id);//if (!dec) {av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i);return AVERROR_DECODER_NOT_FOUND;}
AVCodecContext *codec_ctx <
RTMP---实例---从内存中读取并rtmp传送
最新推荐文章于 2024-07-04 16:27:15 发布