1 #include <libavcodec/avcodec.h>
2 #include <libavformat/avformat.h>
3 #include <libswscale/swscale.h>
5 char const video[] = "/home/carl/Films/A town called panic.divx";//Ghostbusters.avi";
8 save_frame (AVFrame *frame, int width, int height, int N)
12 sprintf (filename, "frame%d.ppm", N);
13 FILE* file = fopen (filename, "wb");
18 fprintf (file, "P6\n%d %d\n255\n", width, height);
20 for (int y = 0; y < height; y++) {
21 fwrite (frame->data[0] + y * frame->linesize[0], 1, width * 3, file);
28 main (int argc, char* argv[])
32 AVFormatContext* format_context = NULL;
33 if (avformat_open_input (&format_context, video, NULL, NULL) != 0) {
34 fprintf (stderr, "avformat_open_input failed.\n");
38 if (avformat_find_stream_info (format_context, NULL) < 0) {
39 fprintf (stderr, "av_find_stream_info failed.\n");
43 av_dump_format (format_context, 0, video, 0);
45 int video_stream = -1;
46 for (uint32_t i = 0; i < format_context->nb_streams; ++i) {
47 if (format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
53 AVCodecContext* codec_context = format_context->streams[video_stream]->codec;
55 AVCodec* codec = avcodec_find_decoder (codec_context->codec_id);
57 fprintf (stderr, "avcodec_find_decoder failed.\n");
61 if (avcodec_open2 (codec_context, codec, NULL) < 0) {
62 fprintf (stderr, "avcodec_open failed.\n");
66 if (codec_context->time_base.num > 1000 && codec_context->time_base.den == 1) {
67 codec_context->time_base.den = 1000;
70 AVFrame* frame = avcodec_alloc_frame ();
72 AVFrame* frame_RGB = avcodec_alloc_frame ();
73 if (frame_RGB == NULL) {
74 fprintf (stderr, "avcodec_alloc_frame failed.\n");
78 int num_bytes = avpicture_get_size (PIX_FMT_RGB24, codec_context->width, codec_context->height);
79 uint8_t* buffer = (uint8_t *) malloc (num_bytes);
81 avpicture_fill ((AVPicture *) frame_RGB, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height);
85 while (av_read_frame (format_context, &packet) >= 0) {
89 if (packet.stream_index == video_stream) {
90 avcodec_decode_video2 (codec_context, frame, &frame_finished, &packet);
93 static struct SwsContext *img_convert_context;
95 if (img_convert_context == NULL) {
96 int w = codec_context->width;
97 int h = codec_context->height;
99 img_convert_context = sws_getContext (
101 codec_context->pix_fmt,
102 w, h, PIX_FMT_RGB24, SWS_BICUBIC,
106 if (img_convert_context == NULL) {
107 fprintf (stderr, "sws_getContext failed.\n");
113 img_convert_context, frame->data, frame->linesize, 0,
114 codec_context->height, frame_RGB->data, frame_RGB->linesize
118 if (i > 100 && i < 150) {
119 save_frame (frame_RGB, codec_context->width, codec_context->height, i);
124 av_free_packet (&packet);
130 avcodec_close(codec_context);
131 avformat_close_input (&format_context);