Display 4 Camera RTSP Streams on Ubuntu with Mplayer and Screen

  1. Type the followins “sudo” commands in a Terminal window
  2. Install Screen

    [code]sudo apt install screen[/code]

  3. Create Configuration file named cams
  4. [code]sudo nano /etc/init.d/cams[/code]

  5. Paste the following into nano editor:

    [code]#!/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 [/code]

  • 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:

    [code]sudo chmod 755 /etc/init.d/cams[/code]

  • Now we can start the cameras:

    [code]sudo /etc/init.d/cams start[/code]

  • Alternately, to stop the cameras:

    [code]sudo /etc/init.d/cams stop[/code]

  • To repair (not working):

    [code]sudo /etc/init.d/cams repair[/code]

4 thoughts on “Display 4 Camera RTSP Streams on Ubuntu with Mplayer and Screen”

  1. Hey this works great!
    Is there a way to control the volume on each stream once they are running. I would like to be able to mute and unmute each stream as needed?

    Reply
      • I should have looked a little harder before asking about volume.
        Alt + Tab(ing) through each instance of mplayer gets me to what ever stream I want to control and then hitting ‘m’ on the keyboard mutes and unmutes it. Perfect for my use case.
        Thanks again for the bash script!

        Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.