flv FLV帧全面分析。研究心得。 视频帧结构

 

 

http://blog.csdn.net/peijiangping1989/article/details/6999224

转载于:https://www.cnblogs.com/spiderdzl/p/3848661.html

要获取ws推送的flv格式视频,您可以使用以下步骤: 1. 建立WebSocket连接并接收数据流。 2. 处理数据流并解析FLV视频信息。 3. 从FLV视频中提取视频。 下面是一些示例代码,演示如何获取FLV视频: ``` import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; public class FlvParser { private static final int FLV_HEADER_SIZE = 9; private static final int PREVIOUS_TAG_SIZE_FIELD_SIZE = 4; private static final int TAG_HEADER_SIZE = 11; private static final int TAG_TYPE_VIDEO = 0x09; private static final int AVC_PACKET_TYPE_SEQUENCE_HEADER = 0x00; private static final int AVC_PACKET_TYPE_NALU = 0x01; private static final int TAG_HEADER_TOTAL_SIZE = PREVIOUS_TAG_SIZE_FIELD_SIZE + TAG_HEADER_SIZE; private static final int NAL_HEADER_SIZE = 4; private static final byte[] NAL_HEADER = new byte[]{0x00, 0x00, 0x00, 0x01}; private ByteBuffer mByteBuffer = ByteBuffer.allocate(1024 * 1024); // 1MB private List<byte[]> mNaluList = new ArrayList<>(); public void parse(byte[] data, int length) { mByteBuffer.put(data, 0, length); mByteBuffer.flip(); while (mByteBuffer.remaining() > TAG_HEADER_TOTAL_SIZE) { int previousTagSize = mByteBuffer.getInt(); int tagType = mByteBuffer.get() & 0xff; int dataSize = ((mByteBuffer.get() & 0xff) << 16) | ((mByteBuffer.get() & 0xff) << 8) | (mByteBuffer.get() & 0xff); int timestamp = ((mByteBuffer.get() & 0xff) << 16) | ((mByteBuffer.get() & 0xff) << 8) | (mByteBuffer.get() & 0xff) | ((mByteBuffer.get() & 0xff) << 24); int streamId = ((mByteBuffer.get() & 0xff) << 16) | ((mByteBuffer.get() & 0xff) << 8) | (mByteBuffer.get() & 0xff); if (tagType == TAG_TYPE_VIDEO) { int frameType = (mByteBuffer.get() & 0xff) >> 4; int codecId = mByteBuffer.get() & 0x0f; if (codecId == 7) { // AVC handleAvcPacket(dataSize); } } mByteBuffer.position(mByteBuffer.position() + dataSize + PREVIOUS_TAG_SIZE_FIELD_SIZE); if (mByteBuffer.remaining() < TAG_HEADER_TOTAL_SIZE) { mByteBuffer.compact(); break; } } mByteBuffer.compact(); } private void handleAvcPacket(int dataSize) { int avcPacketType = mByteBuffer.get() & 0xff; if (avcPacketType == AVC_PACKET_TYPE_SEQUENCE_HEADER) { // TODO: parse SPS and PPS } else if (avcPacketType == AVC_PACKET_TYPE_NALU) { byte[] naluData = new byte[dataSize - 1]; mByteBuffer.get(naluData); mNaluList.add(naluData); } while (mNaluList.size() > 0) { byte[] naluData = mNaluList.get(0); int naluLength = naluData.length + NAL_HEADER_SIZE; byte[] frameData = new byte[naluLength]; System.arraycopy(NAL_HEADER, 0, frameData, 0, NAL_HEADER_SIZE); System.arraycopy(naluData, 0, frameData, NAL_HEADER_SIZE, naluData.length); // TODO: handle frameData mNaluList.remove(0); } } } ``` 在上面的示例代码中,`parse()`方法处理FLV视频流,`handleAvcPacket()`方法解析AVC数据包并提取NAL单元,`NAL_HEADER`是NAL单元的头部。 当您成功解析NAL单元后,可以将它们组合成完整的视频,并对其进行处理。 希望这可以帮助您获取ws推送的flv格式视频
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值