2 #include <libavcodec/avcodec.h>
3 #include <libavformat/avformat.h>
4 #include <libswscale/swscale.h>
7 char const video[] = "/home/carl/Films/A town called panic.divx";//Ghostbusters.avi";
10 save_frame (AVFrame *frame, int width, int height, int N)
14 sprintf (filename, "frame%d.ppm", N);
15 FILE* file = fopen (filename, "wb");
20 fprintf (file, "P6\n%d %d\n255\n", width, height);
22 for (int y = 0; y < height; y++) {
23 fwrite (frame->data[0] + y * frame->linesize[0], 1, width * 3, file);
30 main (int argc, char* argv[])
34 AVFormatContext* format_context = NULL;
35 if (avformat_open_input (&format_context, video, NULL, NULL) != 0) {
36 fprintf (stderr, "avformat_open_input failed.\n");
40 if (avformat_find_stream_info (format_context, NULL) < 0) {
41 fprintf (stderr, "av_find_stream_info failed.\n");
45 av_dump_format (format_context, 0, video, 0);
47 int video_stream = -1;
48 for (uint32_t i = 0; i < format_context->nb_streams; ++i) {
49 if (format_context->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
55 AVCodecContext* codec_context = format_context->streams[video_stream]->codec;
57 AVCodec* codec = avcodec_find_decoder (codec_context->codec_id);
59 fprintf (stderr, "avcodec_find_decoder failed.\n");
63 if (avcodec_open2 (codec_context, codec, NULL) < 0) {
64 fprintf (stderr, "avcodec_open failed.\n");
68 if (codec_context->time_base.num > 1000 && codec_context->time_base.den == 1) {
69 codec_context->time_base.den = 1000;
72 AVFrame* frame = avcodec_alloc_frame ();
74 AVFrame* frame_RGB = avcodec_alloc_frame ();
75 if (frame_RGB == NULL) {
76 fprintf (stderr, "avcodec_alloc_frame failed.\n");
80 int num_bytes = avpicture_get_size (PIX_FMT_RGB24, codec_context->width, codec_context->height);
81 uint8_t* buffer = (uint8_t *) malloc (num_bytes);
83 avpicture_fill ((AVPicture *) frame_RGB, buffer, PIX_FMT_RGB24, codec_context->width, codec_context->height);
87 while (av_read_frame (format_context, &packet) >= 0) {
91 if (packet.stream_index == video_stream) {
92 avcodec_decode_video2 (codec_context, frame, &frame_finished, &packet);
95 static struct SwsContext *img_convert_context;
97 if (img_convert_context == NULL) {
98 int w = codec_context->width;
99 int h = codec_context->height;
101 img_convert_context = sws_getContext (
103 codec_context->pix_fmt,
104 w, h, PIX_FMT_RGB24, SWS_BICUBIC,
108 if (img_convert_context == NULL) {
109 fprintf (stderr, "sws_getContext failed.\n");
115 img_convert_context, frame->data, frame->linesize, 0,
116 codec_context->height, frame_RGB->data, frame_RGB->linesize
120 if (i > 100 && i < 150) {
121 save_frame (frame_RGB, codec_context->width, codec_context->height, i);
126 av_free_packet (&packet);
132 avcodec_close(codec_context);
133 avformat_close_input (&format_context);