Skip to content

Instantly share code, notes, and snippets.

@libraplanet
Created February 24, 2016 19:56
Show Gist options
  • Save libraplanet/061d02c79f671e654ed1 to your computer and use it in GitHub Desktop.
Save libraplanet/061d02c79f671e654ed1 to your computer and use it in GitHub Desktop.
recording RTMP to local using ffmpeg.
#!/bin/sh
#------------------
# arguments
#------------------
BASE_DIR="$1"
REC_SEC="$2"
REC_TYPE="$3"
CHANNEL_TITLE="$4"
#------------------
# general settings
#------------------
LOCK_FILE_NAME=.lock
CNT_FILE_NAME=.cnt
RTMP_URL="rtmp://fms-base1.mitene.ad.jp/agqr/aandg22"
RADIO_PARAMS="-vn -acodec copy -f adts"
VIDEO_PARAMS="-vcodec copy -acodec copy -f mp4"
RADIO_EXT=.aac
VIDEO_EXT=.mp4
function lock() {
echo "lock start."
until `ln -s $$ "$1"`
do
echo wait.
sleep 5
done
trap unlock EXIT
echo "locking."
}
function unlock () {
if [ -L "$1" ]; then
rm -f "$1"
fi
echo "unlock()."
trap "" EXIT
}
function recording () {
local FF_INPUT="$1"
local FF_TIMEOUT_SEC="$2"
local FF_PARAMS="$3"
local FF_OUTPUT="$4"
ffmpeg -t ${FF_TIMEOUT_SEC} -y -re -i "${FF_INPUT}" \
${FF_PARAMS} "${FF_OUTPUT}"
}
function main () {
local cnt=0
local output_cnt=
local output_format="[%s] No.%03d_%s"
local output_date=`date +"%y%m%d"`
local output_fname=
cd "${BASE_DIR}"
echo start recording.
# increment onair no.
lock "${LOCK_FILE_NAME}"
if [ -f "${CNT_FILE_NAME}" ]; then
cnt=`cat "${CNT_FILE_NAME}"`
fi
output_cnt=`expr "${cnt}" + 1`
echo "${output_cnt}" > "${CNT_FILE_NAME}"
unlock "${LOCK_FILE_NAME}"
# create base file name.
output_fname=`printf "${output_format}" "${output_date}" "${output_cnt}" "${CHANNEL_TITLE}"`
# trace.
echo BASE_DIR=${BASE_DIR}
echo REC_SEC=${REC_SEC}
echo output_cnt=${output_cnt}
echo output_fname=${output_fname}
# execute recording.
case ${REC_TYPE} in
radio) echo radio; recording "${RTMP_URL}" "${REC_SEC}" "${RADIO_PARAMS}" "${output_fname}${RADIO_EXT}";;
video) echo video; recording "${RTMP_URL}" "${REC_SEC}" "${VIDEO_PARAMS}" "${output_fname}${VIDEO_EXT}";;
esac
echo stop recording.
}
main
usage: recorder.sh [BASE_DIR] [REC_SEC] [REC_TYPE] [CHANNEL_TITLE]
BASE_DIR working dir path.
REC_SEC recoding seconds.
REC_TYPE 'radio' or 'video'
radio demux audio. save to aac.
vodei convert container. save to mp4.
CHANNEL_TITLE channel title. use save file name.
this script create count file in BASE_DIR and recoding files.
count file in last onair no.
onair no is incremented by this script.
e.g.)
> recorder.sh "/var/www/html/hoge" 3600 radio "hoge channel"
> recorder.sh "/var/www/html/hoge" 3600 video "hoge channel"
> recorder.sh "/var/www/html/hoge" 3600 radio "hoge channel"
-> /var/www/html/hoge/[160102] No.001 hoge channel.aac
-> /var/www/html/hoge/[160102] No.002 hoge channel.mp4
-> /var/www/html/hoge/[160102] No.003 hoge channel.aac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment