ffmpeg opencv 打开视频文件,并且播放
来源:互联网 发布:看漫威漫画的软件 编辑:程序博客网 时间:2024/05/30 04:42
// FFMpeg + OpenCV demo #include <stdio.h> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #ifdef __cplusplus extern "C" {#endif #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #include <libavformat/avformat.h> #include <libavcodec/avcodec.h> #include <libswscale/swscale.h> #include <libavutil/opt.h>#include <libavcodec/avcodec.h>#include <libavutil/channel_layout.h>#include <libavutil/common.h>#include <libavutil/imgutils.h>#include <libavutil/mathematics.h>#include <libavutil/samplefmt.h>#ifdef __cplusplus }#endif #include <iostream>static void CopyDate(AVFrame *pFrame, int width, int height, int time);static void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame);using namespace cv;using namespace std;int main(int argc, char* argv[]){int result = 0;av_register_all(); // 注册所有FFmpeg库所支持的文件格式和codecAVFormatContext* pFormatCtx;char* filename = "up.mp4"; //输入文件名// step1: 打开媒体文件,最后2个参数是用来指定文件格式,buffer大小和格式参数,设置成NULL的话,libavformat库会自动去探测它们result = avformat_open_input(&pFormatCtx, filename, NULL, NULL);if (result != 0){cout << "open file fail" << endl;return -1;}cout << "open file succ" << endl;// step2: 查找信息流的信息result = avformat_find_stream_info(pFormatCtx, NULL);if (result != 0){cout << "find stream fail" << endl;return -1;}cout << "find stream succ" << endl;// step3: 打印信息//av_dump_format(pFormatCtx, 0, filename, 0);// step4:找到video流数据int i = 0;int videoStream = -1;AVCodecContext* pCodecCtx = NULL;for (i = 0; i < pFormatCtx->nb_streams; i++){if (pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO){videoStream = i;break;}}if (videoStream == -1){cout << "find stream video fail" << endl;return -1;}cout << "find stream video succ." << endl;// 得到video编码格式pCodecCtx = pFormatCtx->streams[videoStream]->codec;// step5: 得到解码器AVCodec* pCodec = NULL;pCodec = avcodec_find_decoder(pCodecCtx->codec_id);if (pCodec == NULL){cout << "find decoder fail" << endl;return -1;}cout << "find decoder succ" << endl;result = avcodec_open2(pCodecCtx, pCodec, NULL);if (result != 0){cout << "open codec fail" << endl;return -1;}cout << "open codec succ" << endl;// step6: 申请原始数据帧 和 RGB帧内存AVFrame* pFrame = NULL;AVFrame* pFrameRGB = NULL;pFrame = av_frame_alloc();pFrameRGB = av_frame_alloc();if (pFrame == NULL || pFrameRGB == NULL){return -1;}int numBytes = avpicture_get_size(AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);uint8_t* buffer = (uint8_t*)av_malloc(numBytes * sizeof(uint8_t));avpicture_fill((AVPicture*)pFrameRGB, buffer, AV_PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);int frameFinishsed = 0;AVPacket packet;i = 0; // step7: 创建格式转化文本SwsContext * pSwxCtx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,pCodecCtx->width, pCodecCtx->height, AV_PIX_FMT_RGB24,SWS_BILINEAR, 0, 0, 0);cv::Mat image(pCodecCtx->height, pCodecCtx->width, CV_8UC3);int b = 0;int g = 1;int r = 2;while (true){// 得到数据包result = av_read_frame(pFormatCtx, &packet);if (result != 0){break;}if (packet.stream_index == videoStream){// 解码avcodec_decode_video2(pCodecCtx, pFrame, &frameFinishsed, &packet);if (frameFinishsed){// 转换sws_scale(pSwxCtx, pFrame->data, pFrame->linesize, 0, pCodecCtx->height,pFrameRGB->data, pFrameRGB->linesize);for (int m = 0; m < pCodecCtx->height; m++){for (int n = 0; n < pCodecCtx->width; n++){image.at<Vec3b>(m, n)[r] = pFrameRGB->data[0][3 * (m*pCodecCtx->width + n) + 0];image.at<Vec3b>(m, n)[g] = pFrameRGB->data[0][3 * (m*pCodecCtx->width + n) + 1];image.at<Vec3b>(m, n)[b] = pFrameRGB->data[0][3 * (m*pCodecCtx->width + n) + 2];}}cv::imshow("pic", image);cv::waitKey(30);}}av_free_packet(&packet);}avformat_close_input(&pFormatCtx);system("pause");return 0;}
0 0
- ffmpeg opencv 打开视频文件,并且播放
- ffmpeg和opencv 播放视频文件并显示
- ffmpeg打开视频文件步骤
- OpenCV 播放视频文件
- OpenCV索引方式播放视频文件
- opencv中如何播放视频文件
- opencv 打开选择视频文件框
- ffmpeg编程(二)把视频文件播放出来
- 应用FFMPEG和SDL,播放视频文件
- 使用ffmpeg播放视频文件的过程
- ffmpeg+opencv播放器
- OPENCV+FFMPEG播放器
- python opencv 读取本地视频文件 修改 ffmpeg
- 使用 FFmpeg 开发播放器基础--使用 ffmpeg 解码视频文件
- ffmpeg播放音视频文件(ffmpeg-3.2.4-win32-dev版本)
- OpenCV 无法打开视频文件的解决办法
- opencv 读取视频、打开摄像头、写入视频文件
- OpenCV学习笔记(3)-----打开视频文件
- iOS开发之网银在线支付快捷支付商户接口规范
- C++ RGB图片水平翻转
- Linux:管理动态链接库
- 二叉树的遍历
- 专项测试(一)兼容测试1-app兼容性测试
- ffmpeg opencv 打开视频文件,并且播放
- 多线程程序在多核和单核上运行的不同
- 关于无法使用xx-pc附加到应用程序iisexpress.exe
- Windows下Python开发环境搭建及 Python的HelloWorld示例
- C基本常识
- android横竖屏切换
- hdu3062 2-sat
- Android网络之数据解析----SAX方式解析XML数据
- c语言链表