ffmpegrtsp

ffmpeg record rtsp video and audio but actual file time duration less than actual


I have a batch script to run ffmpeg command to write RTSP stream video + audio to .ts file. The rtsp stream is from an IP camera. Here are the batch/command:

set VID_SOURCE=rtsp://192.168.0.80:9000/live 
set VIDEO_OPTS=-f mpegts -b:v 800k -r 60 -vcodec libx264 -s 1280x960 -aspect 4:3 -bufsize 6000k
set AUDIO_OPTS=-af asetrate=48000 -acodec aac -b:a 96k -ac 1
ffmpeg -use_wallclock_as_timestamps 1 -rtsp_transport tcp -i %VID_SOURCE% %VIDEO_OPTS% %AUDIO_OPTS% -y %outputpath%\%OUTPUT_FILE%

This batch is invoked by a python script, and set to run 1 minute (timeout=60). Then kill the recording process, as below:

def recording_start(script_path, output_path, output_filename):
# call batch script to start recording
return subprocess.Popen(['cmd', '/c', os.path.join(script_path, 'batch_script.bat'), output_path, output_filename])
# if camera connected
if capture.isOpened():
    print('INFO: camera connected')
    proc = recording_start(script_path=REC_PATH, output_path=OUTPUT_PATH, output_filename=OUTPUT_FILENAME)

    # start recording for "timeout" seconds
    try:
        print('INFO: start recording')
        proc.communicate(timeout=60) # record 60 seconds

    # when time's up, will catch the "TimeoutExpired" exception and kill the recording process
    except subprocess.TimeoutExpired:
        print('{} seconds finished, stop the recording process: {}'.format(timeout, proc.pid))
        kill_recording(proc)

    print('INFO: recording complete')
    print('INFO: recording file saved at {}'.format(OUTPUT_PATH))
    break

# still not connected, go back to wait
else:
    print('Error opening video stream')

But actual recording file has only 48 seconds long, not 60 seconds. Anyone could help with where went wrong? Thanks


Solution

  • I am not an expert on ffmpeg but you set -r flag with 60 so it means 60FPS, maybe when your input source (RTSP) doesnt provide 60 FPS your duration of the output is not exaclty 60 seconds.