Skip to content

Instantly share code, notes, and snippets.

@mikeboers
Last active November 6, 2018 16:33
Show Gist options
  • Save mikeboers/091669058cd565381ed16c436cd7cc87 to your computer and use it in GitHub Desktop.
Save mikeboers/091669058cd565381ed16c436cd7cc87 to your computer and use it in GitHub Desktop.
Threading solution for PyAV #448
import glob
import threading
from queue import Queue
import av
packet_queue = Queue(1)
def read_target():
for name in sorted(glob.glob("data/be*.00*")):
data = open(name, 'rb').read()
packet_queue.put(data)
packet_queue.put(None)
read_thread = threading.Thread(target=read_target)
read_thread.daemon = True
read_thread.start()
class IOBuffer(object):
def __init__(self):
self.buffer = b''
self.open = True
self.pos = 0
def read(self, num):
if self.open and len(self.buffer) < num:
print("GET PACKET")
packet = packet_queue.get(1)
if packet:
self.buffer += packet
else:
print("QUEUE IS CLOSED")
self.open = False
ret = self.buffer[:num]
self.buffer = self.buffer[num:]
self.pos += len(ret)
print('read({}) -> {}'.format(num, len(ret)))
return ret
def tell(self):
print('tell() -> {}'.format(self.pos))
return self.pos
def xseek(self, pos, whence):
raise NotImplementedError()
buf = IOBuffer()
fh = av.open(buf)
print(fh.dumps_format())
for p in fh.demux():
print(p)
for f in p.decode():
print(f)
$ python go.py
GET PACKET
read(32768) -> 318
GET PACKET
read(32768) -> 185
GET PACKET
read(32768) -> 308
GET PACKET
read(32768) -> 339
GET PACKET
read(32768) -> 269
GET PACKET
read(32768) -> 207
GET PACKET
read(32768) -> 220
GET PACKET
read(32768) -> 214
Input #0, matroska,webm, from '':
Metadata:
encoder : Chrome
Duration: N/A, start: 0.000000, bitrate: N/A
Stream #0:0(eng): Audio: opus, 48000 Hz, mono, fltp (default)
<av.Packet of #0, dts=0, pts=0; 26 bytes at 0x10754eb88>
<av.AudioFrame 0, pts=0, 2856 samples at 48000Hz, mono, fltp at 0x109b1eba8>
<av.Packet of #0, dts=60, pts=60; 34 bytes at 0x10754e9a8>
<av.AudioFrame 1, pts=60, 2880 samples at 48000Hz, mono, fltp at 0x109b1ec18>
<av.Packet of #0, dts=119, pts=119; 35 bytes at 0x10754eb88>
<av.AudioFrame 2, pts=119, 2880 samples at 48000Hz, mono, fltp at 0x109b1ecf8>
<av.Packet of #0, dts=180, pts=180; 37 bytes at 0x10754e9a8>
<av.AudioFrame 3, pts=180, 2880 samples at 48000Hz, mono, fltp at 0x109b1ed68>
<av.Packet of #0, dts=240, pts=240; 24 bytes at 0x10754eb88>
<av.AudioFrame 4, pts=240, 2880 samples at 48000Hz, mono, fltp at 0x109b1edd8>
<av.Packet of #0, dts=299, pts=299; 33 bytes at 0x10754e9a8>
<av.AudioFrame 5, pts=299, 2880 samples at 48000Hz, mono, fltp at 0x109b1ee48>
<av.Packet of #0, dts=359, pts=359; 37 bytes at 0x10754eb88>
<av.AudioFrame 6, pts=359, 2880 samples at 48000Hz, mono, fltp at 0x109b1eeb8>
<av.Packet of #0, dts=420, pts=420; 26 bytes at 0x10754e9a8>
<av.AudioFrame 7, pts=420, 2880 samples at 48000Hz, mono, fltp at 0x109b1ef28>
<av.Packet of #0, dts=479, pts=479; 35 bytes at 0x10754eb88>
<av.AudioFrame 8, pts=479, 2880 samples at 48000Hz, mono, fltp at 0x109b1ef98>
<av.Packet of #0, dts=540, pts=540; 33 bytes at 0x10754e9a8>
<av.AudioFrame 9, pts=540, 2880 samples at 48000Hz, mono, fltp at 0x109b1ec88>
<av.Packet of #0, dts=599, pts=599; 52 bytes at 0x10754eb88>
<av.AudioFrame 10, pts=599, 2880 samples at 48000Hz, mono, fltp at 0x109efe0b8>
<av.Packet of #0, dts=660, pts=660; 62 bytes at 0x10754e9a8>
<av.AudioFrame 11, pts=660, 2880 samples at 48000Hz, mono, fltp at 0x109efe128>
<av.Packet of #0, dts=719, pts=719; 66 bytes at 0x10754eb88>
<av.AudioFrame 12, pts=719, 2880 samples at 48000Hz, mono, fltp at 0x109efe048>
<av.Packet of #0, dts=779, pts=779; 65 bytes at 0x10754e9a8>
<av.AudioFrame 13, pts=779, 2880 samples at 48000Hz, mono, fltp at 0x109efe198>
<av.Packet of #0, dts=840, pts=840; 64 bytes at 0x10754eb88>
<av.AudioFrame 14, pts=840, 2880 samples at 48000Hz, mono, fltp at 0x109efe208>
<av.Packet of #0, dts=899, pts=899; 57 bytes at 0x10754e9a8>
<av.AudioFrame 15, pts=899, 2880 samples at 48000Hz, mono, fltp at 0x109efe278>
<av.Packet of #0, dts=960, pts=960; 72 bytes at 0x10754eb88>
<av.AudioFrame 16, pts=960, 2880 samples at 48000Hz, mono, fltp at 0x109efe2e8>
<av.Packet of #0, dts=1020, pts=1020; 62 bytes at 0x10754e9a8>
<av.AudioFrame 17, pts=1020, 2880 samples at 48000Hz, mono, fltp at 0x109efe358>
<av.Packet of #0, dts=1080, pts=1080; 54 bytes at 0x10754eb88>
<av.AudioFrame 18, pts=1080, 2880 samples at 48000Hz, mono, fltp at 0x109efe3c8>
<av.Packet of #0, dts=1140, pts=1140; 70 bytes at 0x10754e9a8>
<av.AudioFrame 19, pts=1140, 2880 samples at 48000Hz, mono, fltp at 0x109efe438>
<av.Packet of #0, dts=1200, pts=1200; 64 bytes at 0x10754eb88>
<av.AudioFrame 20, pts=1200, 2880 samples at 48000Hz, mono, fltp at 0x109efe4a8>
<av.Packet of #0, dts=1260, pts=1260; 41 bytes at 0x10754e9a8>
<av.AudioFrame 21, pts=1260, 2880 samples at 48000Hz, mono, fltp at 0x109efe518>
<av.Packet of #0, dts=1320, pts=1320; 36 bytes at 0x10754eb88>
<av.AudioFrame 22, pts=1320, 2880 samples at 48000Hz, mono, fltp at 0x109efe588>
<av.Packet of #0, dts=1380, pts=1380; 28 bytes at 0x10754e9a8>
<av.AudioFrame 23, pts=1380, 2880 samples at 48000Hz, mono, fltp at 0x109efe5f8>
<av.Packet of #0, dts=1440, pts=1440; 37 bytes at 0x10754eb88>
<av.AudioFrame 24, pts=1440, 2880 samples at 48000Hz, mono, fltp at 0x109efe668>
<av.Packet of #0, dts=1500, pts=1500; 34 bytes at 0x10754e9a8>
<av.AudioFrame 25, pts=1500, 2880 samples at 48000Hz, mono, fltp at 0x109efe6d8>
<av.Packet of #0, dts=1560, pts=1560; 37 bytes at 0x10754eb88>
<av.AudioFrame 26, pts=1560, 2880 samples at 48000Hz, mono, fltp at 0x109efe748>
<av.Packet of #0, dts=1620, pts=1620; 36 bytes at 0x10754e9a8>
<av.AudioFrame 27, pts=1620, 2880 samples at 48000Hz, mono, fltp at 0x109efe7b8>
<av.Packet of #0, dts=1680, pts=1680; 33 bytes at 0x10754eb88>
<av.AudioFrame 28, pts=1680, 2880 samples at 48000Hz, mono, fltp at 0x109efe828>
<av.Packet of #0, dts=1740, pts=1740; 42 bytes at 0x10754e9a8>
<av.AudioFrame 29, pts=1740, 2880 samples at 48000Hz, mono, fltp at 0x109efe898>
<av.Packet of #0, dts=1800, pts=1800; 40 bytes at 0x10754eb88>
<av.AudioFrame 30, pts=1800, 2880 samples at 48000Hz, mono, fltp at 0x109efe908>
<av.Packet of #0, dts=1860, pts=1860; 40 bytes at 0x10754e9a8>
<av.AudioFrame 31, pts=1860, 2880 samples at 48000Hz, mono, fltp at 0x109efe978>
<av.Packet of #0, dts=1920, pts=1920; 36 bytes at 0x10754eb88>
<av.AudioFrame 32, pts=1920, 2880 samples at 48000Hz, mono, fltp at 0x109efe9e8>
<av.Packet of #0, dts=1980, pts=1980; 32 bytes at 0x10754e9a8>
<av.AudioFrame 33, pts=1980, 2880 samples at 48000Hz, mono, fltp at 0x109efea58>
<av.Packet of #0, dts=2040, pts=2040; 34 bytes at 0x10754eb88>
<av.AudioFrame 34, pts=2040, 2880 samples at 48000Hz, mono, fltp at 0x109efeac8>
<av.Packet of #0, dts=2100, pts=2100; 38 bytes at 0x10754e9a8>
<av.AudioFrame 35, pts=2100, 2880 samples at 48000Hz, mono, fltp at 0x109efeb38>
<av.Packet of #0, dts=2160, pts=2160; 36 bytes at 0x10754eb88>
<av.AudioFrame 36, pts=2160, 2880 samples at 48000Hz, mono, fltp at 0x109efeba8>
<av.Packet of #0, dts=2220, pts=2220; 37 bytes at 0x10754e9a8>
<av.AudioFrame 37, pts=2220, 2880 samples at 48000Hz, mono, fltp at 0x109efec18>
<av.Packet of #0, dts=2280, pts=2280; 39 bytes at 0x10754eb88>
<av.AudioFrame 38, pts=2280, 2880 samples at 48000Hz, mono, fltp at 0x109efec88>
GET PACKET
read(32768) -> 207
<av.Packet of #0, dts=2340, pts=2340; 35 bytes at 0x10754e9a8>
<av.AudioFrame 39, pts=2340, 2880 samples at 48000Hz, mono, fltp at 0x109efecf8>
<av.Packet of #0, dts=2400, pts=2400; 31 bytes at 0x10754eb88>
<av.AudioFrame 40, pts=2400, 2880 samples at 48000Hz, mono, fltp at 0x109efed68>
<av.Packet of #0, dts=2460, pts=2460; 41 bytes at 0x10754e9a8>
<av.AudioFrame 41, pts=2460, 2880 samples at 48000Hz, mono, fltp at 0x109efedd8>
<av.Packet of #0, dts=2520, pts=2520; 33 bytes at 0x10754eb88>
<av.AudioFrame 42, pts=2520, 2880 samples at 48000Hz, mono, fltp at 0x109efee48>
<av.Packet of #0, dts=2580, pts=2580; 37 bytes at 0x10754e9a8>
<av.AudioFrame 43, pts=2580, 2880 samples at 48000Hz, mono, fltp at 0x109efeeb8>
GET PACKET
read(32768) -> 212
<av.Packet of #0, dts=2640, pts=2640; 33 bytes at 0x10754eb88>
<av.AudioFrame 44, pts=2640, 2880 samples at 48000Hz, mono, fltp at 0x109efef28>
<av.Packet of #0, dts=2700, pts=2700; 40 bytes at 0x10754e9a8>
<av.AudioFrame 45, pts=2700, 2880 samples at 48000Hz, mono, fltp at 0x109efef98>
<av.Packet of #0, dts=2760, pts=2760; 32 bytes at 0x10754eb88>
<av.AudioFrame 46, pts=2760, 2880 samples at 48000Hz, mono, fltp at 0x109efa048>
<av.Packet of #0, dts=2820, pts=2820; 38 bytes at 0x10754e9a8>
<av.AudioFrame 47, pts=2820, 2880 samples at 48000Hz, mono, fltp at 0x109efa0b8>
<av.Packet of #0, dts=2880, pts=2880; 39 bytes at 0x10754eb88>
<av.AudioFrame 48, pts=2880, 2880 samples at 48000Hz, mono, fltp at 0x109efa128>
GET PACKET
QUEUE IS CLOSED
read(32768) -> 0
read(32768) -> 0
read(32768) -> 0
read(32768) -> 0
<av.Packet of #0, dts=None, pts=None; 0 bytes at 0x10754e5e8>
<av.AudioFrame 49, pts=2880, 24 samples at 48000Hz, mono, fltp at 0x109efa198>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment