Skip to content

Instantly share code, notes, and snippets.

@jpxiong
Last active June 4, 2018 06:33
Show Gist options
  • Select an option

  • Save jpxiong/ed8c6326729e3eedcaf8227d56a2f909 to your computer and use it in GitHub Desktop.

Select an option

Save jpxiong/ed8c6326729e3eedcaf8227d56a2f909 to your computer and use it in GitHub Desktop.
save_avframe_as_jpeg of ffmpeg
int save_avframe_as_jpeg(AVCodecContext *pCodecCtx, AVFrame *pFrame) {
AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_MJPEG);
if (!jpegCodec) {
return -1;
}
AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
if (!jpegContext) {
return -1;
}
jpegContext->pix_fmt = AV_PIX_FMT_YUVJ420P;
jpegContext->compression_level = 0;
jpegContext->thread_count = 1;
jpegContext->prediction_method = 1;
jpegContext->flags2 = 0;
jpegContext->sample_aspect_ratio = pCodecCtx->sample_aspect_ratio;
jpegContext->time_base = pCodecCtx->time_base;
jpegContext->height = pFrame->height;
jpegContext->width = pFrame->width;
int ret = avcodec_open2(jpegContext, jpegCodec, NULL);
if (ret < 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, "%s/test_%lld.jpg", "/sdcard/", packet.pts);
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