ubuntuでのFFmpeg Server起動スクリプト

Intel系とarm系では、FFmpeg起動の際のデバイス名指定方法が、以下の通り異なります。

Intel:

/usr/bin/ffmpeg -ac 2 -f alsa -ar 16000 -i default -loglevel quiet -acodec mp2 -ab 64k -ac 1 http://127.0.0.1:8090/feed1.ffm >/dev/null 2>/var/log/ffmpeg.log &

arm:

/usr/bin/ffmpeg -ac 1 -f alsa -ar 8000 -i “hw:0,0” -acodec mp2 -ab 32k -ac 1 http://127.0.0.1:8090/feed1.ffm >/dev/null 2>/var/log/ffmpeg.log &

起動スクリプトを以下のように記述しました。

/etc/init.d/FFserver_FFmpeg_start-daemon.sh [start|stop|status|restart]

cat /etc/init.d/FFserver_FFmpeg_start-daemon.sh
#!/bin/bash

#prgfile=<Program Script filepath>

# FFserver
prgfile="/usr/bin/ffserver -f ./ffserver.conf > /dev/null 2>&1"
prgfile="/usr/bin/ffserver -loglevel quiet -f ./ffserver.conf > /dev/null 2>&1"
curdir="/home/USER/Dropbox/shell/x201s/ffserver/"
prgtitle=ffserver
#pidfile=<PID filepath>
pidfile=/tmp/$prgtitle.pid

# FFmpeg
prgfile2="/usr/bin/ffmpeg -ac 2 -f alsa -ar 16000 -i default -loglevel quiet -acodec mp2 -ab 64k -ac 1 http://127.0.0.1:8090/feed1.ffm "
prgtitle2=ffmpeg
pidfile2=/tmp/$prgtitle2.pid



start() {
    if [ -f $pidfile ]; then
        pid=`cat $pidfile`
        kill -0 $pid >& /dev/null
        if [ $? -eq 0 ]; then
            echo "Daemon has started."
            return 1
        fi
    fi

    cd $curdir
    # Starting FFserver
    echo "Starting $prgfile"
    $prgfile &
    #pgrep -fl ffserver | awk '{print $1}' > $pidfile
    pgrep -fl $prgtitle | awk '{print $1}' > $pidfile

    # Starting FFmpeg
    sleep 1
    echo "Starting $prgfile2"
    $prgfile2 > /tmp/ffmpeg.log &
    #pgrep -fl ffserver | awk '{print $1}' > $pidfile2
    #pgrep -fl ffmpeg | awk '{print $1}' > $pidfile2
    pgrep -fl $prgtitle2 | awk '{print $1}' > $pidfile2

    if [ $? -eq 0 ]; then
        echo "Daemon started."
	pid=`cat $pidfile`
	pid2=`cat $pidfile2`
	echo "Daemon $prgtitle is started. (PID: ${pid})"
	echo "Daemon $prgtitle2 is started. (PID: ${pid2})"
        return 0
    else
        echo "Failed to start daemon."
        return 1
    fi
}

stop() {
    if [ ! -f $pidfile ]; then
        echo "Daemon not started."
        return 1
    fi

    pid=`cat $pidfile`
    pid2=`cat $pidfile2`
    kill $pid >& /dev/null
    if [ $? -ne 0 ]; then
        echo "Operation not permitted."
        return 1
    fi

    echo -n "Stopping daemon..."
    while true
    do
        kill -0 $pid >& /dev/null
        kill -0 $pid2 >& /dev/null
        if [ $? -ne 0 ]; then
            break
        fi

        sleep 2
        echo -n "."
    done

    rm -rf $pidfile
    rm -rf $pidfile2

    echo -e "\nDaemon stopped."
    return 0
}

status() {

    # FFserver
    if [ -f $pidfile ]; then
        pid=`cat $pidfile`
        pid2=`cat $pidfile2`
        kill -0 $pid >& /dev/null
        if [ $? -eq 0 ]; then
            echo "Daemon $prgtitle is running. (PID: ${pid})"
            echo "Daemon $prgtitle2 is running. (PID: ${pid2})"
            return 0
        else
            echo "Daemon might crash. (PID: ${pid} file remains)"
            return 1
        fi
    else
        echo "Daemon not started."
        return 0
    fi
}

restart() {
    stop
    if [ $? -ne 0 ]; then
        return 1
    fi

    sleep 2

    start
    return $?
}

case "$1" in
    start | stop | status | restart)
        $1
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 2
esac

#exit $
exit 0