[FFmpeg-user] Decode H264 and save to yuv file

Pavel Koshevoy pkoshevoy at gmail.com
Tue Jul 30 18:54:30 CEST 2013

On 7/30/2013 2:25 AM, edward20 wrote:
> Anyone? Please I just need part of C code which
> explains how to write decoded YUV data to file.

You are asking API questions on the wrong mailing list.  API questions 
are answered on https://lists.ffmpeg.org/mailman/listinfo/libav-user/

> Is there any example of it?


In particular -- 

> Here is what I have tried so far
> void write_frame_to_file(AVFrame* frame, int width, int height, int iframe)
> {
>          FILE* outfile;
>          char filename[32];
>          int y;
>          // Open file
>          sprintf(filename, "frame%d.yuv", iframe);
>          outfile = fopen(filename, "wb");
>          if (outfile == NULL) {
>                  return;
>          }
>          // Write pixel data
>          for (y = 0; y < height; ++y) {
>                  fwrite(frame->data[0]+y*frame->linesize[0], 1, width*3,
> outfile);

not width * 3, it should be frame->linesize[0] (if you really want to 
save it line by line)

And unless the frame pixel format is packed YUV (it's usually planar) 
you wouldn't be saving the UV part of the image.  For planar formats 
you'll probably want to save all the frame->data planes.

Also, there is no guarantee that the frame data is YUV at all (it could 
be BGRA), so you'll probably want to convert the frame to YUV pixel 
format.  Take a look at 
-- it's probably all you really need).

More information about the ffmpeg-user mailing list