With FFMPEG, you can easily encode audio and video files and write files.
The followingCodeIt is to encode 5 1280*720 images and write them to the file.
The Code is a bit messy, but I hope it will be helpful to my friends who want to learn this.
[CPP] View plaincopy
-
- Cfile file [5];
-
- Byte* Sztxt [5];
-
- IntNwidth = 0;
-
- IntNheight = 0;
-
-
- IntNdatalen = 0;
-
-
- IntNlen;
-
-
- Cstring csfilename;
-
- For(IntFilei = 1; filei <= 5; filei ++)
-
- {
- Csfilename. Format ("E: \ pics \ % d.bmp", Filei );
-
- File [filei-1]. Open (csfilename, cfile: moderead | cfile: typebinary );
-
- Nlen = file [filei-1]. getlength ();
-
-
- Sztxt [filei-1] =New Byte[Nlen];
-
- File [filei-1]. Read (sztxt [filei-1], nlen );
-
- File [filei-1]. Close ();
-
- // Bmp bmi; // bitmapinfo BMI;
-
- // Int nheadlen = sizeof (BMP );
-
- Bitmapfileheader BMP fheader;
-
- Bitmapinfoheader BMP iheader;
-
- Memcpy (& BMP fheader, sztxt [filei-1],Sizeof(Bitmapfileheader ));
-
-
- IntNheadlen = BMP fheader. bfoffbits-Sizeof(Bitmapfileheader );
- Memcpy (& BMP iheader, sztxt [filei-1] +Sizeof(Bitmapfileheader), nheadlen );
-
-
- Nwidth = BMP iheader. biwidth;// 464; // bmi.bmp info. bmiheader. biwidth ;//;
-
- Nheight = BMP iheader. biheight;// 362; // bmi.bmp info. bmiheader. biheight ;//;
-
-
- Sztxt [filei-1] + = BMP fheader. bfoffbits;
-
- Ndatalen = nLen-bmpFHeader.bfOffBits;
-
- }
-
- Av_register_all ();
-
- Avcodec_register_all ();
-
- Avframe * m_prgbframe =NewAvframe [1];// RGB frame data
-
- Avframe * m_pyuvframe =NewAvframe [1];// YUV frame data
-
- Avcodeccontext * c = NULL;
-
- Avcodeccontext * in_c = NULL;
-
- Avcodec * pcodech264;// Encoder
- Uint8_t * yuv_buff;//
-
-
- // Find the h264 Encoder
-
- Pcodech264 = avcodec_find_encoder (codec_id_h264 );
-
- If(! Pcodech264)
-
- {
-
- Fprintf (stderr,"H264 codec not found \ n");
-
- Exit (1 );
-
- }
-
- C = avcodec_alloc_context3 (pcodech264 );
-
- C-> bit_rate = 3000000;// Put sample Parameters
-
- C-> width = nwidth;//
-
- C-> Height = nheight;//
-
-
- // Frames per second
-
- Avrational rate;
-
- Rate. num = 1;
-
- Rate. Den = 25;
- C-> time_base = rate;// (Avrational) {25 };
-
- C-> gop_size = 10;// Emit one intra frame every ten Frames
-
- C-> max_ B _frames = 1;
-
- C-> thread_count = 1;
-
- C-> pix_fmt = pix_fmt_yuv420p;// Pix_fmt_rgb24;
-
-
- // Av_opt_set (c-> priv_data,/* "preset" */"libvpx-1080p.ffpreset",/* "slow" */null, 0 );
- // Enable the Encoder
-
- If(Avcodec_open2 (C, pcodech264, null) <0)
-
- Trace ("Cannot open the encoding library");
-
-
- IntSize = C-> width * C-> height;
-
-
- Yuv_buff = (uint8_t *) malloc (size * 3)/2 );// Size for YUV 420
-
- // Fill RGB image data with RGB Frames
-
- Uint8_t * rgb_buff =NewUint8_t [ndatalen];
-
-
- // Image Encoding
-
- IntOutbuf_size = 100000;
-
- Uint8_t * outbuf = (uint8_t *) malloc (outbuf_size );
-
- IntU_size = 0;
-
- File* F = NULL;
- Char* Filename ="E: \ pics \ mydata. h264";
-
- F = fopen (filename,"WB");
-
- If(! F)
-
- {
-
- Trace ("Cocould not open % s \ n", Filename );
-
- Exit (1 );
-
- }
-
-
- // Initialize swscontext
- Swscontext * scxt = sws_getcontext (c-> width, C-> height, pix_fmt_bgr24, C-> width, C-> height, pix_fmt_yuv420p, sws_point, null );
-
-
- Avpacket avpkt;
-
-
- // Avframe * ptframe = new avframe
-
- For(IntI = 0; I <250; ++ I)
-
- {
-
-
- // Avframe * m_pyuvframe = new avframe [1];
-
- IntIndex = (I/25) % 5;
-
- Memcpy (rgb_buff, sztxt [Index], ndatalen );
-
-
- Avpicture_fill (avpicture *) m_prgbframe, (uint8_t *) rgb_buff, pix_fmt_rgb24, nwidth, nheight );
-
-
- // Fill the YUV buffer with the YUV Frame
-
- Avpicture_fill (avpicture *) m_pyuvframe, (uint8_t *) yuv_buff, pix_fmt_yuv420p, nwidth, nheight );
-
- // Flip the RGB image
-
- M_prgbframe-> data [0] + = m_prgbframe-> linesize [0] * (nheight-1 );
-
- M_prgbframe-> linesize [0] * =-1;
-
- M_prgbframe-> data [1] + = m_prgbframe-> linesize [1] * (nheight/2-1 );
-
- M_prgbframe-> linesize [1] * =-1;
- M_prgbframe-> data [2] + = m_prgbframe-> linesize [2] * (nheight/2-1 );
-
- M_prgbframe-> linesize [2] * =-1;
-
-
-
- // Convert RGB to YUV
-
- Sws_scale (scxt, m_prgbframe-> data, m_prgbframe-> linesize, 0, C-> height, m_pyuvframe-> data, m_pyuvframe-> linesize );
-
-
- IntGot_packet_ptr = 0;
- Av_init_packet (& avpkt );
-
- Avpkt. Data = outbuf;
-
- Avpkt. size = outbuf_size;
-
- U_size = avcodec_encode_video2 (C, & avpkt, m_pyuvframe, & got_packet_ptr );
-
- If(U_size = 0)
-
- {
-
- Fwrite (avpkt. Data, 1, avpkt. Size, F );
-
- }
-
- }
-
- Fclose (f );
-
- Delete[] M_prgbframe;
-
- Delete[] M_pyuvframe;
-
- Delete[] Rgb_buff;
-
- Free (outbuf );
-
- Avcodec_close (C );
-
- Av_free (C );