Reputation: 93
I'm trying to record an RTP stream via python-vlc for exactly 30 seconds but the output file is sometimes less than or greater than my desired video length.
Note: I tried using ffmpeg, but it can't properly decode the stream hence the decision to use python-vlc.
This is my code:
import vlc
import time
vlcInstance = vlc.Instance("--demux=ts")
player1 = vlcInstance.media_player_new()
media1 = vlcInstance.media_new("rtp://239.194.115.71:5000")
media1.add_option("sout=file/ts:sample.mpg")
limiter = 0
player1.set_media(media1)
player1.play()
time.sleep(1)
while player1.is_playing():
if limiter > 30:
player1.stop()
media1.release()
break
limiter = limiter + 1
time.sleep(1)
What possible method can I use to always record the stream for exactly 30 seconds?
Upvotes: 2
Views: 3533
Reputation: 93
I used opencv-python to get the current frame count and fps of the output file and used it to compute for the video length.
import vlc
import time
import cv2
import os.path
vid_len = 0
vlcInstance = vlc.Instance("--demux=ts")
player1 = vlcInstance.media_player_new()
media1 = vlcInstance.media_new("rtp://239.194.115.71:5000")
media1.add_option("sout=file/ts:sample.mpg")
player1.set_media(media1)
player1.play()
#checks if the length of the output exceeds 30 seconds
while vid_len < 30:
time.sleep(1)
#checks if the file exists and not empty
if os.path.isfile('sample.mpg') and (os.path.getsize('sample.mpg') > 0):
video_file = cv2.VideoCapture('sample.mpg')
frames = int(video_file.get(cv2.CAP_PROP_FRAME_COUNT))
fps = (video_file.get(cv2.CAP_PROP_FPS))
vid_len = frames/fps
player1.stop()
media1.release()
Upvotes: 3