ffmpeg2.8将多媒体文件保存为ppm
来源:互联网 发布:京东自营名龙堂 知乎 编辑:程序博客网 时间:2024/06/06 17:39
基于ffmpeg 2.8 API 的改写
//// main.cpp// save_video_to_ppm//// Created by meitu on 15/10/15.// Copyright © 2015年 meitu. All rights reserved.//#ifdef __cplusplusextern "C"{#endif#include "libavcodec/avcodec.h"#include "libavutil/imgutils.h"#include "libavformat/avformat.h"#include "libswscale/swscale.h"#ifdef __cplusplus}#endifstatic void SaveFrame(AVFrame* pFrame, int width, int height, int iFrame){ FILE *pFile; char szFilename[32]; int y; //Open file sprintf(szFilename, "frame%d.ppm", iFrame); pFile = fopen(szFilename, "wb"); if (pFile == NULL) { fprintf(stderr, "Could not open the ppm file.\n"); return; } //Write header fprintf(pFile, "P6\n%d %d\n255\n", width, height); //Write pixel data for (y = 0; y < height; ++y) { fwrite(pFrame->data[0] + y*pFrame->linesize[0], 1, width*3, pFile); } fclose(pFile);}int main(int argc, char *argv[]){ av_register_all(); AVFormatContext *pFormatCtx = NULL; //Open video file and allocate format context //读取文件的头部信息,并且保持这些信息到AVFormatContext中 //如果AVFormatContext是NULL,这个函数将会同时申请一个空间 if (avformat_open_input(&pFormatCtx, argv[1], NULL, NULL) < 0) { av_log(pFormatCtx, AV_LOG_ERROR, "Could not open file.\n"); return 1; } //检查在文件的流的信息 //这个函数为 pFormatCtx-streams填充正确的信息 if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { av_log(pFormatCtx, AV_LOG_ERROR, "Could not find stream info.\n"); return 1; } //输出信息 av_dump_format(pFormatCtx, 0, argv[1], 0); //找到视频流 int videoStream = 0; videoStream = av_find_best_stream(pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); if (videoStream == -1) { av_log(pFormatCtx, AV_LOG_ERROR, "Could not find stream index.\n"); return 1; } AVCodecContext *pCodecCtx = NULL; pCodecCtx = pFormatCtx->streams[videoStream]->codec; AVCodec *pCodec = NULL; pCodec =avcodec_find_decoder(pCodecCtx->codec_id); if (pCodec == NULL) { av_log(pCodec, AV_LOG_ERROR, "Could not find decoder.\n"); return 1; } //open codec if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) { av_log(pCodecCtx, AV_LOG_ERROR, "Could open decoder.\n"); return 1; } AVFrame *pFrameRGB = NULL; pFrameRGB = av_frame_alloc(); if (!pFrameRGB) { av_log(pFrameRGB , AV_LOG_ERROR, "Could allocate frame.\n"); return 1; } uint8_t *buffer; int numBytes = 0; /* 之前我们申请了一个 帧 对象,当转换的时候,我们仍然需要一个地方来放置原始的数据。 我们使用avpicture_get_size来获取大小,然后av_malloc申请大小 avpicture_fill将申请的帧和我们新申请的内存结合起来 */// numBytes = avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height);// buffer = (uint8_t *)av_malloc(numBytes);// avpicture_fill((AVPicture*)pFrameRGB, buffer, PIX_FMT_RGB24, pCodecCtx->width, pCodecCtx->height); //使用av_image_alloc代替上面注释的三行代码 av_image_alloc(pFrameRGB->data, pFrameRGB->linesize, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, 1); //接下来,读入整个视频流,然后把它解码成帧,最后转换格式并且保存 int frameFinished; AVPacket pkt; //用来存储解码后的原始数据 AVFrame *pFrame = av_frame_alloc(); int i = 0; auto img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height, PIX_FMT_RGB24, SWS_BILINEAR, NULL, NULL, NULL); while (av_read_frame(pFormatCtx, &pkt) >= 0) { //Is this a packet from the video stream? if (pkt.stream_index == videoStream) { //Decode video frame //packet -----> frame avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &pkt); if (frameFinished) { sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, (uint8_t *const *)pFrameRGB->data, pFrameRGB->linesize); if( ++i <= 100 ) SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, i); } av_free_packet(&pkt); } } sws_freeContext(img_convert_ctx); //free the rgb image //av_free(buffer); av_free(pFrameRGB); //free the yuv frame av_free(pFrame); //close the codec avcodec_close(pCodecCtx); //close the video file avformat_close_input(&pFormatCtx); return 0;}
一些具体的解释请看参考文献
0 0
- ffmpeg2.8将多媒体文件保存为ppm
- ffmpeg将多媒体文件的Video Stream每帧画面保存为PPM格式图片
- Android_调用ffmpeg_把截取视频保存为ppm文件
- 读取视频文件,保存帧图片为ppm文件
- 代码学习,将视频文件处理为ppm文件
- FFMPEG解复用、解码测试,音频保存WAV文件,视频保存为PPM图像
- 将HBITMAP保存为文件
- 将对象保存为文件
- 将bitmap保存为文件
- 将bmp图像转换为ppm格式
- 文件上传将文件保存为二进制
- 将网页保存为mht文件
- 将位图句柄保存为BMP文件
- 将DBGrid 结果保存为Execl 文件
- 将DataView保存为Excel文件
- 将html流保存为word文件
- 将html流保存为word文件
- 将HDC保存为BMP文件
- HTML5格式问题,你遇到了吗?
- Android Studio导入第三开源库
- 发明专利:一种适用于监控视频的编码方法
- 第四周项目6多项式求和
- 第7周 项目6-停车场模拟
- ffmpeg2.8将多媒体文件保存为ppm
- C++内存管理
- 第七周 建立顺序环形队列算法库
- Crystal Image Toolkit: thumbnail image control and picture viewing.
- C/S 类型网络编程练习 猜数游戏
- 第7周项目3-负数把正数赶出队列
- 多机调度问题
- 第7周项目4 - 队列数组
- 网络编程--多线程断点下载文件