RSTP Writing Video and Audio to MP4 and Doing Frame over Frame Processing #1733
Unanswered
UltimateCodeWarrior
asked this question in
1. Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi All,
Was able to write some python code to lock on a RSTP feed and write Video and Audio to a MP4 file from a streaming security camera (AMCREST). Is there a way while writing to MP4 to also grab frame over frame of video data and enque it to be processed with motion detection? When I started experimenting with this, it seemed to corrupt the video .mp4 stream to where there were a lot of distortions in the playback video. Any suggestions/improvements? Thx!
`import av
import time
Open the RTSP feed
try:
source = av.open('rtsp://admin:admin@192.168.0.109:554/cam/realmonitor?channel=1&subtype=0', metadata_encoding='utf-8')
except OSError as e:
print(f"Error opening RTSP feed: {e}")
exit(1)
Create the output file
try:
output = av.open('output-%s.mp4' % time.time(), mode='w')
except OSError as e:
print(f"Error creating output file: {e}")
source.close()
exit(1)
Map input streams to output streams
in_to_out = {}
for stream in source.streams:
if stream.type in ('audio', 'video'):
try:
in_to_out[stream] = output.add_stream_from_template(stream)
except OSError as e:
print(f"Error adding stream: {e}")
source.close()
output.close()
exit(1)
Process packets and save 10 seconds of data
start_time = time.time()
try:
for packet in source.demux():
if packet.stream in in_to_out:
packet.stream = in_to_out[packet.stream]
output.mux(packet)
except KeyboardInterrupt:
pass
except OSError as e:
print(f"An error occurred during demuxing/muxing: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
try:
output.close()
except Exception as e:
print(f"Error closing output file: {e}")
try:
source.close()
except Exception as e:
print(f"Error closing source file: {e}")
`
Beta Was this translation helpful? Give feedback.
All reactions