Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 1
    @t-mart: It just occured to me that Numpy has its own binary data loader, numpy.frombuffer(), which could be used for the same purpose. Commented Sep 12, 2019 at 9:02
  • As an improvement, from things I just read in the struct package: 1. The returned values from struct functions are "a tuple even if it contains exactly one item", so list.extend() might be better than append to avoid nested data 2. "Creating a Struct object once and calling its methods is more efficient than calling the struct functions with the same format since the format string only needs to be compiled once." Since we're repeating this call for a (large?) file, seems like a good optimization. 3. Also,Struct objects expose a size property, which is simpler than 32 // 8
    – t-mart
    Commented Sep 12, 2019 at 9:10
  • 1
    Yes, the struct-using code could be greatly improved for correctness and efficiency; it was mostly meant to demonstrate that it's still a very simple parser (literally a list of samples). The best option seems to be either array.frombuffer() which I forgot or numpy.frombuffer() which I didn't know previously, either of which should completely avoid creating objects for every sample. Commented Sep 12, 2019 at 9:15
  • array.frombuffer() unfortunately only uses the native endianness of the current machine. I suppose you could fix this at ffmpeg-time to match your own. stackoverflow.com/a/23320951/235992 Numpy types (docs.scipy.org/doc/numpy/user/basics.types.html) also seem to lack specification of endianness, however you could do something like docs.scipy.org/doc/numpy/user/…
    – t-mart
    Commented Sep 12, 2019 at 9:29
  • Yes, but it still allows you to do if sys.byteorder != "big": arr.byteswap(), which is a bit more annoying than having the module take care of it, but ultimately performs the same thing. (Hopefully.) Commented Sep 12, 2019 at 9:40