Skip to content

Update recv_av.py #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions example/recv_av.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def main():
return 0

ndi_find = ndi.find_create_v2()

if ndi_find is None:
return 0

Expand All @@ -22,6 +21,9 @@ def main():
ndi.find_wait_for_sources(ndi_find, 1000)
sources = ndi.find_get_current_sources(ndi_find)

for s in sources:
print(f"Device Name:{s.ndi_name}")

recv_create_desc = ndi.RecvCreateV3()
recv_create_desc.color_format = ndi.RECV_COLOR_FORMAT_BGRX_BGRA

Expand All @@ -35,35 +37,53 @@ def main():
ndi.find_destroy(ndi_find)

fps = 30
output = av.open('output.mov', mode='w')
stream = output.add_stream('mpeg4', rate=fps)
stream.width = 1920
stream.height = 1080
stream.pix_fmt = 'yuv420p'
stream.bit_rate = 8e+6
stream.bit_rate_tolerance = 12e+6
stream.codec_context.time_base = Fraction(1, fps)
output = av.open('../output.mov', mode='w')

video_stream = output.add_stream('mpeg4', rate=fps)
video_stream.width = 1920
video_stream.height = 1080
video_stream.pix_fmt = 'yuv420p'
video_stream.bit_rate = 8e+6
video_stream.bit_rate_tolerance = 12e+6
video_stream.codec_context.time_base = Fraction(1, fps)

audio_stream = output.add_stream('aac', rate=48000)
audio_stream.channels = 2
audio_stream.format = 'fltp'
audio_stream.time_base = Fraction(1, 48000)

start = time.time()
while time.time() - start < 1.0 * 30:
t, v, _, _ = ndi.recv_capture_v2(ndi_recv, 5000)
t, v, a, _ = ndi.recv_capture_v2(ndi_recv, 5000)

if t == ndi.FRAME_TYPE_VIDEO:
print('Video data received (%dx%d).' % (v.xres, v.yres))
frame_time = time.time() - start
try:
frame = av.VideoFrame.from_ndarray(v.data, format='bgra')
frame.pts = int(
round(frame_time / stream.codec_context.time_base))
for packet in stream.encode(frame):
frame.pts = int(round(frame_time / video_stream.codec_context.time_base))
for packet in video_stream.encode(frame):
output.mux(packet)
except Exception as e:
print(e)
ndi.recv_free_video_v2(ndi_recv, v)

for packet in stream.encode():
output.mux(packet)
elif t == ndi.FRAME_TYPE_AUDIO:

av_audio_frame = av.AudioFrame.from_ndarray(a.data, format='fltp')
av_audio_frame.sample_rate = audio_stream.rate
av_audio_frame.time_base = Fraction(1, audio_stream.rate)
av_audio_frame.pts = int((time.time() - start) * audio_stream.rate)
for packet in audio_stream.encode(av_audio_frame):
output.mux(packet)


for packet in video_stream.encode():
output.mux(packet)

for packet in audio_stream.encode():
output.mux(packet)

output.close()

ndi.recv_destroy(ndi_recv)
Expand Down