Av_register_all ();//initial Dissolve code device and multiplexer
avformatcontext* Ofmt_ctx = null;//It contains a lot of code flow parameters, is a data structure throughout the constant, many functions have to use it as a parameter
First create an encapsulation format,
AVFORMAT_ALLOC_OUTPUT_CONTEXT2 (&OFMT_CTX, NULL, NULL, OUT_FILENAME.C_STR ());//Initialize a AVFORMATCONTEXT structure for output, Video frame rate and width height set in this function
Avstream *st;//constructs an output stream
Avcodec *codec;
st = Avformat_new_stream (OC, NULL);//Initialize output stream
codec = Avcodec_find_encoder (codec_id);//Find MJPEG Decoder
Avcodec_get_context_defaults3 (St->codec, codec);//Request Avstream->codec (Avcodeccontext object) space and set the default value (by Avcodec_ GET_CONTEXT_DEFAULTS3 () settings
St->codec->bit_rate = 400000;//Sets the sampling parameter, which is the bit rate
St->codec->width = 640;//Set the video width is high, here is consistent with the width of the picture to save
St->codec->height = 360;
St->codec->time_base.den = 25;//Set frame rate
St->codec->time_base.num = 1;
ST->CODEC->PIX_FMT = pix_fmt_yuv420p;//Setting pixel format
St->codec->codec_tag = 0;
if (Oc->oformat->flags & Avfmt_globalheader)//Some formats require video stream data header to separate
St->codec->flags |= Codec_flag_global_header;
if (! ( Ofmt_ctx->oformat->flags & Avfmt_nofile)//Open Output video file
{
ret = Avio_open (&OFMT_CTX->PB, Out_filename.c_str (), avio_flag_write);
if (Ret < 0) {
printf ("Could not open output file '%s '", Out_filename.c_str ());
Return
}
}
if (Avformat_write_header (Ofmt_ctx, NULL) < 0)//write header (write file header)
{
printf ("Error occurred when opening output file\n");
Return
}
FILE *file;//Open a JPEG image and read its data, where the maximum image is 1M, if more than 1M, you need to modify 1024*1024 here
Fopen_s (&file, "123.jpg", "RB");
Pkt.size = Fread (MyData, 1, datasize, file);
Pkt.data = MyData;
fclose (file);
if (Av_interleaved_write_frame (Ofmt_ctx, &PKT) < 0)//write image to video
{
printf ("Error muxing packet\n");
Return
}
This place can be directly replaced with other memory images, but the image encoding format to be consistent, this is YUV
Av_free_packet (&PKT);//Release the Frame packet object
Av_write_trailer (OFMT_CTX);//write the end of the document (write file trailer)
delete[]mydata;//Releasing Data Objects
if (Ofmt_ctx &&!) ( Ofmt_ctx->oformat->flags & Avfmt_nofile))
Avio_close (OFMT_CTX->PB);//close the video file
Avformat_free_context (OFMT_CTX);//release output video related data structure
Delete MyData;
The most important is the video encoding and decoding parameter settings