Skip to content

Commit 36476e6

Browse files
committed
Add ffmpeg example
1 parent c33f7da commit 36476e6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

websocket/test_ffmpeg.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python3
2+
3+
import asyncio
4+
import websockets
5+
import sys
6+
7+
async def hello(uri):
8+
async with websockets.connect(uri) as websocket:
9+
10+
proc = await asyncio.create_subprocess_exec(
11+
'ffmpeg', '-nostdin', '-loglevel', 'quiet', '-i', sys.argv[1],
12+
'-ar', '8000', '-ac', '1', '-f', 's16le', '-',
13+
stdout=asyncio.subprocess.PIPE)
14+
15+
while True:
16+
data = await proc.stdout.read(8000)
17+
18+
if len(data) == 0:
19+
break
20+
21+
await websocket.send(data)
22+
print (await websocket.recv())
23+
24+
await websocket.send('{"eof" : 1}')
25+
print (await websocket.recv())
26+
27+
await proc.wait()
28+
29+
asyncio.get_event_loop().run_until_complete(
30+
hello('ws://localhost:2700'))
31+
asyncio.get_event_loop().close()

0 commit comments

Comments
 (0)