歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 在Android程序中獲取avc中的數據

在Android程序中獲取avc中的數據

日期:2017/3/1 10:51:40   编辑:Linux編程

在Android程序中獲取avc中的數據,也就是AVCDecoderConfigurationRecord 。

你可以先錄制一小段視頻保存在sd卡中。

然後根據以下代碼來獲取數據。這樣就可以根據不同的手機或者不同的分辨率來調整sps pps或者avc了

  1. package com.ppmeet.util;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import android.util.Log;
  9. /**
  10. * class name:MyDetect<BR>
  11. * class description:get avc data<BR>
  12. * PS: <BR>
  13. *
  14. * @version 1.00 2011/09/21
  15. * @author CODYY)peijiangping
  16. */
  17. public class MyDetect {
  18. // We should check 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x**, 'A', 'v', 'c', 'C',
  19. // 0x01
  20. final static int MAX_HEADER_LENGTH = 128;
  21. public static byte[] spsData = new byte[MAX_HEADER_LENGTH];
  22. public static int spsDataLength = 0;
  23. public static byte[] ppsData = new byte[MAX_HEADER_LENGTH];
  24. public static int ppsDataLength = 0;
  25. public static int mdataPosition = 32;
  26. private static byte[] checkBuffer = new byte[1024 * 4];
  27. private static int headerLength;
  28. public static void writeConfig(String discFile) {
  29. File newFile = new File(discFile);
  30. try {
  31. OutputStream os = new FileOutputStream(newFile.getAbsolutePath());
  32. os.write(checkBuffer, 0, headerLength);
  33. os.flush();
  34. os.close();
  35. } catch (IOException e) {
  36. }
  37. }
  38. public static void getHeaderData(byte[] buffer) {
  39. /*
  40. * put_byte(pb, 1); // version put_byte(pb, sps[1]); // profile
  41. * put_byte(pb, sps[2]); // profile compat put_byte(pb, sps[3]); //
  42. * level put_byte(pb, 0xff); // 6 bits reserved (111111) + 2 bits nal
  43. * size length - 1 (11)
  44. *
  45. * put_byte(pb, 0xe1); // 3 bits reserved (111) + 5 bits number of sps
  46. * (00001)
  47. *
  48. *
  49. * put_be16(pb, sps_size); put_buffer(pb, sps, sps_size); put_byte(pb,
  50. * 1); // number of pps put_be16(pb, pps_size); put_buffer(pb, pps,
  51. * pps_size);
  52. */
  53. mdataPosition = (int) (buffer[0] & 0xFF) * 256
  54. + (int) (buffer[1] & 0xFF);
  55. spsDataLength = (int) (buffer[6] & 0xFF) * 256
  56. + (int) (buffer[7] & 0xFF);
  57. for (int i = 0; i < spsDataLength; i++)
  58. spsData[i] = buffer[8 + i];
  59. ppsDataLength = (int) (buffer[9 + spsDataLength] & 0xFF) * 256
  60. + (int) (buffer[10 + spsDataLength] & 0xFF);
  61. for (int i = 0; i < ppsDataLength; i++)
  62. ppsData[i] = buffer[11 + spsDataLength + i];
  63. Log.d("TEAONLY", "spsDataLength = " + spsDataLength
  64. + " ppsDataLength = " + ppsDataLength);
  65. headerLength = 6 + 2 + 2 + 1 + spsDataLength + ppsDataLength;
  66. for (int i = 0; i < headerLength; i++) {
  67. checkBuffer[i] = buffer[i];
  68. }
  69. checkBuffer[0] = (byte) ((mdataPosition >> 8) & 0xFF);
  70. checkBuffer[1] = (byte) (mdataPosition & 0xFF);
  71. }
  72. public static boolean checkMP4_MDAT(String fileName) throws IOException {
  73. File fin = new File(fileName);
  74. if (fin.isFile()) {
  75. InputStream is = new FileInputStream(fin.getAbsolutePath());
  76. int pos;
  77. int fms = 0;
  78. boolean isOK;
  79. int n;
  80. int fpos = 0;
  81. while (true) {
  82. isOK = false;
  83. n = is.read(checkBuffer);
  84. if (n < 0) {
  85. break;
  86. }
  87. for (pos = 0; pos < n; pos++) {
  88. fpos++;
  89. byte tmp = checkBuffer[pos];
  90. switch (tmp) {
  91. case (byte) 'm':
  92. if (fms == 0)
  93. fms = 1;
  94. else
  95. fms = 0;
  96. break;
  97. case (byte) 'd':
  98. if (fms == 1)
  99. fms = 2;
  100. else
  101. fms = 0;
  102. break;
  103. case (byte) 'a':
  104. if (fms == 2)
  105. fms = 3;
  106. else
  107. fms = 0;
  108. break;
  109. case (byte) 't':
  110. if (fms == 3)
  111. fms = 4;
  112. else
  113. fms = 0;
  114. break;
  115. default:
  116. fms = 0;
  117. break;
  118. }
  119. if (fms == 4) {
  120. isOK = true;
  121. break;
  122. }
  123. }
  124. if (isOK) {
  125. Log.d("TEAONLY",
  126. "MP4 file MDAT position is OK.**************");
  127. mdataPosition = fpos;
  128. return true;
  129. }
  130. }
  131. }
  132. return false;
  133. }
  134. public static boolean checkMP4_MOOV(String fileName) throws IOException {
  135. File fin = new File(fileName);
  136. if (fin.isFile()) {
  137. InputStream is = new FileInputStream(fin.getAbsolutePath());
  138. int pos;
  139. int fms = 0;
  140. boolean isOK;
  141. int n;
  142. while (true) {
  143. isOK = false;
  144. n = is.read(checkBuffer);
  145. if (n < 0) {
  146. break;
  147. }
  148. for (pos = 0; pos < n; pos++) {
  149. byte tmp = checkBuffer[pos];
  150. switch (tmp) {
  151. case (byte) 'a':
  152. if (fms == 0)
  153. fms = 1;
  154. else
  155. fms = 0;
  156. break;
  157. case (byte) 'v':
  158. if (fms == 1)
  159. fms = 2;
  160. else
  161. fms = 0;
  162. break;
  163. case (byte) 'c':
  164. if (fms == 2)
  165. fms = 3;
  166. else
  167. fms = 0;
  168. break;
  169. case (byte) 'C':
  170. if (fms == 3)
  171. fms = 4;
  172. else
  173. fms = 0;
  174. break;
  175. case (byte) 0x01:
  176. if (fms == 4)
  177. fms = 5;
  178. else
  179. fms = 0;
  180. break;
  181. default:
  182. fms = 0;
  183. break;
  184. }
  185. if (fms == 5) {
  186. isOK = true;
  187. break;
  188. }
  189. }
  190. if (isOK) {
  191. Log.d("TEAONLY", "MP4 file SPS PPS is OK.**************");
  192. // ��ʼ��ȡPPS�Լ�SPS���
  193. for (int i = 0; i < checkBuffer.length - pos; i++) {
  194. checkBuffer[i] = checkBuffer[i + pos];
  195. }
  196. if (pos > checkBuffer.length - MAX_HEADER_LENGTH) {
  197. is.read(checkBuffer, checkBuffer.length - pos,
  198. MAX_HEADER_LENGTH);
  199. }
  200. getHeaderData(checkBuffer);
  201. return true;
  202. }
  203. }
  204. }
  205. return false;
  206. }
  207. }
Copyright © Linux教程網 All Rights Reserved