Created
March 29, 2017 18:22
-
-
Save tyraeltong/2309baf41e9546b7d757a6477c236418 to your computer and use it in GitHub Desktop.
Save an AVFrame as a JPEG file using FFmpeg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| int save_frame_as_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) { | |
| AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000); | |
| if (!jpegCodec) { | |
| return -1; | |
| } | |
| AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec); | |
| if (!jpegContext) { | |
| return -1; | |
| } | |
| jpegContext->pix_fmt = pCodecCtx->pix_fmt; | |
| jpegContext->height = pFrame->height; | |
| jpegContext->width = pFrame->width; | |
| if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) { | |
| return -1; | |
| } | |
| FILE *JPEGFile; | |
| char JPEGFName[256]; | |
| AVPacket packet = {.data = NULL, .size = 0}; | |
| av_init_packet(&packet); | |
| int gotFrame; | |
| if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) { | |
| return -1; | |
| } | |
| sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo); | |
| JPEGFile = fopen(JPEGFName, "wb"); | |
| fwrite(packet.data, 1, packet.size, JPEGFile); | |
| fclose(JPEGFile); | |
| av_free_packet(&packet); | |
| avcodec_close(jpegContext); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment