ffmpeg视频流

目录大纲

安装

1.升级系统

yum install epel-release -y
yum update -y

2.安装Nux Dextop Yum 源

#CentOS 7
rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm

3.安装FFmpeg 和 FFmpeg开发包

yum install ffmpeg ffmpeg-devel -y

4.测试是否安装成功

ffmpeg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用FFmpeg API实现视频流解码的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <pthread.h> #include <sys/time.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> #include <libswscale/swscale.h> #define MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio // 视频流解码线程 void *decode_video_thread(void *arg) { AVFormatContext *pFormatCtx = NULL; AVCodecContext *pCodecCtx = NULL; AVCodec *pCodec = NULL; AVFrame *pFrame = NULL; AVPacket packet; int videoStream = -1; int frameFinished = 0; struct SwsContext *img_convert_ctx = NULL; // 打开视频流 if (avformat_open_input(&pFormatCtx, "rtsp://xxx.xxx.xxx.xxx:xxxx/xxx", NULL, NULL) != 0) { printf("Couldn't open input stream.\n"); return NULL; } // 查找视频流信息 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { printf("Couldn't find stream information.\n"); return NULL; } // 查找视频流 for (int i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { videoStream = i; break; } } if (videoStream == -1) { printf("Couldn't find a video stream.\n"); return NULL; } // 获取视频解码器 pCodec = avcodec_find_decoder(pFormatCtx->streams[videoStream]->codecpar->codec_id); if (pCodec == NULL) { printf("Codec not found.\n"); return NULL; } // 初始化解码器上下文 pCodecCtx = avcodec_alloc_context3(pCodec); if (avcodec_parameters_to_context(pCodecCtx, pFormatCtx->streams[videoStream]->codecpar) < 0) { printf("Couldn't copy codec context.\n"); return NULL; } // 打开解码器 if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { printf("Couldn't open codec.\n"); return NULL; } // 分配视频帧内存 pFrame = av_frame_alloc(); if (pFrame == NULL) { printf("Couldn't allocate video frame.\n"); return NULL; } // 初始化图像转换上下文 img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL); if (img_convert_ctx == NULL) { printf("Couldn't initialize sws context.\n"); return NULL; } // 读取视频流 while (av_read_frame(pFormatCtx, &packet) >= 0) { if (packet.stream_index == videoStream) { // 解码视频帧 avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet); if (frameFinished) { // 转换图像格式 uint8_t *out_buffer = (uint8_t *)av_malloc(av_image_get_buffer_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1)); av_image_fill_arrays(pFrame->data, pFrame->linesize, out_buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height, 1); sws_scale(img_convert_ctx, (const uint8_t *const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrame->data, pFrame->linesize); // 处理图像数据 // ... av_free(out_buffer); } } av_packet_unref(&packet); } // 释放资源 sws_freeContext(img_convert_ctx); av_frame_free(&pFrame); avcodec_close(pCodecCtx); avformat_close_input(&pFormatCtx); return NULL; } // 主函数 int main(int argc, char *argv[]) { // 初始化FFmpeg库 av_register_all(); avformat_network_init(); // 创建视频流解码线程 pthread_t tid; pthread_create(&tid, NULL, decode_video_thread, NULL); // 等待线程结束 pthread_join(tid, NULL); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值