FFmpeg fetching pictures from the video stream

Source: Internet
Author: User

The basic flow of capturing images from video: Open video stream Address---get video stream packt-> decode to picture frame--Output picture

I. Initializing FFMPEG

void Ffmpeginit ()
{
Av_register_all (); Avformat_network_init ();
}

If you do not want to output log, set the log level to Av_log_panic.

Two. Open the video.

intOpen (char* URL) {
    context = Avformat_alloc_context ();
Context->interrupt_callback.opaque = this; C++
Context->interrupt_callback.callback = interruptcallback;//Sets the callback function, otherwise it is possible that the ffmpeg has been suspended.
Context->start_time_realtime = Av_gettime ();
avdictionary* options = nullptr; Av_dict_set (&options,"Rtsp_transport", "UDP",0); Open UDP, if TCP is turned on to replace UDP with TCP Av_dict_set (&options,"Stimeout","3000000",0); Set timeout disconnect time int ret =Avformat_open_input (&context, URL, nullptr, &options);   avformat_open_input  returns 0 for open success, less than 0 for open failure
if (ret < 0) return ret;
Ret = Avformat_find_stream_info (context, options);  ///avformat_find_ stream_info  returns 0 for check Stream info success   Less than 0 indicates failure.
    if (options!= nullptr)
    {
     av_dict_free ( Options);
   }  
return ret; }
int INTERRUPT_CB (void *if)//10s Timeout exit    {         return  averror_eof;    }     return 0;            

Three. Read the video package:

Shared_ptr<avpacket>Readpacket () {shared_ptr<AVPacket> packet ((avpacket*) Av_malloc (sizeof(Avpacket)), [&] (Avpacket *p) {av_free_packet (P); Av_freep (&( p);}); Av_init_packet (packet.Get()); Lastframerealtime=Av_gettime (); intRET = av_read_frame (context, packet.Get()); if(Ret >=0)   {        returnpacket; }   Else    {        returnnullptr; }}

Note: You don't have to use smart pointers. I'm sure there will be no memory leaks, so do not change, the code can be written with a bug.

Four. Decoding

1. Initializing the decoder

initdecodercodec{
int ret =-1;
i < context->nb_streams; ++i)
{ *codeccontext = context->streams[i]->codec; if (Codeccontext->codec_type = = Avmedia_type_video) {
//return less than 0, open decoder failed = Avcodec_open2 (Codeccontext, Avcodec_find_decoder (codeccontext->codec_id), &options); }
}
return ret;}

2. Decoding Video Packets

avframe* decodevideopacket (avcodeccontext* codeccontext)
{
avframe* videoframe = Av_frame_alloc ();
Auto hr = Avcodec_decode_video2 (Codeccontext, Frame, &gotframe, packet); if 0 0)
{
return videoframe;
}
Else
{
    Avcodec_free_frame (&videoframe);
Return nullptr
}
}

Output Image:

uint8_t *getpicturedata (int width,int height, int *buffersize)
{
pframeyuv= Av_frame_alloc (); *Out_buffer; New uint8_t[avpicture_get_size (pix_fmt_rgb32, width, height)]; *) PFRAMEYUV, Out_buffer, pix_fmt_rgb32, width, height); Sws_scale (SWS, (constconst0
*buffersize = width * Height * 4;
Return pframeyuv->data[0];

Context is a global variable, if there is a problem, please contact me. 350197870

FFmpeg fetching pictures from the video stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.