librtmp接收flv流中提取h264码流:根据多个资料汇总
rtmpdump可以下载rtmp流并保存成flv文件。
如果要对流中的音频或视频单独处理,需要根据flv协议分别提取。
简单修改rtmpdump代码,增加相应功能。
1 提取音频:
rtmpdump程序在Download函数中循环下载:
....
do
{
....
nRead = RTMP_Read(rtmp, buffer, bufferSize);
....
}while(!RTMP_ctrlC && nRead > -1 && RTMP_IsConnected(rtmp) && !RTMP_IsTimedout(rtmp));
....
原程序是收到后写文件,生成flv。
现在,在写之前分别提取音视频,提取音频比较简单,直接分析buffer(参考RTMP_Write函数里的方法).
注意的是,rtmpdump里用的是RTMP_Read来接收,注意它的参数。为了方便,也可以直接用RTMP_ReadPacket。后面的视频使用RTMP_ReadPacket来接收并处理。
int RTMP_Write2(RTMP *r, const char *buf, int size)
{
RTMPPacket *pkt = &r->m_write;
char *pend, *enc;
int s2 = size, ret, num;
if (size < 11) {
/* FLV pkt too small */
return 0;
}
if (buf[0] == 'F' && buf[1] == 'L' && buf[2] == 'V')
{
buf += 13;
s2 -= 13;
}
pkt->m_packetType = *buf++;
pkt->m_nBodySize = AMF_DecodeInt24(buf);
buf += 3;
pkt->m_nTimeStamp = AMF_DecodeInt24(buf);
buf += 3;
pkt->m_nTimeStamp |= *buf++ << 24;
buf += 3;
s2 -= 11;
if (((pkt->m_packetType == RTMP_PACKET_TYPE_AUDIO
|| pkt->m_packetType == RTMP_PACKET_TYPE_VIDEO) &&
!pkt->m_nTimeStamp) || pkt->m_packetType == RTMP_PACKET_TYPE_INFO)
{
pkt->m_headerType = RTMP_PACKET_SIZE_LARGE;
if (pkt->m_packetType == RTMP_PACKET_TYPE_INFO)
pkt->m_nBodySize += 16;
}
else
{
pkt->m_headerType = RTMP_PACKET_SIZE_MEDIUM;
}
BYTE outbuf2[640];
int nLen2 = 640;
AVManager::GetInstance()->Decode((BYTE*)(pkt->m_body+1), pkt->m_nBodySize-1, outbuf2, nLen2);
//实际音频内容为pkt->m_body+1,大小是pkt->m_nBodySize-1。这里的声音是speex编码。
为什么跳过第一字节,可以参考:http://bbs.rosoo.net/thread-16488-1-1.html
evt_OnReceivePacket((char*)outbuf2, nLen2);//回调出来
RTMPPacket_Free(pkt);
pkt->m_nBytesRead = 0;
2
视频处理
可以参考rtmpsrv.c
把nRead = RTMP_Read(rtmp, buffer, bufferSize);改成:
RTMPPacket pc = { 0 }, ps = { 0 };
bool bFirst = true;
while (RTMP_ReadPacket(rtmp, &pc))
{
if (RTMPPacket_IsReady(&pc))
{
if (pc.m_packetType == RTMP_PACKET_TYPE_VIDEO && RTMP_ClientPacket(rtmp, &pc))
{
bool bIsKeyFrame = false;
if (result == 0x17)//I frame
{
bIsKeyFrame = true;
}
else if (result == 0x27)
{
bIsKeyFrame = false;
}
static unsigned char const start_code[4] = {0x00, 0x00, 0x00, 0x01};
fwrite(start_code, 1, 4, pf );
//int ret = fwrite(pc.m_body + 9, 1, pc.m_nBodySize-9, pf);
if( bFirst) {
//AVCsequence header
//ioBuffer.put(foredata);
//获取sps
int spsnum = data[10]&0x1f;
int number_sps = 11;
int count_sps = 1;
while (count_sps<=spsnum){
int spslen =(data[number_sps]&0x000000FF)<<8 |(data[number_sps+1]&0x000000FF);
number_sps += 2;
fwrite(data+number_sps, 1, spslen, pf );
fwrite(start_code, 1, 4, pf );
//ioBuffer.put(data,number_sps, spslen);
//ioBuffer.put(foredata);
number_sps += spslen;
count_sps ++;
}
//获取pps
int ppsnum = data[number_sps]&0x1f;
int number_pps = number_sps+1;
int count_pps = 1;
while (count_pps<=ppsnum){
int ppslen =(data[number_pps]&0x000000FF)<<8|data[number_pps+1]&0x000000FF;
number_pps += 2;
//ioBuffer.put(data,number_pps,ppslen);
//ioBuffer.put(foredata);
fwrite(data+number_pps, 1, ppslen, pf );
fwrite(start_code, 1, 4, pf );
number_pps += ppslen;
count_pps ++;
}
bFirst =false;
} else {
//AVCNALU
int len =0;
int num =5;
//ioBuffer.put(foredata);
while(num<pc.m_nBodySize)
{
len =(data[num]&0x000000FF)<<24|(data[num+1]&0x000000FF)<<16|(data[num+2]&0x000000FF)<<8|data[num+3]&0x000000FF;
num = num+4;
//ioBuffer.put(data,num,len);
//ioBuffer.put(foredata);
fwrite(data+num, 1, len, pf );
fwrite(start_code, 1, 4, pf );
num = num + len;
}
}
}
}
具体视频分析的见: http://blog.csdn.net/cssmhyl/article/details/8128478
http://blog.chinaunix.net/uid-15063109-id-4273162.html
librtmp接收flv流中提取h264码流:根据多个资料汇总的更多相关文章
- [转]【流媒體】H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流
[流媒體]H264—MP4格式及在MP4文件中提取H264的SPS.PPS及码流 SkySeraph Apr 1st 2012 Email:skyseraph00@163.com 一.MP4格式基本 ...
- 从H264码流中获取视频宽高 (SPS帧) 升级篇
之前写过 <从H264码流中获取视频宽高 (SPS帧)> . 但发现很多局限性,而且有时解出来是错误的. 所以重新去研究了. 用了 官方提供的代码库来解析. 花了点时间,从代码库里单独把解 ...
- H264码流中SPS PPS详解<转>
转载地址:https://zhuanlan.zhihu.com/p/27896239 1 SPS和PPS从何处而来? 2 SPS和PPS中的每个参数起什么作用? 3 如何解析SDP中包含的H.264的 ...
- 从H264码流中获取视频宽高 (SPS帧)
获取.h264视频宽高的方法 花了2个通宵终于搞定.(后面附上完整代码) http://write.blog.csdn.net/postedit/7852406 图像的高和宽在H264的SPS帧中.在 ...
- H264码流解析及NALU
ffmpeg 从mp4上提取H264的nalu http://blog.csdn.net/gavinr/article/details/7183499 639 /* bitstream fil ...
- H264码流打包分析
转自:http://www.360doc.com/content/13/0124/08/9008018_262076786.shtml SODB 数据比特串-->最原始的编码数据 RBSP ...
- H264码流打包分析(精华)
H264码流打包分析 SODB 数据比特串-->最原始的编码数据 RBSP 原始字节序列载荷-->在SODB的后面填加了结尾比特(RBSP trailing bits 一个bit“1”)若 ...
- H264编码原理以及I帧、B和P帧详解, H264码流结构分析
H264码流结构分析 http://blog.csdn.net/chenchong_219/article/details/37990541 1.码流总体结构: h264的功能分为两层,视频编码层(V ...
- RTP协议全解析(H264码流和PS流)
转自:http://blog.csdn.net/chen495810242/article/details/39207305 写在前面:RTP的解析,网上找了很多资料,但是都不全,所以我力图整理出一个 ...
随机推荐
- cx_freeze打包EXE文件
创建setup.py文件 import osimport sysfrom cx_Freeze import setup, Executable build_exe_options = dict(pac ...
- LeetCode OJ:Implement strStr()(实现子字符串查找)
Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle ...
- Java常用类:StringBuilder
一.介绍 2 3 4 5 //同样,StringBuilder也是final修饰的不可变,相比String来说,继承了AbstractStringBuilder,StringBuffer也是同样继承了 ...
- mac下初始化eclipse的安卓开发ndk开发环境
最近电脑由windows换成mac了,很多环境都要重新搭建,顺便纪录下,方便以后查阅. 1.先到eclipse官网下载最新版eclipse,我下载的是neon版,下载后直接解压到即可使用(前提是你安装 ...
- leetCode之Median of Two Sorted Arrays
[题目描述] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of ...
- 6个变态的C语言程序
转载自 陈浩 coolshell.cn 下面的六个程序片段主要完成这些事情: 输出Hello, World 混乱C语言的源代码 下面的所有程序都可以在GCC下编译通过,只有最后一个需要动用C++的编译 ...
- 剑指offer--40.翻转单词顺序列
时间限制:1秒 空间限制:32768K 热度指数:276854 本题知识点: 字符串 题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写 ...
- a的样式
.myAucCItem a { color: rgb(71,71,71);} .myAucCItem a:hover { color: rgb(71,71,71); text-decoration: ...
- 神奇的 ViewDragHelper,让你轻松定制拥有拖拽能力的 ViewGroup
为了吸引大家的注意力,先给大家看一张动图: 相信这种效果大家都见过吧?我第一次见到这样的效果时,心里也痒痒的,急于想实现这种功能,后来因为拖延症的问题,就一直没有去弄这件事.现在这段时间,工作比较轻闲 ...
- HDU 1374
http://acm.hdu.edu.cn/showproblem.php?pid=1374 已知三点坐标,求三点确定的圆的周长 #include <iostream> #include ...