Display 4 Camera RTSP Streams on Ubuntu with Mplayer and Screen

  1. Type the followins “sudo” commands in a Terminal window
  2. Install Screen
    sudo apt install screen
  3. Create Configuration file named cams
  4. sudo nano /etc/init.d/cams
  5. Paste the following into nano editor:
    #!/bin/bash 
    ### BEGIN INIT INFO 
    # Provides: mplayer
    # Required-Start: 
    # Required-Stop: 
    # Default-Start: 2 3 4 5 
    # Default-Stop: 0 1 6 
    # Short-Description: Displays camera feeds for monitoring 
    # Description: 
    ### END INIT INFO
    
    # Output mplayer to remote device if using ssh remotely
    if [ -n "$SSH_TTY" ]; then
    export DISPLAY=:0.0
    fi
    
    # Camera Feeds & Positions
    top_left="screen -dmS top_left sh -c 'mplayer -noborder -x 640 -y 400 -geometry 0:0 rtsp://admin:admin@10.148.1.6:554/live1.264'";
    top_right="screen -dmS top_right sh -c 'mplayer -noborder -x 640 -y 400 -geometry 640:0 rtsp://admin:admin@10.148.1.2:554'";
    bottom_left="screen -dmS bottom_left sh -c 'mplayer -noborder -x 640 -y 400 -geometry 0:400 rtsp://admin:admin@10.148.1.4:554'";
    bottom_right="screen -dmS bottom_right sh -c 'mplayer -noborder -x 640 -y 400 -geometry 640:400 rtsp://admin:admin@10.148.1.5:554'";
    
    # Camera Feed Names 
    # (variable names from above, separated by a space) 
    camera_feeds=(top_left top_right bottom_left bottom_right)
    
    #---- There should be no need to edit anything below this line ----
    
    # Start displaying camera feeds 
    case "$1" in 
    start) 
    for i in "${camera_feeds[@]}" 
    do 
    eval eval '$'$i 
    done 
    echo "Camera Display Started" 
    ;;
    
    # Stop displaying camera feeds 
    stop) 
    sudo killall mplayer 
    echo "Camera Display Ended" 
    ;;
    
    # Restart any camera feeds that have died 
    repair) 
    for i in "${camera_feeds[@]}" 
    do 
    if !(sudo screen -list | grep -q $i) 
    then 
    eval eval '$'$i 
    echo "$i is now running" 
    fi 
    done 
    ;;
    
    *) 
    echo "Usage: /etc/init.d/cams {start|stop|repair}" 
    exit 1
    
    ;; 
    esac 
  • Save the file after adjusting the rtsp url/ip address to match your cameras
  • We need to let the system use the file so we need to change the permissions on the file we just created by issuing the following command: 
    sudo chmod 755 /etc/init.d/cams
  • Now we can start the cameras: 
    sudo /etc/init.d/cams start
  • Alternately, to stop the cameras: 
    sudo /etc/init.d/cams stop
  • To repair (not working): 
    sudo /etc/init.d/cams repair

Leave a Comment