Skip to content

Instantly share code, notes, and snippets.

@Envek
Created June 30, 2011 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Envek/1057514 to your computer and use it in GitHub Desktop.
Save Envek/1057514 to your computer and use it in GitHub Desktop.
Script for launching multicast videostreaming (with logging, restarting and notifications)
#!/bin/bash
#
# Script for managing videostreaming over LAN with logging and GUI notifications.
#
# Dependencies: tstools libnotify-bin
#
# 2012, Novikov «Envek» Andrey. Use it freely under the MIT license.
# Configuration
MULTICASTADDR=224.0.0.1 # Target IP for streaming (from multicast range)
MULTICASTPORT=1234 # Target port for streaming
MULTICASTIFACE=192.168.1.1 # Local interface IP from where stream is going to the world
LOGFILE=/path/to/logfile.log # A log file for writing what and when have been streamed
LOCKFILE=/path/to/lockfile.lock # Prevent simultaneous launches with a lock file
VIDEOPATH=~/Video # A path, where videofiles is stored
if [ -z "$DISPLAY" ]
then export DISPLAY=:0
fi
# Logs an entry to logfile.
# Syntax: log ( message, prefix )
function log () {
echo -e "${2}`date --rfc-3339=seconds` $1" >> $LOGFILE
}
# Starts broadcasting
function broadcast_start () {
if [ ! -e "$LOCKFILE" ]; then
log "Начинаем воспроизведение.\n===================================================" "\n\n"
touch $LOCKFILE
killall -9 tsplay 2> /dev/null
while true; do
log "Начинаем вещание файлов с начала\n----------------------------------------------------------" "\n\n"
for videofile in ${VIDEOPATH}/*; do
if [ ! -e "$LOCKFILE" ]; then break 2; fi
bname=$(basename "$videofile") # filename without path
filename="${bname%.*}" # filename without extension
log "Вещаем $videofile"
notify-send "Видеовещание" "Вещается $filename" -i /usr/share/pixmaps/totem.xpm
tsplay "$videofile" ${MULTICASTADDR}:${MULTICASTPORT} -i $MULTICASTIFACE > /dev/null
done
done
log "Конец воспроизведения" "\n\n"
notify-send "Видеовещание завершено" "В настоящий момент ничего не воспроизводится" -u low -i /usr/share/pixmaps/totem.xpm
else
notify-send "Видеовещание уже запущено" "В настоящий момент уже идёт вещание" -u low -i /usr/share/pixmaps/totem.xpm
fi
}
function broadcast_stop () {
if [ -f "$LOCKFILE" ]; then
log "Дана команда на останов вещания..." "\n"
rm -f $LOCKFILE
if [ "$1" = "forced" ]; then
log "Принудительный останов"
killall tsplay
fi
fi
}
case "$1" in
start)
broadcast_start &
;;
next)
killall tsplay
;;
stop)
broadcast_stop
notify-send "Видеовещание завершается" "Вещание будет остановлено после завершения вещания текущего ролика" -u low -i /usr/share/pixmaps/totem.xpm
;;
force-stop)
broadcast_stop forced
;;
restart)
broadcast_stop forced
sleep 1
broadcast_start &
;;
*)
echo "Usage: $0 {start|next|stop|force-stop|restart}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment