Record radio stations usig NixOS and systemd-timers

Following snippet in this article will help you record your favorite radio shows using ffmpeg and systemd-timers.

We schedule two different recordings, one at Sun *-*-* 21:59:00, meaning every Sunday at 10 PM and an other one at Mon..Fri 18:59:00 Europe/London, which means every weekday at 7 PM London time.

{ config, lib, pkgs, ... }: {

  systemd.timers = {
    "Record-SWR-Bigband" = {
      wantedBy = [ "timers.target" ];
      timerConfig = {
        OnCalendar = "Sun *-*-* 21:59:00";
        Unit = "Record-SWR-Bigband.service";
      };
    };
    "BBC-Classical-Mixtape" = {
      wantedBy = [ "timers.target" ];
      timerConfig = {
        OnCalendar = "Mon..Fri 18:59:00 Europe/London";
        Unit = "BBC-Classical-Mixtape.service";
      };
    };
  };
  
  systemd.services = {
    "Record-SWR-Bigband" = {
      script = ''
        set -eu
        ${lib.getExe pkgs.ffmpeg} -i https://liveradio.swr.de/sw282p3/swr4bw/ -t 7320 /home/picloud/media/radio-records/SWR-Bigband/SWR-Bigband-$(date +%F_%H).mp3
      '';
      serviceConfig = {
        Type = "simple";
        User= "picloud";
      };
    };
    "BBC-Classical-Mixtape" = {
      script = ''
        set -eu
        ${lib.getExe pkgs.ffmpeg} -i http://as-hls-ww-live.akamaized.net/pool_23461179/live/ww/bbc_radio_three/bbc_radio_three.isml/bbc_radio_three-audio=320000.norewind.m3u8 -t 1920 /home/picloud/media/radio-records/BBC-Classical-Mixtape/BBC-Classical-Mixtape-$(date +%F_%H).mp3
      '';
      serviceConfig = {
        Type = "simple";
        User= "picloud";
      };
    };
  };

  systemd.tmpfiles.rules = [
    "d /home/picloud/media/radio-records/SWR-Bigband 0755 picloud users -"
    "d /home/picloud/media/radio-records/BBC-Classical-Mixtape 0755 picloud users -"
  ];

}

The script automatically creates the target directories for the user picloud. The scheduled systemd unit will record the radio station stream with a specified duration (-t, duration in seconds). The target recording filename will have a variable in it for the corresponding date.

💬 Are you interested in our work or have some questions? Join us in our public Signal chat pi crew 👋
🪙 If you like our work or want to supprot us, you can donate MobileCoins to our address.

Leave a Reply

Your email address will not be published. Required fields are marked *