Skip to content

Commit

Permalink
Switch to ffmpeg (#5)
Browse files Browse the repository at this point in the history
* Use ffmpeg to ensure dropped connections don't cause problems
* Custom UA
  • Loading branch information
rmens authored Jun 24, 2024
1 parent c77892d commit edd5217
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
42 changes: 21 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@

# Audiologger ZuidWest FM

This repository contains a bash script designed to record audio streams on an hourly basis and log relevant metadata about the current broadcast. The script also ensures that audio old recordings are periodically cleaned up.
This repository contains a bash script designed to record audio streams hourly and log relevant metadata about the current broadcast. It also ensures the periodic cleanup of old recordings.

## Features

- **Continuous Recording**: Automatically records the audio stream from ZuidWest FM every hour.
- **Metadata Logging**: Fetches and logs the current program name from the broadcast data API, providing context for each recording.
- **Log File**: Maintains a detailed log file for tracking the script’s activities and any potential errors.
- **Automatic Cleanup**: Removes audio files older than 31 days to free up space.
- **Debug Mode**: Troubleshooting by providing additional output when enabled.
- **Continuous Recording**: Automatically captures the audio stream from ZuidWest FM every hour.
- **Metadata Logging**: Fetches and logs the current program name from the broadcast data API, adding context to each recording.
- **Detailed Log File**: Maintains a comprehensive log file for tracking the script’s activities and any potential errors.
- **Automatic Cleanup**: Deletes audio files older than 31 days to conserve storage space.
- **Debug Mode**: Provides additional output for troubleshooting when enabled.

## Prerequisites

The script requires the following tools to be installed:
- `jq` - Command-line JSON processor.
- `curl` - Command line tool and library for transferring data with URLs.
The script requires the following tools:
- `jq` - A command-line JSON processor.
- `curl` - A command-line tool for transferring data with URLs.
- `ffmpeg` - A command-line tool for recording, converting, and streaming audio and video.

This script is designed for use with websites based on the [Streekomroep WordPress Theme](https://github.com/oszuidwest/streekomroep-wp), utilizing the Broadcast Data API from the theme. If you're using a different API, set `PARSE_METADATA` to 0 and use a plaintext file with metadata, or implement your own parsing.
This script is intended for use with websites based on the [Streekomroep WordPress Theme](https://github.com/oszuidwest/streekomroep-wp), which utilizes the Broadcast Data API from the theme. If you are using a different API, set `PARSE_METADATA` to 0 and use a plaintext file for metadata, or implement your own parsing method.

## Installation

1. **Clone this repository:**
```
```bash
git clone https://github.com/oszuidwest/zwfm-audiologger
cd zwfm-audiologger
```
Expand All @@ -33,29 +33,29 @@ This script is designed for use with websites based on the [Streekomroep WordPre

## Configuration

Modify the script to specify the recording directory (`RECDIR`), log file path (`LOGFILE`), and other parameters:
- `STREAMURL`: URL of the audio stream.
- `RECDIR`: Directory where audio recordings are stored.
- `LOGFILE`: File path for logging script operations.
- `METADATA_URL`: API endpoint for fetching broadcast metadata.
- `KEEP`: Number of days to retain audio recordings.
Edit the script to specify the recording directory (`RECDIR`), log file path (`LOGFILE`), and other parameters:
- `STREAMURL`: The URL of the audio stream.
- `RECDIR`: The directory where audio recordings are stored.
- `LOGFILE`: The path to the log file for logging script operations.
- `METADATA_URL`: The API endpoint for fetching broadcast metadata.
- `KEEP`: The number of days to retain audio recordings.
- `PARSE_METADATA`: Enables or disables metadata parsing from the Streekomroep WordPress theme.

## Usage

Schedule the script to run every hour using cron:
1. Open your crontab:
```
```bash
crontab -e
```
2. Add the following line to run the script at the start of every hour:
```
```bash
0 * * * * /path/to/your/zwfm-audiologger/audiologger.sh
```

## Debugging

To enable debug mode, set `DEBUG=1` in the script. This will output debug information to the console and help identify any issues during execution.
To enable debug mode, set `DEBUG=1` in the script. This will output debug information to the console to help identify any issues during execution.

## Contributing

Expand Down
22 changes: 9 additions & 13 deletions audiologger.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TIMESTAMP=$(/bin/date +"%Y-%m-%d_%H")
# Number of days to keep the audio files
KEEP=31
# Debug mode flag (set to 1 to enable debug mode)
DEBUG=0
DEBUG=1
# Metadata parsing flag (set to 1 to enable metadata parsing, 0 for plain text)
PARSE_METADATA=1

Expand All @@ -29,11 +29,13 @@ if [ ! -f "$LOGFILE" ]; then
touch "$LOGFILE"
fi

# Check if jq is installed
if ! command -v jq &> /dev/null; then
log_message "jq is not installed"
exit 1
fi
# Check if required commands are installed (ffmpeg, curl, jq)
for cmd in ffmpeg curl jq; do
if ! command -v $cmd &> /dev/null; then
log_message "$cmd is not installed."
exit 1
fi
done

# Create recording directory if it does not exist
if [ ! -d "$RECDIR" ]; then
Expand All @@ -43,12 +45,6 @@ fi
# Remove old files based on the KEEP variable
find "$RECDIR" -type f -mtime "+$KEEP" -exec rm {} \; || log_message "Failed to remove old files in $RECDIR"

# Kill processes from the previous hour associated with the stream URL
PIDS=$(pgrep -f "$STREAMURL")
if [ -n "$PIDS" ]; then
kill -9 "$PIDS" || { log_message "Failed to kill processes: $PIDS"; exit 1; }
fi

# Fetch current program name
if [ "$PARSE_METADATA" -eq 1 ]; then
# Parse metadata using jq
Expand All @@ -70,4 +66,4 @@ fi
echo "$PROGRAM_NAME" > "${RECDIR}/${TIMESTAMP}.meta" || { log_message "Failed to write metadata file"; exit 1; }

# Record next hour's stream
curl -s -o "${RECDIR}/${TIMESTAMP}.mp3" -A "Audiologger ZuidWest FM (2024.05)" "$STREAMURL" &> /dev/null & disown || { log_message "Failed to start recording from $STREAMURL"; exit 1; }
ffmpeg -loglevel error -user_agent "ZuidWest Audiologger 3.0" -t 3600 -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 -i "$STREAMURL" -c copy -f mp3 -y "${RECDIR}/${TIMESTAMP}.mp3" & disown

0 comments on commit edd5217

Please sign in to comment.