canada flag  linkedinfacebook   Call Us Today: (613) 494-5010

Capture video stream using FFmpeg with specific time

A small script to capture video stream (eg: from a security camera). The script takes in time in format of HH:MM:SS and created directories based on the current date. Based on the current variables, it outputs the files to /opt/camera/recordings/. It also needs a temp directory, called streams under /opt/camera/.

Usage example (to capture 11 minutes of the stream):
capture.sh 00:11:00

#!/bin/bash
STREAM="rtsp://172.16.0.25:554/play2.sdp"
FILE="cam"
TIME=$1 # in 00:00:00 format
OUT=$FILE-`date '+%m-%d-%y_%H_%M'`
CWD=/opt/camera
DATE=`date '+%m-%d-%y'`
if [ ! -d $CWD/streams]
then
mkdir -p $CWD/streams
fi
ffmpeg -i "$STREAM" -t $TIME -c copy -an $CWD/streams/$OUT.mkv
if [ ! -d $CWD/recordings/$DATE ]
then
mkdir -p $CWD/recordings/$DATE
fi
mv $CWD/streams/$OUT.mkv $CWD/recordings/$DATE/$OUT.mkv

If you need help with your IT projects, fee free to contact us for IT support. Our consultants will be more than happy to help.

Last updated Dec 2, 2018