Radio2Podcast script
About script
Short destription
I created a Radio2Podcast (with help from GPT-41) to record radio shows and make them into podcasts. Some stations charge a subscription fee for access to archived broadcasts - this script is a clever workaround. The script also generates an RSS feed automatically, making it easy to listen later in a podcast app. Script can be used to archive radio broadcasts. You can choose a station, set the recording duration, and save the files locally, to an FTP server and NextCloud.
Script can be downloaded here: Radio2Podcast 0.051
Changelog
- 2024-10-20 relased version 0.051
- Added SFTP as a new save destination alongside local, FTP, and NextCloud.
- Recording now happens in .ts format using the stream’s original quality before converting it to the desired output format (like mp3 or opus).
- Added support for pauses during recording (e.g., to skip commercial breaks), controlled via command-line parameters. More info in usage section.
- 2024-08-30 relased version 0.05
- I changed name of scrpit from RSRS to Radio2Podcast.
- Changes in the logic of the script, from now on the names of broadcasts and radios can consist of multiple words. If the title of the broadcast or the name of the station contains special diacritical marks specific - the script retains them in the RSS but when creating paths to the file replaces them with Latin equivalents.
- Added to the config.json ability to choose the path to save file locally. Default is ./ (witch is location of script) but you can use different dir.
- 2024-07-18 relased version 0.04
- Lots of small fixes to make the script more convenient to use, changing the parameters that call the script, adding a function that keeps an eye on the number of files kept locally.
- 2024-05-01 released version 0.3
- I added basic podcast support. The script uploads the file to the FTP server to the appropriate folder and then checks the list of audio files in that folder and generates an RSS file each time. The RSS file is standards-compliant - although I have yet to discover why it cannot be added to PocketCasts. But in other podcast players everything is ok. The RSS file references the cover in the folder, the cover must be a file named podcast.jpg. In addition, a bit of a fix that relates to the correct management of file paths.
- 2024-04-03 released version 0.02
- The script has been completely rebuilt. You can choose the file format (mp3 or opus), bitrate, and channels (stereo or mono) for each destination. This allows you to save the file locally in .opus 32kbps stereo format, on FTP in mp3 32kbps mono format, and on Nextcloud in .opus 16kbps mono format. This feature will also be useful when the script supports podcasts. I added support for aac(m4a) to roadmap.
- 2024-03-29 released version 0.01
Roadmap
- multiple formats
Ability to save different file types depending on location (example: .mp3 to FTP sever and .opus to NextCloud)(done in version 0.02)- Ability to use opus, mp3, aac/m4a formats (work in progress, currently aac/m4a not supported)
- podcast support
If user decided to upload save files to FTP script should generate podcast feed.(done in version 0.03)Feed should be valid with podcast standards. (done in version 0.05)- Rewriting generating RSS part of code. Leave pod2gen library and use instead feedgen.
- Script should generate simple cover image.
- file save path
Adding to the config.json file the ability to choose the path to save files locally. Useful if you are running the script on a VPS on which a web server is set up at the same time.(done in version 0.05)Adding SFTP support(done in version 0.05)
Usage
Before first use
Dependencies
If you are using debian based distro you can install everything at once by running this command in your terminal:
sudo apt-get update && sudo apt-get install -y python3 python3-pip ffmpeg && pip3 install webdavclient3 pod2gen paramiko
This will ensure all the necessary tools and modules are installed and ready to use.
To run the script, you’ll need to install a few essential tools:
Details:
Config file
The script relies on a configuration file named config.json to manage various settings. This file is essential for specifying how and where the recorded audio files should be saved, as well as controlling the format and quality of those recordings.
If you want to send recorded files to FTP server or NextCloud fill details like hostname, username, password. You can choose codec (mp3 or opus), bitrate, channels (stereo or mono) for every destination. If you do not need a different file format and quality - set them all the same. This way, the script won’t need to convert the local file before uploading it to FTP or NextCloud, which will reduce CPU usage.
The parameters local_max_files, ftp_max_files, and nextcloud_max_files allow you to control the number of recordings kept in each location. When set to 0, there is no limit on the number of recordings, and all recorded files will be saved without restriction. When set to a positive number, only the most recent recordings up to the specified number will be kept, and older recordings will be automatically deleted to maintain the specified limit.
Stations
Edit the stations.json file to include the radio stream URLs you want to record. If you’re unsure how to find a stream URL, you can use tools like FMStream to locate it. If the station website provides a .pls file, open it in a text editor to find the URL inside. In cases where the stream URL is hidden, you may need to inspect the website’s source code to extract it.
First use
This is example usage:
python3 r2p.py "BBC Radio 1" "Night Show" 15 -s local sftp ftp nextcloud
This will record 15 minutes of BBC Radio 1 stream. Audio file will be saved localy in folder /recordings/BBC Radio 1/Night Show, next file will be also send to FTP, SFTP and NextCloud.
So this is all parameters:
- Station name (e.g., “BBC Radio 1”)
- Program name (for organization in folders and filename)
- Recording duration (in minutes)
- Choose where to save (use
-s
or--save
to specify where the recording should be saved. Options include local, ftp, and nextcloud. You can select one or more locations.)- The parameters -s or –save allow you to choose where the recording should be saved. For example:
- Use
-s local
to save the file only locally. - Use
-s ftp
to save the file only on FTP. - Use
-s local nextcloud
to save the file locall and on NextCloud. - Use
-s sftp
to save only to SFTP. - Use
-s local ftp sftp nextcloud
to save the file in all fours locations.
- Use
- The parameters -s or –save allow you to choose where the recording should be saved. For example:
- Pause and recording segments (use -p to set a sequence of recording and pause times in minutes):
- For example,
-p 10,2,8,1
means recording for 10 minutes, pausing for 2 minutes, recording for 8 minutes, pausing for 1 minute, and so on. This is useful for skipping parts like commercials.
- For example,
This provides flexibility for your recordings. So another example:
python3 r2p.py "BBC Radio 1" "Morning Show" 25 -s local nextcloud -p 10,3,15
In this case, the script will record the BBC Radio 1 stream for a total of 25 minutes, saving the audio locally and on NextCloud. The recording will start for 10 minutes, then pause for 3 minutes, and resume recording for another 15 minutes. This allows you to skip unwanted sections, such as advertisements, by controlling the recording and pause segments.
Schedule recording
On Linux you can use crontab (Cron Table). This is guide how to do it. Sample line of crontab should look like this:
0 19 * * 1 python3 r2p.py "BBC Radio 1" "Night Show" 15 -s local ftp nextcloud
This will execute the script every Monday at 19:00, starting a 15-minute recording of the BBC Radio 1 Night Show and saving it locally, as well as uploading it to FTP and NextCloud.
RSS Feed Generation
The script automatically generates an RSS feed whenever a file is uploaded to the FTP server, turning your recorded shows into a podcast. Here’s how it works:
- The RSS feed (feed.xml) lists all audio files in the specified FTP directory, including details like podcast name, description, and publication dates.
- The feed references a cover image (podcast.jpg), which should be manually uploaded to the same FTP directory. In the future, the script will be able to generate a thumbnail image automatically.
- Each time a new recording is uploaded, the RSS feed is updated to include the latest episode.
This feature simplifies distributing your recorded radio shows as a podcast, making it easy for listeners to subscribe and receive new episodes automatically.
-
Some parts are wroten by GPT-4. I’m not a programmer and AI helped me a lot, especially with the parts that using the FTP server. ↩︎