我的程序需要在文件转码的同时去截图,按首祯截图和时间序列截图,但困扰于如何保存成jpg格式。
方案一:
利用ffmpeg将从文件读取出来的祯解码成YUV420P像素格式,用ffmpeg编码成jpg格式
借鉴于雷博士的最简单的基于FFmpeg的图像编码器 连接:http://blog.csdn.net/leixiaohua1020/article/details/25346147
方案二:
自己按照jpg格式,写jpg文件或者利用libjpeg转换
提供一个jpg格式头分析,连接 : http://www.cnblogs.com/lakeone/p/3596996.html
用libjpeg开源库去转换,我的程序内没采用这种方案,提供个连接:http://blog.csdn.net/yixianfeng41/article/details/52181578
下面贴上我的代码:
/**************************************************
* 函数名称: Frame2JPG
* 功能描述: 将AVFrame(YUV420格式)保存为JPEG格式的图片
* 参 数: AVPacket packetav_read_frame读取的一包数据
* 参 数: AVFrame * pFrame解码完的帧
* 参 数: stream_index流下标,标记是视频流还是音频流
* 参 数: int widthYUV420的宽
* 参 数: int heightYUV420的高
* 返 回 值: int 0 代表成功,其他失败
* 其他说明:
* 其它说明:
* 修改日期 修改人 修改内容
* --------------------------------------------
**************************************************/
int Frame2JPG(AVPacket packet, AVFrame* pFrame, unsigned int stream_index,
int width, int height)
{
// 输出文件路径
char out_file[MAX_PATH] = { 0 };
sprintf_s(out_file, sizeof(out_file), "%s%d.jpg", "e:\\", packet.pts);
// 分配AVFormatContext对象
AVFormatContext* pFormatCtx = avformat_alloc_context();
// 设置输出文件格式
pFormatCtx->oformat = av_guess_format("mjpeg", NULL, NULL);
// 创建并初始化一个和该url相关的AVIOContext
if (avio_open(&pFormatCtx->pb, out_file, AVIO_FLAG_READ_WRITE) < 0)
{
printf("Couldn't open output file");
return -1;
}
// 构建一个新stream
AVStream* pAVStream = avformat_new_stream(pFormatCtx, 0);
if (pAVStream == NULL)
{
printf("Frame2JPG::avformat_new_stream error.");
return -1;
}
// 设置该stream的信息
AVCodecContext* pCodecCtx = pAVStream->codec;
pCodecCtx->codec_id = pFormatCtx->oformat->video_codec;
pCodecCtx->codec_type = AVMEDIA_TYPE_VIDEO;
pCodecCtx->pix_fmt = AV_PIX_FMT_YUVJ420P;
pCodecCtx->width = width;
pCodecCtx->height = height;
pCodecCtx->time_base.num = 1;
pCodecCtx->time_base.den = 25;
// 查找解码器
AVCodec* pCodec = avcodec_find_encoder(pCodecCtx->codec_id);
if (!pCodec)
{
printf("avcodec_find_encoder() error.");
return -1;
}
// 设置pCodecCtx的解码器为pCodec
if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0)
{
printf("Could not open codec.");
return -1;
}
//Write Header
int ret = avformat_write_header(pFormatCtx, NULL);
if (ret < 0)
{
printfe("avformat_write_header() error.\n");
return -1;
}
int y_size = pCodecCtx->width * pCodecCtx->height;
//Encode
// 给AVPacket分配足够大的空间
AVPacket pkt;
av_new_packet(&pkt, y_size * 3);
int got_picture = 0;
ret = avcodec_encode_video2(pCodecCtx, &pkt, pFrame, &got_picture);
if (ret < 0)
{
printf("avcodec_encode_video2() error.\n");
return -1;
}
if (got_picture == 1)
{
ret = av_write_frame(pFormatCtx, &pkt);
}
av_free_packet(&pkt);
//Write Trailer
av_write_trailer(pFormatCtx);
if (pAVStream)
{
avcodec_close(pAVStream->codec);
}
avio_close(pFormatCtx->pb);
avformat_free_context(pFormatCtx);
return 0;
}
如有不足之处,还请指出。
下一步将把编译ffmpeg以及转码demo附上