1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| int main(int argc, char *argv[]) { int ret; const char *in_filename, *out_filename; AVFormatContext *fmt_ctx = NULL; const AVCodec *codec; AVCodecContext *codeCtx = NULL;
AVStream *stream = NULL; int stream_index;
AVPacket avpkt;
int frame_count; AVFrame *frame;
if (argc <= 2) { printf("Usage: %s <input file> <output file>\n", argv[0]); exit(0); } in_filename = argv[1]; out_filename = argv[2];
if (avformat_open_input(&fmt_ctx, in_filename, NULL, NULL) < 0) { printf("Could not open source file %s\n", in_filename); exit(1); } if (avformat_find_stream_info(fmt_ctx, NULL) < 0) { printf("Could not find stream information\n"); exit(1); }
av_dump_format(fmt_ctx, 0, in_filename, 0);
av_init_packet(&avpkt); avpkt.data = NULL; avpkt.size = 0;
stream_index = av_find_best_stream(fmt_ctx, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0); if (ret < 0) { fprintf(stderr, "Could not find %s stream in input file '%s'\n", av_get_media_type_string(AVMEDIA_TYPE_VIDEO), in_filename); return ret; }
stream = fmt_ctx->streams[stream_index];
codec = avcodec_find_decoder(stream->codecpar->codec_id); if (codec == NULL) { return -1; }
codeCtx = avcodec_alloc_context3(NULL); if (!codeCtx) { fprintf(stderr, "Could not allocate video codec context\n"); exit(1); }
if ((ret = avcodec_parameters_to_context(codeCtx, stream->codecpar)) < 0) { fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n", av_get_media_type_string(AVMEDIA_TYPE_VIDEO)); return ret; }
avcodec_open2(codeCtx, codec, NULL);
frame = av_frame_alloc(); if (!frame) { fprintf(stderr, "Could not allocate video frame\n"); exit(1); }
frame_count = 0; char buf[1024]; while (av_read_frame(fmt_ctx, &avpkt) >= 0) { if (avpkt.stream_index == stream_index) { int re = avcodec_send_packet(codeCtx, &avpkt); if (re < 0) { continue; } while (avcodec_receive_frame(codeCtx, frame) == 0) { snprintf(buf, sizeof(buf), "%s/Demo-%d.jpg", out_filename, frame_count); saveJpg(frame, buf); }
frame_count++; } av_packet_unref(&avpkt); }
}
|