简体   繁体   English

使用python将位流保存到文件

[英]Save a bitstream to a file using python

I need to output an h.265 (or hevc, is the same) bit-stream onto an str file in python. 我需要将h.265(或hevc,是相同的)位流输出到python中的str文件中。

I have a bitstream file and i select some data from this file to save it to a new one. 我有一个比特流文件,我从该文件中选择了一些数据以将其保存到新文件中。 I use bitstring module to process the bitstream file. 我使用bitstring模块来处理位流文件。

Edit: My question is how to create a new bitstream file and insert data into. 编辑:我的问题是如何创建一个新的比特流文件并将数据插入其中。

Take a look at struct 看看结构

A quick example: 一个简单的例子:

import struct
characters = "Hello World"
with open(filepath, 'wb') as f:
    for char in characters:
        # @B means to pack native (LSB or MSB) to size unsigned char (1 byte)
        packed = struct.pack('@B', char)
        f.write(packed)

Check out the part about Joining BitArrays (base class of BitStream) in this part of the bitstring documentation. 在位串文档的这一部分中,查看有关加入BitArrays(BitStream的基类)的部分。 How to join the substreams depends on how you have them in the first place. 如何加入子流取决于您首先如何拥有它们。

For writing the bitstream to a file, use the method 'toFile' of the Bits class , which is a base class of BitStream. 要将比特流写入文件,请使用Bits类的方法“ toFile”, 该类是BitStream的基类。

f = open('fileToWriteTo', 'wb')
bitstreamObject.tofile(f)

If you want to write multiple substreams one after another, you can open the file in append mode the next times you write something. 如果要一个接一个地编写多个子流,则下次编写文件时,可以在附加模式下打开文件。

f = open('fileToWriteTo', 'ab')
nextSubstream.tofile(f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM