Skip to content

Commit

Permalink
Merge abe4f39 into 6206ff0
Browse files Browse the repository at this point in the history
  • Loading branch information
paraenggu committed Dec 1, 2019
2 parents 6206ff0 + abe4f39 commit 941d0e8
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 25 deletions.
65 changes: 57 additions & 8 deletions bin/raar-record-handler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# raar-record-handler.sh - Moves new recordings to the raar import directory
################################################################################
#
# Copyright (C) 2018 Radio Bern RaBe
# Switzerland
# http://www.rabe.ch
# Copyright (C) 2018 - 2019 Radio Bern RaBe
# Switzerland
# https://rabe.ch
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public
Expand All @@ -32,6 +32,8 @@
# watch directory, containing recording files. The script assumes, that a
# recording has finished on such an event and moves the file to the given
# destination directory.
# Before moving the recording file to its final location, the script determines
# the duration of the recording and adds it to the final file name.
# If the archival was successful, the script sends the last successful
# recording timestamp to a Zabbix monitoring system with the help of the
# zabbix_sender tool.
Expand All @@ -42,6 +44,20 @@
# raar-record-handler.sh <WATCH-DIRECTORY> <DESTINATION-DIRECTORY>
#

# Check if all required external commands are available
for cmd in ffprobe \
inotifywait \
mv \
printf \
zabbix_sender
do
command -v "${cmd}" >/dev/null 2>&1 || {
echo >&2 "Missing command '${cmd}'"
exit 1
}

done

watchDir="$1"
destDir="$2"

Expand Down Expand Up @@ -74,7 +90,7 @@ minFileSize="$(( 20 * 1048576 ))" # 20 MiB
inotifywait --monitor --event close_write "${watchDir}" | while read \
watchedFileName eventNames eventFileName
do
echo "${eventNames} occoured on ${eventFileName}"
echo "${eventNames} occurred on ${eventFileName}"

sourcePath="${watchDir}/${eventFileName}"

Expand All @@ -86,8 +102,41 @@ do

echo "Archiving ${eventFileName}"

tmpPath="${destDir}/.${eventFileName}.tmp"
destPath="${destDir}/${eventFileName}"
# Get the duration of the recording in seconds, with microsecond accuracy
# such as "3599.998979"
ffprobeOutput="$( LC_ALL=C ffprobe -i "${sourcePath}" \
-show_entries format="duration" \
-print_format csv="print_section=0" \
-v quiet )"

if [ $? -eq 0 ]; then
# Round to integer value in seconds
# 3599.998979 => 3600
duration=$( LC_ALL=C printf '%.0f' "$ffprobeOutput" )

echo "Duration of recording is ${duration} seconds (${ffprobeOutput})"
else
echo "Unable to determine duration of recording" >&2
duration="0"
fi


# Get the file name without the extension, such as "2019-11-30T170000+0100"
fileName="${eventFileName%.*}"

# Get the file extension, such as flac, opus or mp3
fileExtension="${eventFileName##*.}"

# Add the duration to the original file name in the ISO 8601 duration
# format.
# For example, the file name "2019-11-30T170000+0100.flac" will be renamed
# to "2019-11-30T170000+0100_PT3600S.flac"
finalFileName="${fileName}_PT${duration}S.${fileExtension}"

echo "Setting final recording file name to: ${finalFileName}"

tmpPath="${destDir}/.${finalFileName}.tmp"
destPath="${destDir}/${finalFileName}"

# Move the file to a temporary location in a first step. This prevents the
# archive from importing an unfinished file, in case the source and
Expand All @@ -105,9 +154,9 @@ do
continue
fi

echo "${eventFileName} successfully archived"
echo "${eventFileName} successfully archived to ${destPath}"

# Inform the monitoring system about the last successfull recording
# Inform the monitoring system about the last successful recording
zabbix_sender --config /etc/zabbix/zabbix_agentd.conf \
--key 'rabe.raar.recording.success[]' \
--value "$(date +%s)" > /dev/null
Expand Down
2 changes: 1 addition & 1 deletion config/systemd/rotter@raar.service.d/override.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[Service]
# Rotter startup options
Environment="ROTTER_OPTIONS=-a -f flac -c 2 -R 10 -L '%%FT%%H%%M%%S%%z_060.flac' -j"
Environment="ROTTER_OPTIONS=-a -f flac -c 2 -R 10 -L '%%FT%%H%%M%%S%%z.flac' -j"
69 changes: 53 additions & 16 deletions doc/recording.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ and some systemd service units, which plays nicely together with RAAR.
[Rotter](https://www.aelius.com/njh/rotter/) is a Recording of Transmission /
Audio Logger for [JACK](http://www.jackaudio.org/). It captures audio from
`jackd`, encodes it in a specified format and creates a new recording file
every hour by default. This makes it ideal to use together with RAAR.
every hour by default. This makes it ideal for using it together with RAAR.

In the following deployment walk-through `rotter` will be configured to capture
the audio from the first two JACK input ports found, while storing the
recordings into the `/var/lib/rotter/raar` directory in the lossless
[FLAC](https://xiph.org/flac/) format. A separate recording-handler will move
the finished recordings from the `/var/lib/rotter/raar` directory to a final
RAAR import directory. Of course, the JACK configuration, audio codec and
directory locations can be customized to meet the requirements of your
environment.
[FLAC](https://xiph.org/flac/) format.

A separate recording-handler will move the finished recordings from the
`/var/lib/rotter/raar` directory to a final RAAR import directory. It also
determines the recording duration with the help of
[`ffprobe`](http://www.ffmpeg.org/ffprobe.html) and adds it to the recording
file name in the [ISO 8601 duration
format](https://en.wikipedia.org/wiki/ISO_8601#Durations) in seconds.

A final recording file will be named according to `YYYY-MM-DDThhmmss±hhmm_PTsS.flac` (such as `2019-11-30T170000+0100_PT3600S.flac`).

Of course, the JACK configuration, audio codec and directory locations can be
customized to meet the requirements of your environment.

Also note, that the deployment was tested on [CentOS
7](#deployment-on-centos-7-systems), but should work on [other
Expand All @@ -36,19 +44,24 @@ modifications).
### Deployment on CentOS 7 systems
There are pre-built binary packages for CentOS 7 available from [Fedora
EPEL](https://fedoraproject.org/wiki/EPEL) (jack) and [RaBe
APEL](https://build.opensuse.org/project/show/home:radiorabe:audio) (rotter),
which can be installed as follows:
APEL](https://build.opensuse.org/project/show/home:radiorabe:audio) (rotter)
and [Nux Dextop](http://li.nux.ro/repos.html) (ffprobe from ffmpeg), which can
be installed as follows:
```bash
# Add Fedora EPEL repository
yum install epel-release

# Add RaBe APEL repository
curl -o /etc/yum.repos.d/home:radiorabe:audio.repo \
http://download.opensuse.org/repositories/home:/radiorabe:/audio/CentOS_7/home:radiorabe:audio.repo

# Add Nux Dextop repository
rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm

# Install rotter (which will also install "jack-audio-connection-kit" and
# "jack-audio-connection-kit-example-clients")
yum install rotter
# "jack-audio-connection-kit-example-clients") as well as the ffprobe command
# from the ffmpeg package.
yum install rotter ffmpeg
```

Install the [raar jackd systemd service instance
Expand Down Expand Up @@ -236,18 +249,33 @@ ls -la /var/lib/rotter/raar
total 717844
drwxr-xr-x. 2 rotter rotter 4096 Feb 6 16:59 .
drwxr-xr-x. 3 rotter rotter 17 Feb 6 15:45 ..
-rw-r--r--. 1 rotter rotter 120309610 Feb 6 16:00 2018-02-06T154512+0100_60.flac
-rw-r--r--. 1 rotter rotter 230000737 Feb 6 16:28 2018-02-06T160000+0100_60.flac
-rw-r--r--. 1 rotter rotter 250600686 Feb 6 16:59 2018-02-06T162851+0100_60.flac
-rw-r--r--. 1 rotter rotter 127717296 Feb 6 17:16 2018-02-06T170000+0100_60.flac
-rw-r--r--. 1 rotter rotter 120309610 Feb 6 16:00 2018-02-06T154512+0100.flac
-rw-r--r--. 1 rotter rotter 230000737 Feb 6 16:28 2018-02-06T160000+0100.flac
-rw-r--r--. 1 rotter rotter 250600686 Feb 6 16:59 2018-02-06T162851+0100.flac
-rw-r--r--. 1 rotter rotter 127717296 Feb 6 17:16 2018-02-06T170000+0100.flac
```

#### RAAR Record Handler
The [RAAR Record Handler](bin/raar-record-handler.sh) will move the finished
recordings from the `/var/lib/rotter/raar` directory to a final RAAR import
directory (`IMPORT_DIRECTORIES`) for the [RAAR Importer](import.md) to pick
them up.
directory (`IMPORT_DIRECTORIES`) waiting for the [RAAR Importer](import.md) to
pick them up.

The record handler also determines the recording duration with the help of
[`ffprobe`](http://www.ffmpeg.org/ffprobe.html) and adds it to the recording
file name in the [ISO 8601 duration
format](https://en.wikipedia.org/wiki/ISO_8601#Durations) in seconds.

For example, a one hour (3600 seconds) rotter recording file
`/var/lib/rotter/raar/2019-11-30T170000+0100.flac` will be renamed and moved to
`/var/tmp/raar/import/2019-11-30T170000+0100_PT3600S.flac`

##### RAAR Record Handler installation
Install `ffmpeg` (required for
[`ffprobe`](https://www.ffmpeg.org/ffprobe.html)) from the packages provided by
your distribution or [compile it from
source](https://www.ffmpeg.org/download.html).

Install the [RAAR Record Handler](bin/raar-record-handler.sh) and its
corresponding systemd service unit
Expand Down Expand Up @@ -360,10 +388,19 @@ RAAR Record Handler:
journalctl -u raar-record-handler.service -f
```

* Duration of recording file in seconds
```bash
ffprobe -i RECORDING-FILENAME \
-show_entries format="duration" \
-print_format csv="print_section=0" \
-v quiet
```

## Links
* [Jack Audio Connection Kit](http://www.jackaudio.org/)
* [Rotter](https://www.aelius.com/njh/rotter/)
* [CentOS 7 RPM Specfile for
Rotter](https://github.com/radiorabe/centos-rpm-rotter)
* [Jack and Rotter Systemd service unit templates
explained](https://github.com/radiorabe/centos-rpm-rotter#systemd-service-unit-templates-explained)
* [ffprobe](https://www.ffmpeg.org/ffprobe.html)

0 comments on commit 941d0e8

Please sign in to comment.