YUV 到 JPG 编码使用 ffmpeg
YUV to JPG encoding using ffmpeg
我正在尝试对YVU文件进行编码并将其另存为jpg文件。 但我不明白以下内容
1.为什么数据包大小是大小*3。
av_new_packet(&pkt,size*3(;'
2.In 担心为什么我们使用尺寸*3/2。
if(fread(buffer , 1, size*3/2, ptrInputFile(<=0('
3.他们如何在这里填写数据
帧>数据[0] = 缓冲区;
帧>数据[1] = 缓冲区 + 大小;
帧>数据[2] = 缓冲区 + siz*5/4;
法典:
AVFormatContext *avFrameContext;
AVOutputFormat *avOutputFormat;
AVStream *avStream;
AVCodecContext *avCodecContext;
AVCodec *avCodec;
AVFrame *frame;
AVPacket pkt;
const char *output = "temp.jpg";
FILE *ptrInputFile;
const char *input = "cuc_view_480x272.yuv";
ptrInputFile = fopen(input ,"rb");
if(!ptrInputFile)
return -1;
avFrameContext = avformat_alloc_context();
avOutputFormat = av_guess_format("mjpeg", NULL, NULL);
if(!avOutputFormat)
return -1;
avFrameContext->oformat = avOutputFormat;
if(avio_open(&avFrameContext->pb ,output ,AVIO_FLAG_READ_WRITE)<0)
return -1;
avStream = avformat_new_stream(avFrameContext,NULL);
if(!avStream)
return -1;
avCodecContext = avStream->codec;
avCodecContext->codec_id = avOutputFormat->video_codec;
avCodecContext->codec_type = AVMEDIA_TYPE_VIDEO;
avCodecContext->pix_fmt = PIX_FMT_YUVJ420P;
avCodecContext->width = 480;
avCodecContext->height = 272;
avCodecContext->time_base.num = 1;
avCodecContext->time_base.den = 25;
avCodec = avcodec_find_encoder(avCodecContext->codec_id);
if(!avCodec)
return -1;
if(avcodec_open2(avCodecContext ,avCodec,NULL)<0)
return -1;
frame = av_frame_alloc();
int size = avpicture_get_size(PIX_FMT_YUVJ420P ,avCodecContext->width, avCodecContext->height);
uint8_t *buffer = (uint8_t*)av_malloc(size*sizeof(uint8_t));
avpicture_fill((AVPicture*)frame, buffer, avCodecContext->pix_fmt ,avCodecContext->width, avCodecContext->height);
//write header
avformat_write_header(avFrameContext, NULL);
int siz = avCodecContext->width*avCodecContext->height;
av_new_packet(&pkt,siz*3);
if(fread(buffer , 1, siz*3/2, ptrInputFile)<=0)
return -1;
frame->data[0] = buffer;
frame->data[1] = buffer + siz;
frame->data[2] = buffer + siz*5/4;
如果你看一下yuv420p(wiki(的格式,数据在文件中的格式为:
As there are 'siz' length of pixels in the image:
siz length of y value
siz/4 length of u value
siz/4 length of v value
因此,对于问题 2:我们有 siz*3/2 的数据长度要读取。
对于问题 3:y 从缓冲区+0 开始,u 从缓冲区+siz 开始,v 从缓冲区+siz*5/4 开始。
至于问题1:我不确定数据是否转换为RGB。如果转换,则每个像素需要 3 个字节。需要其他代码才能看到这一点。
我对你上面提供的代码知之甚少。但是,如果您尝试对yuv视频进行编码并另存为jpeg,则可以在ffmpeg中直接使用以下命令
ffmpeg -f rawvideo -vcodec rawvideo -s <resolution> -r 25 -pix_fmt yuv420p -i video.yuv -preset ultrafast -qp 0 %d.jpg
用视频的分辨率替换<resolution>
,例如1920x1080
相关文章:
- 如何使用 ffmpeg 将 3840 nb_samples编码为请求 1024 的编解码器
- FFmpeg:使用自定义线程池进行并行编码
- FFMPEG H264对每个图像进行编码
- 如何使用FFMPEG从编码的视频中提取AVPACKET的重要信息
- 使用ffmpeg中的muxing读取编码数据
- 使用 FFmpeg 编码和带有网络摄像头的 UDP
- ffmpeg/c++ 使用 ffmpeg 对视频帧的附加信息进行编码
- 如何使用FFMPEG与C拦截编码的视频数据
- ffmpeg H264一次对帧进行编码,用于网络流传输
- 使用ffmpeg编码时丢失质量
- 如何使用 x264 进行 ffmpeg 编码
- FFmpeg:编码PCM 16音频数据分配错误
- 将标准的Android H264软件编码器替换为基于ffmpeg的编码器
- FFmpeg音频编码器新增编码功能
- H264 编码视频流 FFMPEG C++
- YUV 到 JPG 编码使用 ffmpeg
- C++ ffmpeg x264 编码设置
- FFMPEG:编码后直接解码数据包
- FFMpeg编码的视频将只在FFPlay中播放
- 用ffmpeg编码:视频质量下降