Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <libavformat/avformat.h>
- #include <libavcodec/avcodec.h>
- #include <libavutil/avutil.h>
- #include <libavutil/mem.h>
- #include <libswscale/swscale.h>
- int main()
- {
- AVFormatContext *avfc;
- AVCodecContext *codecctx;
- struct SwsContext *swsctx;
- AVCodec *codec;
- AVFrame *pFrame;
- AVFrame *pFrameRGB;
- uint8_t *picbuf;
- int numbytes;
- int res = 0;
- int frameFinished = 0;
- int gotpic;
- AVPacket pkt;
- int i = 0;
- printf("Hello world!\n");
- char fname[100] = "out.avi";
- av_log_set_level(AV_LOG_DEBUG);
- // Register
- av_register_all();
- // Alloc formatcontext and open input file
- avfc = avformat_alloc_context();
- res = avformat_open_input(&avfc, fname, NULL, NULL);
- printf("%i\n", res);
- // Populate streams (not always necessary
- res = avformat_find_stream_info(avfc, NULL);
- printf("%i\n", res);
- // Open codec described the first stream (should search for first video stream though)
- codecctx = avfc->streams[0]->codec;
- codec = avcodec_find_decoder(codecctx->codec_id);
- res = avcodec_open2(codecctx, codec, NULL);
- printf("%i\n", res);
- av_dump_format(avfc, 0, fname, 0);
- // Allocate frame and get next frame
- pFrame = avcodec_alloc_frame();
- pFrameRGB = avcodec_alloc_frame();
- pFrameRGB->format = PIX_FMT_RGBA;
- pFrameRGB->width = 512;
- pFrameRGB->height = 512;
- numbytes = avpicture_get_size(pFrameRGB->format, pFrameRGB->width, pFrameRGB->height);
- picbuf = av_malloc(numbytes*sizeof(uint8_t));
- avpicture_fill((AVPicture *) pFrameRGB, picbuf, pFrameRGB->format, pFrameRGB->width, pFrameRGB->height);
- av_init_packet(&pkt);
- int framenum = 0;
- while (av_read_frame(avfc, &pkt)>= 0) {
- pkt.flags = pkt.flags&AV_PKT_FLAG_KEY;
- avcodec_decode_video2(codecctx, pFrame, &gotpic, &pkt);
- printf("%d\n", gotpic);
- if (gotpic) {
- printf("Got frame.\n");
- swsctx = sws_getContext(pFrame->width, pFrame->height, pFrame->format, pFrameRGB->width, pFrameRGB->height, pFrameRGB->format, SWS_BICUBIC, NULL, NULL, NULL);
- sws_scale(swsctx, (const uint8_t * const*) pFrame->data, pFrame->linesize, 0, pFrame->height, pFrameRGB->data, pFrameRGB->linesize);
- AVCodec *encodec = avcodec_find_encoder(CODEC_ID_PNG);
- AVCodecContext *avencctx = avcodec_alloc_context3(encodec);
- avcodec_get_context_defaults3(avencctx, encodec);
- avencctx->pix_fmt = PIX_FMT_RGBA;
- avencctx->width = pFrameRGB->width;
- avencctx->height = pFrameRGB->height;
- int resenc = avcodec_open2(avencctx, avcodec_find_encoder(CODEC_ID_PNG), NULL);
- AVPacket avpenc;
- avpenc.data = NULL;
- int gotpkt = 0;
- if (avcodec_encode_video2(avencctx, &avpenc, pFrameRGB, &gotpkt) != 0) printf("Failed to encode.\n");
- else {FILE *file = NULL;
- char fname[10];
- sprintf(fname, "%04d.raw", framenum);
- file = fopen(fname, "wb");
- if (file != NULL) {
- fwrite(avpenc.data, avpenc.size, 1, file);
- } else printf("Can't access file.\n");
- fclose(file);
- }
- framenum++;
- }
- av_free_packet(&pkt);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement