Original:
Http://blog.chinaunix.net/uid-24203478-id-3035890.html
The version used is FFMPEG 0.6.3.
Use the compilation command:
Gcc-O tutorial01 tutorial01.c-lavutil-lavformat-lavcodec-lswscale
The source program is as follows:
[CPP]
View plaincopy
- # Include <libavcodec/avcodec. h>
- # Include <libavformat/avformat. h>
- # Include <libswscale/swscale. h>
- # Include <stdio. h>
- # Define debug () fprintf (stderr, "% s # % I \ n" ,__ func __,__ line __)
- Static struct swscontext * img_convert_ctx;
- Void saveframe (avframe * pframe, int width, int height, int IFRAME)
- {
- File * pfile;
- Char szfilename [32];
- Int y;
- Sprintf (szfilename, "frame % d. ppm", IFRAME );
- Pfile = fopen (szfilename, "WB ");
- If (pfile = NULL ){
- Return;
- }
- Fprintf (pfile, "P6 \ n % d \ n255 \ n", width, height );
- For (y = 0; y
- Fwrite (pframe-> data [0] + y * pframe-> linesize [0], 1, width * 3, pfile );
- }
- Fclose (pfile );
- }
- Int main (INT argc, char * argv [])
- {
- Avformatcontext * pformatctx;
- Int I, videostream;
- Avcodeccontext * pcodecctx;
- Avcodec * pcodec;
- Avframe * pframe;
- Avframe * pframergb;
- Avpacket packet;
- Int framefinished;
- Int numbytes;
- Uint8_t * buffer;
- If (argc <2 ){
- Printf ("Please provide a movie file \ n ");
- Return-1;
- }
- Fprintf (stderr, "Arg OK. \ n ");
- Debug ();
- Av_register_all ();
- Fprintf (stderr, "av_register_all () OK. \ n ");
- Debug ();
- Pformatctx = avformat_alloc_context (); // allocate space
- Fprintf (stderr, "avformat_alloc_context () OK. \ n ");
- Debug ();
- If (av_open_input_file (& pformatctx, argv [1], null, 0, null )! = 0 ){
- // If (avformat_open_input (& pformatctx, argv [1], null, null )! = 0) {// open the input file
- Return-1;
- }
- Fprintf (stderr, "avformat_open_input () OK. \ n ");
- Debug ();
- If (av_find_stream_info (pformatctx) <0 ){
- Return-1;
- }
- Fprintf (stderr, "av_find_stream_info () OK. \ n ");
- Debug ();
- Dump_format (pformatctx, 0, argv [1], 0 );
- Fprintf (stderr, "dump_format () OK. \ n ");
- Debug ();
- Videostream =-1;
- For (I = 0; I <pformatctx-> nb_streams; I ++ ){
- If (pformatctx-> streams [I]-> codec-> codec_type = codec_type_video ){
- Videostream = I;
- Break;
- }
- }
- Fprintf (stderr, "Find codec_type_video OK. \ n ");
- Debug ();
- If (videostream =-1 ){
- Return-1;
- }
- Debug ();
- Pcodecctx = pformatctx-> streams [videostream]-> codec;
- Img_convert_ctx = sws_getcontext (
- Pcodecctx-> width,
- Pcodecctx-> height,
- Pcodecctx-> pix_fmt,
- Pcodecctx-> width,
- Pcodecctx-> height,
- Pix_fmt_rgb24,
- Sws_bicubic,
- Null, null, null );
- Fprintf (stderr, "sws_getcontext () OK. \ n ");
- Debug ();
- Pcodec = avcodec_find_decoder (pcodecctx-> codec_id );
- If (pcodec = NULL ){
- Fprintf (stderr, "unsupported codec! \ N ");
- Return-1;
- }
- Fprintf (stderr, "avcodec_find_decoder () OK. \ n ");
- Debug ();
- If (avcodec_open (pcodecctx, pcodec) <0 ){
- Return-1;
- }
- Fprintf (stderr, "avcodec_open () OK. \ n ");
- Debug ();
- Pframe = avcodec_alloc_frame ();
- Fprintf (stderr, "avcodec_alloc_frame () OK. \ n ");
- Debug ();
- Pframergb = avcodec_alloc_frame ();
- If (pframergb = NULL ){
- Return-1;
- }
- Fprintf (stderr, "avcodec_alloc_frame () OK. \ n ");
- Debug ();
- Numbytes = avpicture_get_size (pix_fmt_rgb24, pcodecctx-> width,
- Pcodecctx-> height );
- Fprintf (stderr, "avpicture_get_size () OK. \ n ");
- Debug ();
- Buffer = (uint8_t *) av_malloc (numbytes * sizeof (uint8_t ));
- Debug ();
- Avpicture_fill (avpicture *) pframergb, buffer, pix_fmt_rgb24,
- Pcodecctx-> width, pcodecctx-> height );
- Fprintf (stderr, "avpicture_fill () OK. \ n ");
- Debug (); I = 0;
- While (av_read_frame (pformatctx, & Packet)> = 0 ){
- If (packet. stream_index = videostream ){
- Avcodec_decode_video (pcodecctx, pframe,
- & Framefinished,
- Packet. Data,
- Packet. size );
- If (framefinished ){
- Sws_scale (img_convert_ctx,
- (Const uint8_t **) pframe-> data,
- Pframe-> linesize, 0,
- Pcodecctx-> height,
- Pframergb-> data,
- Pframergb-> linesize );
- If (++ I <= 5 ){
- Saveframe (pframergb, pcodecctx-> width, pcodecctx-> height, I );
- }
- }
- }
- Av_free_packet (& Packet );
- }
- Debug ();
- Av_free (buffer );
- Av_free (pframergb );
- Av_free (pframe );
- Avcodec_close (pcodecctx );
- Av_close_input_file (pformatctx );
- Return 0;
- }