Record Live Radio Shows using ffmpeg

2 minutes read

Over the last 3-6 months or so, I have listened to the radio more frequently as I multi-task or unwind, but I’ve listened since I was little. There’s something especially great about online-radio and being to listen-in from hundreds of miles away. It sounds washed but I’ve started listening to C-Span occasionally as of late. Maybe it’s the unfiltered perspectives or opinions seem realer when it’s a person saying them aloud? C-Span’s call-in show is broadcasted on TV, but it’s like 6am in the morning, and TV doesn’t allow multi-tasking anyways.

Here’s my config. It took a bit of tweaking over a week or two, but now it is at set-it-and-forget-it status.

cspan.service
[Unit]
Description=Script to record C-Span show daily

[Service]
ExecStart=/bin/bash -c 'TMPFILE="/tmp/cspan_$(date +%%Y-%%m-%%d).opus" && OUTFILE="path/to/dir/_$(date +%%Y-%%m-%%d).opus" && ffmpeg -i https://playerservices.streamtheworld.c
om/api/livestream-redirect/CSPANRADIO.mp3 -t 12600 -c:a libopus -b:a 64k "$TMPFILE" && mv "$TMPFILE" "$OUTFILE"'
  1. I use opus because it seems to be the best audio codec, while being open-source and solid even at smaller bitrates.
  2. Originally, I saved the file directly to the folder that I planned on storing the file, but ran into issues when playing the file… Things like fast-forwarding/rewinding in Audiobookshelf would break and restart the episode. I think it’s related to Audiobookshelf reading the file’s metadata before it finished being created or something? Now, the file is saved in /tmp/ initially and then moved to the proper folder
  3. Sometimes the show goes late and they take extra calls, so I record 3.5 hours status.
  4. Each recording is approximately 115M.
cspan.timer
[Unit]
Description=Record C-SPAN daily 
                                                                                                                 
[Timer]
OnCalendar=*-*-* 06:00:00
Persistent=true

[Install]
WantedBy=timers.target
  1. Run the app daily @ 6am (CDT)
  2. Persistent=true means that if the server got rebooted and turned on after 6am, the timer would still trigger
cspanCleanup (Service @ top, Timer @ bottom)

Since the show is covering the political topic-du-jour, the episodes become stale quickly. I delete episodes older than 5 days (and usually skip episodes older than 3 days old)

# Service File
[Unit]
Description=Create backups daily & delete backups older than 5 days

[Service]
Type=oneshot
ExecStart=/bin/bash -c '/usr/bin/fdfind -d1 CSPAN --changed-before 5d  /path/to/cSpan/ -x /home/username/.local/bin/trash-cli/trash {}'
--------------------------------------------------------------------------------
# Timer File
[Unit]
Description=Daily run of the service file @ 6pm

[Timer]
OnCalendar= *-*-* 18:00:00
Persistent=true

[Install]
WantedBy=timers.target