当前位置:   article > 正文

H264视频编码成MP4文件_java h264转mp4

java h264转mp4

        最近需要将H264视频编码成MP4格式。研究了一下,一种方法是采用ffmpeg库,可以先将H264文件解码,再编码生成MP4文件,但这种方式效率较低,10M的视频可能需要几秒钟才能完成。另一种方式根据MP4文件协议直接将H264包封装成MP4格式,由于是直接基于MP4的封装,因而效率很高。H264可以很方便的封装成FLV文件,但MP4格式格式相对比较复杂,封装起来会比较麻烦。由于没时间研究MP4协议,在Google Code上找到一个开源的MP4编解码库Mp4v2(https://code.google.com/p/mp4v2/),通过Mp4v2可以很方便的将H264编码成MP4格式文件。为了方便使用,基于该库封装了一个MP4Encoder类,MP4Encoder封装的接口如下。目前仅支持将H264文件或数据帧编码成MP4文件。

  1. class MP4Encoder
  2. {
  3. public:
  4. MP4Encoder(void);
  5. ~MP4Encoder(void);
  6. public:
  7. // open or creat a mp4 file.
  8. MP4FileHandle CreateMP4File(const char *fileName,int width,int height,int timeScale = 90000,int frameRate = 25);
  9. // wirte 264 metadata in mp4 file.
  10. bool Write264Metadata(MP4FileHandle hMp4File,LPMP4ENC_Metadata lpMetadata);
  11. // wirte 264 data, data can contain multiple frame.
  12. int WriteH264Data(MP4FileHandle hMp4File,const unsigned char* pData,int size);
  13. // close mp4 file.
  14. void CloseMP4File(MP4FileHandle hMp4File);
  15. // convert H264 file to mp4 file.
  16. // no need to call CreateMP4File and CloseMP4File,it will create/close mp4 file automaticly.
  17. bool WriteH264File(const char* pFile264,const char* pFileMp4);
  18. // Prase H264 metamata from H264 data frame
  19. static bool PraseMetadata(const unsigned char* pData,int size,MP4ENC_Metadata &metadata);
  20. };

客户端调用示例代码:

  1. #include <stdio.h>
  2. #include "MP4Encoder\MP4Encoder.h"
  3. int main(int argc, char** argv)
  4. {
  5. MP4Encoder mp4Encoder;
  6. // convert H264 file to mp4 file
  7. mp4Encoder.WriteH264File("test.264","test.mp4");
  8. }


MP4Encoder完整的代码如下:

  1. /********************************************************************
  2. filename: MP4Encoder.h
  3. created: 2013-04-16
  4. author: firehood
  5. purpose: MP4编码器,基于开源库mp4v2实现(https://code.google.com/p/mp4v2/)。
  6. *********************************************************************/
  7. #pragma once
  8. #include "mp4v2\mp4v2.h"
  9. // NALU单元
  10. typedef struct _MP4ENC_NaluUnit
  11. {
  12. int type;
  13. int size;
  14. unsigned char *data;
  15. }MP4ENC_NaluUnit;
  16. typedef struct _MP4ENC_Metadata
  17. {
  18. // video, must be h264 type
  19. unsigned int nSpsLen;
  20. unsigned char Sps[1024];
  21. unsigned int nPpsLen;
  22. unsigned char Pps[1024];
  23. } MP4ENC_Metadata,*LPMP4ENC_Metadata;
  24. class MP4Encoder
  25. {
  26. public:
  27. MP4Encoder(void);
  28. ~MP4Encoder(void);
  29. public:
  30. // open or creat a mp4 file.
  31. MP4FileHandle CreateMP4File(const char *fileName,int width,int height,int timeScale = 90000,int frameRate = 25);
  32. // wirte 264 metadata in mp4 file.
  33. bool Write264Metadata(MP4FileHandle hMp4File,LPMP4ENC_Metadata lpMetadata);
  34. // wirte 264 data, data can contain multiple frame.
  35. int WriteH264Data(MP4FileHandle hMp4File,const unsigned char* pData,int size);
  36. // close mp4 file.
  37. void CloseMP4File(MP4FileHandle hMp4File);
  38. // convert H264 file to mp4 file.
  39. // no need to call CreateMP4File and CloseMP4File,it will create/close mp4 file automaticly.
  40. bool WriteH264File(const char* pFile264,const char* pFileMp4);
  41. // Prase H264 metamata from H264 data frame
  42. static bool PraseMetadata(const unsigned char* pData,int size,MP4ENC_Metadata &metadata);
  43. private:
  44. // read one nalu from H264 data buffer
  45. static int ReadOneNaluFromBuf(const unsigned char *buffer,unsigned int nBufferSize,unsigned int offSet,MP4ENC_NaluUnit &nalu);
  46. private:
  47. int m_nWidth;
  48. int m_nHeight;
  49. int m_nFrameRate;
  50. int m_nTimeScale;
  51. MP4TrackId m_videoId;
  52. };

MP4Encoder.cpp

  1. /********************************************************************
  2. filename: MP4Encoder.cpp
  3. created: 2013-04-16
  4. author: firehood
  5. purpose: MP4编码器,基于开源库mp4v2实现(https://code.google.com/p/mp4v2/)。
  6. *********************************************************************/
  7. #include "MP4Encoder.h"
  8. #include <string.h>
  9. #define BUFFER_SIZE (1024*1024)
  10. MP4Encoder::MP4Encoder(void):
  11. m_videoId(NULL),
  12. m_nWidth(0),
  13. m_nHeight(0),
  14. m_nTimeScale(0),
  15. m_nFrameRate(0)
  16. {
  17. }
  18. MP4Encoder::~MP4Encoder(void)
  19. {
  20. }
  21. MP4FileHandle MP4Encoder::CreateMP4File(const char *pFileName,int width,int height,int timeScale/* = 90000*/,int frameRate/* = 25*/)
  22. {
  23. if(pFileName == NULL)
  24. {
  25. return false;
  26. }
  27. // create mp4 file
  28. MP4FileHandle hMp4file = MP4Create(pFileName);
  29. if (hMp4file == MP4_INVALID_FILE_HANDLE)
  30. {
  31. printf("ERROR:Open file fialed.\n");
  32. return false;
  33. }
  34. m_nWidth = width;
  35. m_nHeight = height;
  36. m_nTimeScale = 90000;
  37. m_nFrameRate = 25;
  38. MP4SetTimeScale(hMp4file, m_nTimeScale);
  39. return hMp4file;
  40. }
  41. bool MP4Encoder::Write264Metadata(MP4FileHandle hMp4File,LPMP4ENC_Metadata lpMetadata)
  42. {
  43. m_videoId = MP4AddH264VideoTrack
  44. (hMp4File,
  45. m_nTimeScale,
  46. m_nTimeScale / m_nFrameRate,
  47. m_nWidth, // width
  48. m_nHeight,// height
  49. lpMetadata->Sps[1], // sps[1] AVCProfileIndication
  50. lpMetadata->Sps[2], // sps[2] profile_compat
  51. lpMetadata->Sps[3], // sps[3] AVCLevelIndication
  52. 3); // 4 bytes length before each NAL unit
  53. if (m_videoId == MP4_INVALID_TRACK_ID)
  54. {
  55. printf("add video track failed.\n");
  56. return false;
  57. }
  58. MP4SetVideoProfileLevel(hMp4File, 0x01); // Simple Profile @ Level 3
  59. // write sps
  60. MP4AddH264SequenceParameterSet(hMp4File,m_videoId,lpMetadata->Sps,lpMetadata->nSpsLen);
  61. // write pps
  62. MP4AddH264PictureParameterSet(hMp4File,m_videoId,lpMetadata->Pps,lpMetadata->nPpsLen);
  63. return true;
  64. }
  65. int MP4Encoder::WriteH264Data(MP4FileHandle hMp4File,const unsigned char* pData,int size)
  66. {
  67. if(hMp4File == NULL)
  68. {
  69. return -1;
  70. }
  71. if(pData == NULL)
  72. {
  73. return -1;
  74. }
  75. MP4ENC_NaluUnit nalu;
  76. int pos = 0, len = 0;
  77. while (len = ReadOneNaluFromBuf(pData,size,pos,nalu))
  78. {
  79. if(nalu.type == 0x07) // sps
  80. {
  81. // 添加h264 track
  82. m_videoId = MP4AddH264VideoTrack
  83. (hMp4File,
  84. m_nTimeScale,
  85. m_nTimeScale / m_nFrameRate,
  86. m_nWidth, // width
  87. m_nHeight, // height
  88. nalu.data[1], // sps[1] AVCProfileIndication
  89. nalu.data[2], // sps[2] profile_compat
  90. nalu.data[3], // sps[3] AVCLevelIndication
  91. 3); // 4 bytes length before each NAL unit
  92. if (m_videoId == MP4_INVALID_TRACK_ID)
  93. {
  94. printf("add video track failed.\n");
  95. return 0;
  96. }
  97. MP4SetVideoProfileLevel(hMp4File, 1); // Simple Profile @ Level 3
  98. MP4AddH264SequenceParameterSet(hMp4File,m_videoId,nalu.data,nalu.size);
  99. }
  100. else if(nalu.type == 0x08) // pps
  101. {
  102. MP4AddH264PictureParameterSet(hMp4File,m_videoId,nalu.data,nalu.size);
  103. }
  104. else
  105. {
  106. int datalen = nalu.size+4;
  107. unsigned char *data = new unsigned char[datalen];
  108. // MP4 Nalu前四个字节表示Nalu长度
  109. data[0] = nalu.size>>24;
  110. data[1] = nalu.size>>16;
  111. data[2] = nalu.size>>8;
  112. data[3] = nalu.size&0xff;
  113. memcpy(data+4,nalu.data,nalu.size);
  114. if(!MP4WriteSample(hMp4File, m_videoId, data, datalen,MP4_INVALID_DURATION, 0, 1))
  115. {
  116. return 0;
  117. }
  118. delete[] data;
  119. }
  120. pos += len;
  121. }
  122. return pos;
  123. }
  124. int MP4Encoder::ReadOneNaluFromBuf(const unsigned char *buffer,unsigned int nBufferSize,unsigned int offSet,MP4ENC_NaluUnit &nalu)
  125. {
  126. int i = offSet;
  127. while(i<nBufferSize)
  128. {
  129. if(buffer[i++] == 0x00 &&
  130. buffer[i++] == 0x00 &&
  131. buffer[i++] == 0x00 &&
  132. buffer[i++] == 0x01
  133. )
  134. {
  135. int pos = i;
  136. while (pos<nBufferSize)
  137. {
  138. if(buffer[pos++] == 0x00 &&
  139. buffer[pos++] == 0x00 &&
  140. buffer[pos++] == 0x00 &&
  141. buffer[pos++] == 0x01
  142. )
  143. {
  144. break;
  145. }
  146. }
  147. if(pos == nBufferSize)
  148. {
  149. nalu.size = pos-i;
  150. }
  151. else
  152. {
  153. nalu.size = (pos-4)-i;
  154. }
  155. nalu.type = buffer[i]&0x1f;
  156. nalu.data =(unsigned char*)&buffer[i];
  157. return (nalu.size+i-offSet);
  158. }
  159. }
  160. return 0;
  161. }
  162. void MP4Encoder::CloseMP4File(MP4FileHandle hMp4File)
  163. {
  164. if(hMp4File)
  165. {
  166. MP4Close(hMp4File);
  167. hMp4File = NULL;
  168. }
  169. }
  170. bool MP4Encoder::WriteH264File(const char* pFile264,const char* pFileMp4)
  171. {
  172. if(pFile264 == NULL || pFileMp4 == NULL)
  173. {
  174. return false;
  175. }
  176. MP4FileHandle hMp4File = CreateMP4File(pFileMp4,352,288);
  177. if(hMp4File == NULL)
  178. {
  179. printf("ERROR:Create file failed!");
  180. return false;
  181. }
  182. FILE *fp = fopen(pFile264, "rb");
  183. if(!fp)
  184. {
  185. printf("ERROR:open file failed!");
  186. return false;
  187. }
  188. fseek(fp, 0, SEEK_SET);
  189. unsigned char *buffer = new unsigned char[BUFFER_SIZE];
  190. int pos = 0;
  191. while(1)
  192. {
  193. int readlen = fread(buffer+pos, sizeof(unsigned char), BUFFER_SIZE-pos, fp);
  194. if(readlen<=0)
  195. {
  196. break;
  197. }
  198. readlen += pos;
  199. int writelen = 0;
  200. for(int i = readlen-1; i>=0; i--)
  201. {
  202. if(buffer[i--] == 0x01 &&
  203. buffer[i--] == 0x00 &&
  204. buffer[i--] == 0x00 &&
  205. buffer[i--] == 0x00
  206. )
  207. {
  208. writelen = i+5;
  209. break;
  210. }
  211. }
  212. writelen = WriteH264Data(hMp4File,buffer,writelen);
  213. if(writelen<=0)
  214. {
  215. break;
  216. }
  217. memcpy(buffer,buffer+writelen,readlen-writelen+1);
  218. pos = readlen-writelen+1;
  219. }
  220. fclose(fp);
  221. delete[] buffer;
  222. CloseMP4File(hMp4File);
  223. return true;
  224. }
  225. bool MP4Encoder:: PraseMetadata(const unsigned char* pData,int size,MP4ENC_Metadata &metadata)
  226. {
  227. if(pData == NULL || size<4)
  228. {
  229. return false;
  230. }
  231. MP4ENC_NaluUnit nalu;
  232. int pos = 0;
  233. bool bRet1 = false,bRet2 = false;
  234. while (int len = ReadOneNaluFromBuf(pData,size,pos,nalu))
  235. {
  236. if(nalu.type == 0x07)
  237. {
  238. memcpy(metadata.Sps,nalu.data,nalu.size);
  239. metadata.nSpsLen = nalu.size;
  240. bRet1 = true;
  241. }
  242. else if((nalu.type == 0x08))
  243. {
  244. memcpy(metadata.Pps,nalu.data,nalu.size);
  245. metadata.nPpsLen = nalu.size;
  246. bRet2 = true;
  247. }
  248. pos += len;
  249. }
  250. if(bRet1 && bRet2)
  251. {
  252. return true;
  253. }
  254. return false;
  255. }


     

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/491211
推荐阅读
相关标签
  

闽ICP备14008679号