樹(shù)莓派使用python-librtmp實(shí)現(xiàn)rtmp推流h264的方法
目的是能使用Python進(jìn)行rtmp推流,方便在h264幀里加入彈幕等操作。
librtmp使用的是0.3.0,使用樹(shù)莓派noir官方攝像頭適配的。
通過(guò)wireshark抓ffmpeg的包一點(diǎn)點(diǎn)改動(dòng),最終可以在red5和斗魚(yú)上推流了。
沒(méi)怎么寫(xiě)過(guò)python,有不恰當(dāng)?shù)牡胤秸?qǐng)包涵。
上代碼:
# -- coding: utf-8 --
# http://blog.csdn.net/luhanglei
import picamera
import time
import traceback
import ctypes
from librtmp import *
global meta_packet
global start_time
class Writer(): # camera可以通過(guò)一個(gè)類(lèi)文件的對(duì)象來(lái)輸出,實(shí)現(xiàn)write方法即可
conn = None # rtmp連接
sps = None # 記錄sps幀,發(fā)過(guò)以后就不需要再發(fā)了(抓包看到ffmpeg是這樣的)
pps = None # 同上
sps_len = 0 # 同上
pps_len = 0 # 同上
time_stamp = 0
def __init__(self, conn):
self.conn = conn
def write(self, data):
try:
# 尋找h264幀間隔符
indexs = []
index = 0
data_len = len(data)
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:
indexs.append(index)
index = index + 3
index = index + 1
# 尋找h264幀間隔符 完成
# 通過(guò)間隔符個(gè)數(shù)確定類(lèi)型,樹(shù)莓派攝像頭的第一幀是sps+pps同時(shí)發(fā)的
if len(indexs) == 1: # 非sps pps幀
buf = data[4: len(data)] # 裁掉原來(lái)的頭(00 00 00 01),把幀內(nèi)容拿出來(lái)
buf_len = len(buf)
type = ord(buf[0]) & 0x1f
if type == 0x05: # 關(guān)鍵幀,根據(jù)wire shark抓包結(jié)果,需要拼裝sps pps 幀內(nèi)容 三部分,長(zhǎng)度都用4個(gè)字節(jié)表示
body0 = 0x17
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,
bytes(bytearray(
[(self.pps_len >> 24) & 0xff, (self.pps_len >> 16) & 0xff, (self.pps_len >> 8) & 0xff,
self.pps_len & 0xff])),
self.pps,
bytes(bytearray(
[(buf_len >> 24) & 0xff, (buf_len >> 16) & 0xff, (buf_len >> 8) & 0xff, (buf_len) & 0xff])),
buf
]
mbody = ''.join(data_body_array)
time_stamp = 0 # 第一次發(fā)出的時(shí)候,發(fā)時(shí)間戳0,此后發(fā)真時(shí)間戳
if self.time_stamp != 0:
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
else: # 非關(guān)鍵幀
body0 = 0x27
data_body_array = [bytes(bytearray(
[body0, 0x01, 0x00, 0x00, 0x00, (buf_len >> 24) & 0xff, (buf_len >> 16) & 0xff,
(buf_len >> 8) & 0xff,
(buf_len) & 0xff])), buf]
mbody = ''.join(data_body_array)
# if (self.time_stamp == 0):
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)
elif len(indexs) == 2: # sps pps幀
if self.sps is not None:
return
data_body_array = [bytes(bytearray([0x17, 0x00, 0x00, 0x00, 0x00, 0x01]))]
sps = data[indexs[0] + 4: indexs[1]]
sps_len = len(sps)
pps = data[indexs[1] + 4: len(data)]
pps_len = len(pps)
self.sps = sps
self.sps_len = sps_len
self.pps = pps
self.pps_len = pps_len
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)
except Exception, e:
traceback.print_exc()
def flush(self):
pass
def get_property_string(string): # 返回兩字節(jié)string長(zhǎng)度及string
length = len(string)
return ''.join([chr((length >> 8) & 0xff), chr(length & 0xff), string])
def get_meta_string(string): # 按照meta packet要求格式返回bytes,帶02前綴
return ''.join([chr(0x02), get_property_string(string)])
def get_meta_double(db):
nums = [0x00]
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):
nums = [0x01]
if (isTrue):
nums.append(0x01)
else:
nums.append(0x00)
return ''.join(bytes(bytearray(nums)))
conn = RTMP(
'rtmp://192.168.199.154/oflaDemo/test', # 推流地址
live=True)
librtmp.RTMP_EnableWrite(conn.rtmp)
conn.connect()
start_time = time.time()
# 拼裝視頻格式的數(shù)據(jù)包
meta_body_array = [get_meta_string('@setDataFrame'), get_meta_string('onMetaData'),
bytes(bytearray([0x08, 0x00, 0x00, 0x00, 0x06])), # 兩個(gè)字符串和ECMA array頭,共計(jì)6個(gè)元素,注釋掉了音頻相關(guān)數(shù)據(jù)
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('audiodatarate'), get_meta_double(125.0),
# get_property_string('audiosamplerate'), get_meta_double(44100.0),
# get_property_string('audiosamplesize'), get_meta_double(16.0),
# get_property_string('stereo'), get_meta_boolean(True),
# get_property_string('audiocodecid'), get_meta_double(10.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 id
stream = conn.create_stream(writeable=True)
with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(2)
camera.start_recording(Writer(conn), format='h264', resize=(640, 480), intra_period=25,
quality=25) # 開(kāi)始錄制,數(shù)據(jù)輸出到Writer的對(duì)象里
while True:#永遠(yuǎn)不停止
time.sleep(60)
camera.stop_recording()
camera.stop_preview()
以上這篇樹(shù)莓派使用python-librtmp實(shí)現(xiàn)rtmp推流h264的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3爬蟲(chóng)帶上cookie的實(shí)例代碼
在本篇文章里小編給各位分享的是一篇關(guān)于Python3爬蟲(chóng)帶上cookie的實(shí)例代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-07-07
python正則表達(dá)式修復(fù)網(wǎng)站文章字體不統(tǒng)一的解決方法
python正則表達(dá)式修復(fù)網(wǎng)站文章字體不統(tǒng)一的解決方法,需要的朋友可以參考一下2013-02-02
Flask框架實(shí)現(xiàn)給視圖函數(shù)增加裝飾器操作示例
這篇文章主要介紹了Flask框架實(shí)現(xiàn)給視圖函數(shù)增加裝飾器操作,結(jié)合實(shí)例形式分析了flask框架視圖添加裝飾器的具體操作方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-07-07
Python創(chuàng)建7種不同的文件格式的方法總結(jié)
今天的這篇文章呢,小編來(lái)介紹一下如何通過(guò)Python來(lái)創(chuàng)建各種形式的文件,這里包括了:文本文件、CSV文件、Excel文件、壓縮文件、XML文件、JSON文件和PDF文件,需要的可以參考一下2023-01-01
使用PDB簡(jiǎn)單調(diào)試Python程序簡(jiǎn)明指南
這篇文章主要介紹了使用PDB簡(jiǎn)單調(diào)試Python程序簡(jiǎn)明指南,本文講解了使用PDB調(diào)試程序的簡(jiǎn)單技巧,方便、簡(jiǎn)潔實(shí)用,需要的朋友可以參考下2015-04-04
Python函數(shù)之iterrows(),iteritems(),itertuples()的區(qū)別說(shuō)明
這篇文章主要介紹了Python函數(shù)之iterrows(),iteritems(),itertuples()的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
Pandas庫(kù)中isnull函數(shù)的實(shí)現(xiàn)
isnull()是Pandas庫(kù)中DataFrame和Series對(duì)象的一個(gè)函數(shù),用于檢測(cè)數(shù)據(jù)中的缺失值,本文主要介紹了Pandas庫(kù)中isnull函數(shù)的實(shí)現(xiàn),具有一定參考價(jià)值,感興趣的可以了解一下2024-07-07
Python讀取txt文件數(shù)據(jù)的方法(用于接口自動(dòng)化參數(shù)化數(shù)據(jù))
這篇文章主要介紹了Python讀取txt文件數(shù)據(jù)的方法(用于接口自動(dòng)化參數(shù)化數(shù)據(jù)),需要的朋友可以參考下2018-06-06

