在Android程序中获取avc中的数据

在Android程序中获取avc中的数据,也就是AVCDecoderConfigurationRecord 。

你可以先录制一小段视频保存在sd卡中。

然后根据以下代码来获取数据。这样就可以根据不同的手机或者不同的分辨率来调整sps pps或者avc了

package com.ppmeet.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * class name:MyDetect<BR>
 * class description:动态获取avc中的数据<BR>
 * PS: <BR>
 * 
 * @version 1.00 2011/10/21
 * @author CODYY)peijiangping
 */
public class MyDetect {
	final static int MAX_HEADER_LENGTH = 128;
	public static int mdataPosition = 32;
	private static byte[] checkBuffer = new byte[1024 * 4];
	public static byte[] avcdata = null;

	public static boolean checkMP4_MOOV(String fileName) throws IOException {
		File fin = new File(fileName);
		if (fin.isFile()) {
			InputStream is = new FileInputStream(fin.getAbsolutePath());
			int pos;
			int fms = 0;
			boolean isOK;
			int n;
			while (true) {
				isOK = false;
				n = is.read(checkBuffer);
				if (n < 0) {
					break;
				}
				for (pos = 0; pos < n; pos++) {
					byte tmp = checkBuffer[pos];
					switch (tmp) {
					case (byte) 'a':
						if (fms == 0)
							fms = 1;
						else
							fms = 0;
						break;

					case (byte) 'v':
						if (fms == 1)
							fms = 2;
						else
							fms = 0;
						break;
					case (byte) 'c':
						if (fms == 2)
							fms = 3;
						else
							fms = 0;
						break;
					case (byte) 'C':
						if (fms == 3)
							fms = 4;
						else
							fms = 0;
						break;
					case (byte) 0x01:
						if (fms == 4)
							fms = 5;
						else
							fms = 0;
						break;
					default:
						fms = 0;
						break;
					}
					if (fms == 5) {
						isOK = true;
						break;
					}
				}
				if (isOK) {
					for (int i = 0; i < checkBuffer.length - pos; i++) {
						checkBuffer[i] = checkBuffer[i + pos];
					}
					if (pos > checkBuffer.length - MAX_HEADER_LENGTH) {
						is.read(checkBuffer, checkBuffer.length - pos,
								MAX_HEADER_LENGTH);
					}
					getAVCData(checkBuffer);
					return true;
				}
			}
		}
		return false;
	}

	private static byte[] getAVCData(byte[] avc) {
		int avcsize = 0;
		for (int index = 0; index < avc.length; index++) {
			for (int i = 0; i < avc.length; i++) {
				if (i > 31 && avc[i] == 0) {
					avcsize = i;
					break;
				}
			}
			avcdata = new byte[avcsize];
			System.arraycopy(avc, 0, avcdata, 0, avcsize);
		}
		return avcdata;
	}
}



原文链接: http://blog.csdn.net/peijiangping1989/article/details/6951648

你可能感兴趣的:(在Android程序中获取avc中的数据)