This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
85 lines (71 loc) · 2.44 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import websockets
import asyncio
import base64
import json
from configure import auth_key
import subprocess
import os
FRAMES_PER_BUFFER = 3200
CHANNELS = 1
RATE = 16000
# the AssemblyAI endpoint we're going to hit
URL = f"wss://api.assemblyai.com/v2/realtime/ws?sample_rate={RATE}"
async def send_receive(stream):
print(f"Connecting websocket to url ${URL}")
async with websockets.connect(
URL,
extra_headers=(("Authorization", auth_key),),
ping_interval=5,
ping_timeout=20
) as _ws:
await asyncio.sleep(0.1)
print("Receiving SessionBegins ...")
session_begins = await _ws.recv()
print(session_begins)
print("Sending messages ...")
async def send():
while True:
try:
data = stream.read(FRAMES_PER_BUFFER)
data = base64.b64encode(data).decode("utf-8")
json_data = json.dumps({"audio_data": str(data)})
await _ws.send(json_data)
except websockets.exceptions.ConnectionClosedError as e:
print(e)
print(e.code)
except Exception:
assert False, "Not a websocket 4008 error"
await asyncio.sleep(0.01)
return True
async def receive():
while True:
try:
result_str = await _ws.recv()
result = json.loads(result_str)
if result.get("message_type") == "FinalTranscript":
print(result["text"])
except websockets.exceptions.ConnectionClosedError as e:
print(e)
print(e.code)
except Exception:
assert False, "Not a websocket 4008 error"
send_result, receive_result = await asyncio.gather(send(), receive())
YT_URL = "https://www.youtube.com/watch?v=cumJ1t7cCx4"
cmd_yt_dlp = ["yt-dlp", YT_URL, "-f", "wav/bestaudio/best", "-o", "-"]
cmd_ffmpeg = [
"ffmpeg",
"-re", # realtime
"-i",
"pipe:",
"-ac",
str(CHANNELS),
"-ar",
str(RATE),
"-f",
"wav",
"pipe:",
]
p_in, p_out = os.pipe()
with subprocess.Popen(cmd_yt_dlp, stdout=p_out, stderr=subprocess.DEVNULL):
with subprocess.Popen(cmd_ffmpeg, stdin=p_in, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) as stream:
asyncio.run(send_receive(stream.stdout))