为了账号安全,请及时绑定邮箱和手机立即绑定

将碎片 MP4 转换为 MP4

将碎片 MP4 转换为 MP4

呼唤远方 2023-07-27 16:19:23
我正在尝试从 Trafficview.org 中抓取视频帧,但似乎无法弄清楚如何解码数据。我已经监控了通过 Chrome 上的网络选项卡传入的消息,还深入研究了下面代码的输出,并且相当确定数据正在以碎片 MP4 的形式流入。以下是前 100 个左右字节/消息:b'\xfa\x00\x02\x86\xf1B\xc0\x1e\x00\x00\x00\x18ftypiso5\x00\x00\x02\x00iso6mp41\x00\x00\x02jmoov\x00\x00\x00lmvhd\x00\x00\x00 \x00\xdb\x7f\xeb\xb2\xdb\x7f\xeb\xb2\x00\x00\x03\xe8\x00\x00\x00\x00\x00\x01\x00\x00\x01\x00\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'在整个输出中,有很多 moof 和 mdat 对。假设我让这段代码运行 30 秒,如何将这个原始字节字符串转换为 mp4 文件?import jsonfrom websocket import create_connectionurl = 'wss://cctv.trafficview.org:8420/DDOT_CAPTOP_13.vod?progressive'headers = json.dumps({    'Accept-Encoding': 'gzip, deflate, br',    'Accept-Language': 'en-US,en;q=0.9',    'Cache-Control': 'no-cache',    'Connection': 'Upgrade',    'Host': 'cctv.trafficview.org:8420',    'Origin': 'https://trafficview.org',    'Pragma': 'no-cache',    'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',    'Sec-WebSocket-Key': 'FzWbrsoHFsJWzvWGJ04ffw==',    'Sec-WebSocket-Version': '13',    'Upgrade': 'websocket',    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36',})ws = create_connection(url, headers=headers)# Then send a message through the tunnelws.send('ping')# Here you will view the message return from the tunnelflag = 3000output = b''while flag > 0:    output += ws.recv()    flag -= 1
查看完整描述

1 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

ws = create_connection(url, headers=headers)

# Then send a message through the tunnel

ws.send('ping')


start = timeit.default_timer()

flag = True

output = []

while flag:

    output.append(ws.recv())

    if timeit.default_timer() - start > 90:

        flag = False


result = output[0][8:]


for msg in output[1:]:

    if msg[0] == 249:

        moofmdat = b''

        moof = b''

        continue


    if msg[0] == 252:

        vidbuf = msg[4:]


    if msg[0] == 251:

        moof += msg[4:]


    if msg[0] == 254:

        mdat = msg[4:]


    if msg[0] == 255:

        moofmdat += moof

        moofmdat += mdat

        moofmdat += vidbuf

        result += moofmdat


with open('test.mp4', 'wb') as file:

    file.write(result)

弄清楚了。MOOV 标头有 8 个字节的不必要信息,必须删除。每个附加消息(除了 PBT_Begin 和 PBT_End)都有 4 个字节的玩家特定数据。只需清理每条消息并按正确的顺序放置即可。然后将原始字节保存为 mp4,瞧,视频在 vlc 中播放。


查看完整回答
反对 回复 2023-07-27
  • 1 回答
  • 0 关注
  • 73 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信