From edd5217ea660fd41e53844ac12515d2e5f2dc3a0 Mon Sep 17 00:00:00 2001 From: Raymon Mens Date: Mon, 24 Jun 2024 23:59:09 +0200 Subject: [PATCH] Switch to ffmpeg (#5) * Use ffmpeg to ensure dropped connections don't cause problems * Custom UA --- README.md | 42 +++++++++++++++++++++--------------------- audiologger.sh | 22 +++++++++------------- 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 0dfd5e7..9bcf2e5 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -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 diff --git a/audiologger.sh b/audiologger.sh index 37d5293..6124405 100755 --- a/audiologger.sh +++ b/audiologger.sh @@ -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 @@ -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 @@ -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 @@ -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 \ No newline at end of file