<div dir="ltr"><div>On Wed, Apr 12, 2023 at 10:19 PM Strahinja Radman <<a href="mailto:dr.strashni@gmail.com">dr.strashni@gmail.com</a>> wrote: <br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">Hi Gustav,</div><div dir="auto"><br></div><div dir="auto">Thats because you are only saving luminance component. There are also two more components for color, in following arrays frame->data[1] and frame->data[2].</div></blockquote></div><br><div>Thank you for the comment. Initially, I tried to fix the problem by modifying the PNG creation procedure... trying to include the other color components in the PNG data, with no luck.</div><div>After that, I tried a different approach: What if the AVFrame format is not the one required by the PNG format? What if I need to make some kind of data translation? <br></div><div>Understanding that the MP4 video container holds frames with the YUV420P format, and PNG files contain image data with an RGB structure.<br></div><div><br></div><div>So, I decided to implement this piece of code before creating the PNG files:</div><div><br></div><div>            // To create the PNG files, the AVFrame data must be translated from YUV420P format into RGB24<br>            struct SwsContext *sws_ctx = sws_getContext(<br>                pFrame->width, pFrame->height, pFrame->format,<br>                pFrame->width, pFrame->height, AV_PIX_FMT_RGB24,<br>                SWS_BILINEAR, NULL, NULL, NULL);<br><br>            // Allocate a new AVFrame for the output RGB24 image<br>            AVFrame* rgb_frame = av_frame_alloc();<br><br>            // Set the properties of the output AVFrame<br>            rgb_frame->format = AV_PIX_FMT_RGB24;<br>            rgb_frame->width = pFrame->width;<br>            rgb_frame->height = pFrame->height;<br><br>            int ret = av_frame_get_buffer(rgb_frame, 0);<br>            if (ret < 0) {<br>                logging("Error while preparing RGB frame: %s", av_err2str(ret));<br>                return ret;<br>            }<br><br>            logging("Transforming frame format from YUV420P into RGB24...");<br>            ret = sws_scale(sws_ctx, pFrame->data, pFrame->linesize, 0, pFrame->height, rgb_frame->data, rgb_frame->linesize);<br>            if (ret < 0) {<br>                logging("Error while translating the frame format from YUV420P into RGB24: %s", av_err2str(ret));<br>                return ret;<br>            }</div><div><br></div><div>As result, the rgb_frame contains the accurate data input required to create the PNG files in a successful way.</div><div>The final solution is available right here: <a href="https://github.com/xtingray/MP4toPNG">https://github.com/xtingray/MP4toPNG</a></div><div><br></div><div>PS: The funny thing about this solution is that it was suggested by ChatGPT.<br></div><div><br></div><div dir="ltr" class="gmail_signature"><div dir="ltr"><div>--<br>  Gustav Gonzalez<br>  <a href="mailto:xtingray@gmail.com" target="_blank">xtingray@gmail.com</a><br><br></div></div></div></div>