目的是能使用Python进行rtmp推流,方便在h264帧里加入弹幕等操作。

    librtmp使用的是0.3.0,使用树莓派noir官方摄像头适配的。

    通过wireshark抓ffmpeg的包一点点改动,最终可以在red5和斗鱼上推流了。

    没怎么写过python,有不恰当的地方请包涵。转载请注明出处:http://blog.csdn.net/luhanglei/article/details/78403900

    上代码:

    1. def __init__(self, conn):while index < data_len - 3:if ord(data[index]) == 0x00 and ord(data[index + 1]) == 0x00 and ord( data[index + 2]) == 0x00 and ord(data[index + 3]) == 0x01: type = ord(buf[0]) & 0x1f data_body_array = [bytes(bytearray( [body0, 0x01, 0x00, 0x00, 0x00, (self.sps_len >> 24) & 0xff, (self.sps_len >> 16) & 0xff, (self.sps_len >> 8) & 0xff, self.sps_len & 0xff])), self.sps, [(self.pps_len >> 24) & 0xff, (self.pps_len >> 16) & 0xff, (self.pps_len >> 8) & 0xff, [(buf_len >> 24) & 0xff, (buf_len >> 16) & 0xff, (buf_len >> 8) & 0xff, (buf_len) & 0xff])), mbody = ''.join(data_body_array) time_stamp = int((time.time() - start_time) * 1000) packet_body = RTMPPacket(type=PACKET_TYPE_VIDEO, format=PACKET_SIZE_LARGE, channel=0x06, timestamp=time_stamp, body=mbody) packet_body.packet.m_nInfoField2 = 1 data_body_array = [bytes(bytearray( [body0, 0x01, 0x00, 0x00, 0x00, (buf_len >> 24) & 0xff, (buf_len >> 16) & 0xff, (buf_len) & 0xff])), buf] mbody = ''.join(data_body_array) self.time_stamp = int((time.time() - start_time) * 1000) packet_body = RTMPPacket(type=PACKET_TYPE_VIDEO, format=PACKET_SIZE_MEDIUM, channel=0x06, timestamp=self.time_stamp, body=mbody) self.conn.send_packet(packet_body) data_body_array = [bytes(bytearray([0x17, 0x00, 0x00, 0x00, 0x00, 0x01]))] sps = data[indexs[0] + 4: indexs[1]] pps = data[indexs[1] + 4: len(data)] data_body_array.append(sps[1:4]) data_body_array.append(bytes(bytearray([0xff, 0xe1, (sps_len >> 8) & 0xff, sps_len & 0xff]))) data_body_array.append(sps) data_body_array.append(bytes(bytearray([0x01, (pps_len >> 8) & 0xff, pps_len & 0xff]))) data_body_array.append(pps) data_body = ''.join(data_body_array) body_packet = RTMPPacket(type=PACKET_TYPE_VIDEO, format=PACKET_SIZE_LARGE, channel=0x06, timestamp=0, body=data_body) body_packet.packet.m_nInfoField2 = 1 self.conn.send_packet(meta_packet, queue=True) self.conn.send_packet(body_packet, queue=True)def get_property_string(string): return ''.join([chr((length >> 8) & 0xff), chr(length & 0xff), string])def get_meta_string(string): return ''.join([chr(0x02), get_property_string(string)]) fp = ctypes.pointer(ctypes.c_double(db)) cp = ctypes.cast(fp, ctypes.POINTER(ctypes.c_longlong))for i in range(7, -1, -1): nums.append((cp.contents.value >> (i * 8)) & 0xff)return ''.join(bytes(bytearray(nums)))def get_meta_boolean(isTrue):return ''.join(bytes(bytearray(nums)))'rtmp://192.168.199.154/oflaDemo/test', librtmp.RTMP_EnableWrite(conn.rtmp)meta_body_array = [get_meta_string('@setDataFrame'), get_meta_string('onMetaData'), bytes(bytearray([0x08, 0x00, 0x00, 0x00, 0x06])), get_property_string('width'), get_meta_double(640.0), get_property_string('height'), get_meta_double(480.0), get_property_string('videodatarate'), get_meta_double(0.0), get_property_string('framerate'), get_meta_double(25.0), get_property_string('videocodecid'), get_meta_double(7.0), get_property_string('encoder'), get_meta_string('Lavf57.56.101'), bytes(bytearray([0x00, 0x00, 0x09]))meta_body = ''.join(meta_body_array)print meta_body.encode('hex')meta_packet = RTMPPacket(type=PACKET_TYPE_INFO, format=PACKET_SIZE_LARGE, channel=0x04, timestamp=0, body=meta_body)meta_packet.packet.m_nInfoField2 = 1 stream = conn.create_stream(writeable=True)with picamera.PiCamera() as camera: camera.start_recording(Writer(conn), format='h264', resize=(640, 480), intra_period=25,