Use FFMPEG for h264 Encoding

Source: Internet
Author: User

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

  1. Cfile file [5];
  2. Byte* Sztxt [5];
  3. IntNwidth = 0;
  4. IntNheight = 0;
  5. IntNdatalen = 0;
  6. IntNlen;
  7. Cstring csfilename;
  8. For(IntFilei = 1; filei <= 5; filei ++)
  9. {
  10. Csfilename. Format ("E: \ pics \ % d.bmp", Filei );
  11. File [filei-1]. Open (csfilename, cfile: moderead | cfile: typebinary );
  12. Nlen = file [filei-1]. getlength ();
  13. Sztxt [filei-1] =New Byte[Nlen];
  14. File [filei-1]. Read (sztxt [filei-1], nlen );
  15. File [filei-1]. Close ();
  16. // Bmp bmi; // bitmapinfo BMI;
  17. // Int nheadlen = sizeof (BMP );
  18. Bitmapfileheader BMP fheader;
  19. Bitmapinfoheader BMP iheader;
  20. Memcpy (& BMP fheader, sztxt [filei-1],Sizeof(Bitmapfileheader ));
  21. IntNheadlen = BMP fheader. bfoffbits-Sizeof(Bitmapfileheader );
  22. Memcpy (& BMP iheader, sztxt [filei-1] +Sizeof(Bitmapfileheader), nheadlen );
  23. Nwidth = BMP iheader. biwidth;// 464; // bmi.bmp info. bmiheader. biwidth ;//;
  24. Nheight = BMP iheader. biheight;// 362; // bmi.bmp info. bmiheader. biheight ;//;
  25. Sztxt [filei-1] + = BMP fheader. bfoffbits;
  26. Ndatalen = nLen-bmpFHeader.bfOffBits;
  27. }
  28. Av_register_all ();
  29. Avcodec_register_all ();
  30. Avframe * m_prgbframe =NewAvframe [1];// RGB frame data
  31. Avframe * m_pyuvframe =NewAvframe [1];// YUV frame data
  32. Avcodeccontext * c = NULL;
  33. Avcodeccontext * in_c = NULL;
  34. Avcodec * pcodech264;// Encoder
  35. Uint8_t * yuv_buff;//
  36. // Find the h264 Encoder
  37. Pcodech264 = avcodec_find_encoder (codec_id_h264 );
  38. If(! Pcodech264)
  39. {
  40. Fprintf (stderr,"H264 codec not found \ n");
  41. Exit (1 );
  42. }
  43. C = avcodec_alloc_context3 (pcodech264 );
  44. C-> bit_rate = 3000000;// Put sample Parameters
  45. C-> width = nwidth;//
  46. C-> Height = nheight;//
  47. // Frames per second
  48. Avrational rate;
  49. Rate. num = 1;
  50. Rate. Den = 25;
  51. C-> time_base = rate;// (Avrational) {25 };
  52. C-> gop_size = 10;// Emit one intra frame every ten Frames
  53. C-> max_ B _frames = 1;
  54. C-> thread_count = 1;
  55. C-> pix_fmt = pix_fmt_yuv420p;// Pix_fmt_rgb24;
  56. // Av_opt_set (c-> priv_data,/* "preset" */"libvpx-1080p.ffpreset",/* "slow" */null, 0 );
  57. // Enable the Encoder
  58. If(Avcodec_open2 (C, pcodech264, null) <0)
  59. Trace ("Cannot open the encoding library");
  60. IntSize = C-> width * C-> height;
  61. Yuv_buff = (uint8_t *) malloc (size * 3)/2 );// Size for YUV 420
  62. // Fill RGB image data with RGB Frames
  63. Uint8_t * rgb_buff =NewUint8_t [ndatalen];
  64. // Image Encoding
  65. IntOutbuf_size = 100000;
  66. Uint8_t * outbuf = (uint8_t *) malloc (outbuf_size );
  67. IntU_size = 0;
  68. File* F = NULL;
  69. Char* Filename ="E: \ pics \ mydata. h264";
  70. F = fopen (filename,"WB");
  71. If(! F)
  72. {
  73. Trace ("Cocould not open % s \ n", Filename );
  74. Exit (1 );
  75. }
  76. // Initialize swscontext
  77. Swscontext * scxt = sws_getcontext (c-> width, C-> height, pix_fmt_bgr24, C-> width, C-> height, pix_fmt_yuv420p, sws_point, null );
  78. Avpacket avpkt;
  79. // Avframe * ptframe = new avframe
  80. For(IntI = 0; I <250; ++ I)
  81. {
  82. // Avframe * m_pyuvframe = new avframe [1];
  83. IntIndex = (I/25) % 5;
  84. Memcpy (rgb_buff, sztxt [Index], ndatalen );
  85. Avpicture_fill (avpicture *) m_prgbframe, (uint8_t *) rgb_buff, pix_fmt_rgb24, nwidth, nheight );
  86. // Fill the YUV buffer with the YUV Frame
  87. Avpicture_fill (avpicture *) m_pyuvframe, (uint8_t *) yuv_buff, pix_fmt_yuv420p, nwidth, nheight );
  88. // Flip the RGB image
  89. M_prgbframe-> data [0] + = m_prgbframe-> linesize [0] * (nheight-1 );
  90. M_prgbframe-> linesize [0] * =-1;
  91. M_prgbframe-> data [1] + = m_prgbframe-> linesize [1] * (nheight/2-1 );
  92. M_prgbframe-> linesize [1] * =-1;
  93. M_prgbframe-> data [2] + = m_prgbframe-> linesize [2] * (nheight/2-1 );
  94. M_prgbframe-> linesize [2] * =-1;
  95. // Convert RGB to YUV
  96. Sws_scale (scxt, m_prgbframe-> data, m_prgbframe-> linesize, 0, C-> height, m_pyuvframe-> data, m_pyuvframe-> linesize );
  97. IntGot_packet_ptr = 0;
  98. Av_init_packet (& avpkt );
  99. Avpkt. Data = outbuf;
  100. Avpkt. size = outbuf_size;
  101. U_size = avcodec_encode_video2 (C, & avpkt, m_pyuvframe, & got_packet_ptr );
  102. If(U_size = 0)
  103. {
  104. Fwrite (avpkt. Data, 1, avpkt. Size, F );
  105. }
  106. }
  107. Fclose (f );
  108. Delete[] M_prgbframe;
  109. Delete[] M_pyuvframe;
  110. Delete[] Rgb_buff;
  111. Free (outbuf );
  112. Avcodec_close (C );
  113. Av_free (C );

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.