[Audio and video] FFmpeg open video | save the picture

1, initialization FFmpeg

av_register_all (); // initialize FFMPEG call this normal use encoder and decoder

  But this function is now useless, and it is to you to define and system initialization of the encoder and decoder are connected only, then no.

  Now initialising the decoder and the encoder by definition of global variables are initialized. Compared with the original, the only change is to use an array list complete replacement decoder and encoder initialization.

## Reference article: https://www.jianshu.com/p/ebb219ec1c0f

 

 2, a dispensing AVFormatContext, FFMPEG operations can not be performed by this data structure AVFormatContext

AVFormatContext *pFormatCtx = avformat_alloc_context();

  AVFormatContext is a data structure that runs through the many functions it should be used as a parameter.

  This structure contains the contents of a video stream format: AVInputFormat (there is only one of the internal AVOutputFormat same time AVFormatContext or), and AVStream, AVPacket several important data structures and other relevant information, such as title, author, copyright Wait. Some may be in the codec will be used in information, such as: duration, file_size, bit_rate and so on.

 

3, open the video file:

char *file_path = "./movie/ifwithoutu.mp4";
avformat_open_input(&pFormatCtx, file_path, NULL, NULL);

  

 

4, after the video opens, find the file in the video stream

    Find /// circulation flow information included in the video until you find the type of video stream     
    /// put their record is saved to videoStream variable 
    /// Here we deal only with the first video stream audio stream regardless of his 
    for (i = 0; I <pFormatCtx-> nb_streams; I ++) 
    { 
        IF (pFormatCtx-> STREAMS [I] -> codec-> == codec_type AVMEDIA_TYPE_VIDEO) 
        { 
            videoStream = I; 
        } 
    } 
 
    /// If videoStream videos found no explanation is -1 flow 
    IF (videoStream == -1) 
    { 
        the printf ( "Did Not Find A Video stream."); 
        return -1; 
    }    

  

5, the video stream, a decoder to decode open

    ///查找解码器    
    pCodecCtx = pFormatCtx->streams[videoStream]->codec;
    pCodec = avcodec_find_decoder(pCodecCtx->codec_id);
 
    if (pCodec == NULL) 
    {
        printf("Codec not found.");
        return -1;
    }
 
    ///打开解码器
    if (avcodec_open2(pCodecCtx, pCodec, NULL) < 0) 
    {
        printf("Could not open codec.");
        return -1;
    }

  FFmpeg allows us to get to the decoder according to the searched video stream information, without knowing what the actual use of a decoder

 

6, reads the video

     pCodecCtx- y_size = int> * pCodecCtx- width> height; 
    AVPacket packet * = (AVPacket *) the malloc (the sizeof (AVPacket)); // allocate a packet 
    av_new_packet (packet, y_size); // allocate a data packet 
 
    if (av_read_frame (pFormatCtx, Packet) <0) 
    { 
        BREAK; // read over here that video 
    }

  av_read_frame a video is read and stored in a configuration AVPacket

 

7, the video encoded data is compressed, it needs to be decoded

   if (packet->stream_index == videoStream) 
    {        
        ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture,packet);
 
        if (ret < 0) 
        {
            printf("decode error.");
            return -1;
        }
    }    

  

8, the image data obtained by the decoder after decoding is YUV420 format, save it as a document image, it is necessary to convert RGB format

    if (got_picture) 
    {        
        sws_scale( img_convert_ctx,
                   (uint8_t const * const *) pFrame->data,
                   pFrame->linesize, 
                   0, 
                   pCodecCtx->height, 
                   pFrameRGB->data,
                   pFrameRGB->linesize
); }

  

9, RGB images will be written

SaveFrame(pFrameRGB,
                 pCodecCtx->width,
                 pCodecCtx->height,
                 index++
);

  

 

error:

(1) : change AV_PIX_FMT_RGB24

 

Guess you like

Origin www.cnblogs.com/xiexinbei0318/p/11426290.html