通过FFmpeg将多媒体文件解码后保存成Bmp图像(YUV420 RGB32)

参考http://hi.baidu.com/mingxin505/item/52d6d1cda805d925a0b50a57,将其改为linux下可编译运行。可实现YUV420P与RGB32的互转。

/*   g++ -o test test.cpp -lavformat -lavcodec -lavutil -lz -lm -lpthread -lswscale  */

#include <string>
#include <cassert>
#include <iostream>
#include <sstream>
//#include <tchar.h>


extern "C"
{
#ifndef INT64_C
#define INT64_C(c) (c ## LL)
#define UINT64_C(c) (c ## ULL)
#endif
#include <libavutil/avutil.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
#include <libavcodec/avcodec.h>
#include <stdlib.h>
#include <unistd.h>
};
//#pragma comment(lib, "avformat.lib")
//#pragma comment(lib, "avutil.lib")
//#pragma comment(lib, "avcodec.lib")
//#pragma comment(lib, "swscale.lib")
#pragma pack(1)
#define BOOL int
#define TRUE 1
#define FALSE 0
#define BI_RGB 0x0

char *itoa(int num,char *str,int radix) {
	/* 索引表 */
	char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	unsigned unum; /* 中间变量 */
	int i=0,j,k;
	/* 确定unum的值 */
	if(radix==10&&num<0) /* 十进制负数 */
	{
		unum=(unsigned)-num;
		str[i++]='-';
	} else unum=(unsigned)num; /* 其他情况 */
	/* 逆序 */
	do {
		str[i++]=index[unum%(unsigned)radix];
		unum/=radix;
	}while(unum);
	str[i]='\0';
	/* 转换 */
	if(str[0]=='-') k=1; /* 十进制负数 */
	else k=0;
	char temp;
	for(j=k;j<=(i-k-1)/2;j++)
	{
		temp=str[j];
		str[j]=str[i-j-1];
		str[i-j-1]=temp;
	}
  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是使用c语言和FFmpeg解码视频为yuv420p格式并保存文件的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <libavutil/imgutils.h> #include <libavutil/parseutils.h> #include <libavutil/opt.h> #include <libavcodec/avcodec.h> #include <libavformat/avformat.h> int main(int argc, char *argv[]) { AVFormatContext *fmt_ctx = NULL; int ret; if (argc < 2) { fprintf(stderr, "Usage: %s <input file>\n", argv[0]); exit(1); } ret = avformat_open_input(&fmt_ctx, argv[1], NULL, NULL); if (ret < 0) { fprintf(stderr, "Could not open input file '%s'\n", argv[1]); exit(1); } ret = avformat_find_stream_info(fmt_ctx, NULL); if (ret < 0) { fprintf(stderr, "Could not find stream information\n"); exit(1); } int video_stream_index = -1; for (int i = 0; i < fmt_ctx->nb_streams; i++) { if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; break; } } if (video_stream_index == -1) { fprintf(stderr, "Could not find video stream\n"); exit(1); } AVCodecParameters *codecpar = fmt_ctx->streams[video_stream_index]->codecpar; AVCodec *codec = avcodec_find_decoder(codecpar->codec_id); if (!codec) { fprintf(stderr, "Codec not found\n"); exit(1); } AVCodecContext *codec_ctx = avcodec_alloc_context3(codec); if (!codec_ctx) { fprintf(stderr, "Could not allocate codec context\n"); exit(1); } ret = avcodec_parameters_to_context(codec_ctx, codecpar); if (ret < 0) { fprintf(stderr, "Could not copy codec parameters to codec context\n"); exit(1); } ret = avcodec_open2(codec_ctx, codec, NULL); if (ret < 0) { fprintf(stderr, "Could not open codec\n"); exit(1); } AVPacket pkt; av_init_packet(&pkt); pkt.data = NULL; pkt.size = 0; AVFrame *frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate frame\n"); exit(1); } int frame_count = 0; while (av_read_frame(fmt_ctx, &pkt) >= 0) { if (pkt.stream_index == video_stream_index) { ret = avcodec_send_packet(codec_ctx, &pkt); if (ret < 0) { fprintf(stderr, "Error sending packet to decoder\n"); exit(1); } while (ret >= 0) { ret = avcodec_receive_frame(codec_ctx, frame); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; } else if (ret < 0) { fprintf(stderr, "Error during decoding\n"); exit(1); } char filename[256]; snprintf(filename, sizeof(filename), "frame-%d.yuv", frame_count); FILE *fp = fopen(filename, "wb"); if (!fp) { fprintf(stderr, "Could not open output file '%s'\n", filename); exit(1); } for (int i = 0; i < codec_ctx->height; i++) { fwrite(frame->data[0] + i * frame->linesize[0], 1, codec_ctx->width, fp); } for (int i = 0; i < codec_ctx->height / 2; i++) { fwrite(frame->data[1] + i * frame->linesize[1], 1, codec_ctx->width / 2, fp); } for (int i = 0; i < codec_ctx->height / 2; i++) { fwrite(frame->data[2] + i * frame->linesize[2], 1, codec_ctx->width / 2, fp); } fclose(fp); frame_count++; } } av_packet_unref(&pkt); } avcodec_free_context(&codec_ctx); avformat_close_input(&fmt_ctx); avformat_free_context(fmt_ctx); av_frame_free(&frame); return 0; } ``` 该示例代码从命令行读取一个输入文件名,并使用FFmpeg打开该文件。然后它搜索视频流并使用视频解码解码视频帧。对于每个解码帧,它将像素数据写入一个yuv文件。每个yuv文件的名称都是“frame-N.yuv”,其中N是帧计数器。最后,它释放所有使用的资源并退出。 请注意,此示例代码假定输入文件是可解码的视频文件。如果您想处理其他类型的输入文件,请根据需要进行修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值