Play Quranic Ayat and Surahs from the Command Line
Category: Shell Scripting
Date: 28 hours ago
Views: 110
Play Quran Ayat with a Shell Script
If you're someone who enjoys listening to Quranic recitations and prefers an automated way to play specific ayat or surahs, this script might be your new favorite tool. It uses files downloaded from EveryAyah and organizes them for seamless playback.
Script Overview: play-ayat
The play-ayat
script lets you play Quranic ayat by specifying their numbers and optional ranges. Here's how it works:
•Arguments:
- first_number
: The surah number (e.g., 001 for Al-Fatiha).
- second_number
: The starting ayah number (e.g., 001).
- [end_range]
: (Optional) The ending ayah number for a range of ayat to play.
- [speed]
: (Optional) Playback speed (defaults to 1.15x
).
How It Works
•Single Ayah Playback: When only the first_number
and second_number
are provided, the script constructs the filename using a padded three-digit format (e.g., 001001.mp3
for Al-Fatiha, Ayah 1). It checks if the file exists and plays it using mpv
with custom player settings.
./play-ayat 1 1
•Range of Ayat: If the end_range
is specified, the script builds a playlist (ayatplaylist.m3u
) containing all ayat in the specified range. This playlist is then fed into mpv
for playback.
./play-ayat 1 1 7
•Custom Playback Speed: You can optionally pass a speed argument to modify the playback speed. For example, to slow it down:
./play-ayat 1 1 7 0.9
The play-ayat
script
#!/bin/bash
# Check for at least two arguments
if [ "$#" -lt 2 ]; then
echo "Usage: $0 <first_number> <second_number> [<end_range>]"
exit 1
fi
# Extract arguments
first_number=$1
second_number=$2
end_range=$3
if [[ -n $4 ]]
then speed=$4
else speed=1.15
fi
# Variables
FILES_DIR="$HOME/Music/Abdul Basit Mujawwad"
#FILES_DIR="$HOME/Music/Minshawy Mujawwad"
#FILES_DIR="$HOME/Music/Minshawy Murattal"
PLAYLIST_FILE="/tmp/ayatplaylist.m3u"
PLAYER_CONFIG="--no-terminal --no-config --no-pause --no-loop-file --speed=$speed"
# Format numbers to three digits
pfirst_number=$(printf "%03d" "$first_number")
psecond_number=$(printf "%03d" "$second_number")
# Check if end_range is provided
if [ -z "$end_range" ]; then
# Single file case
filename="${pfirst_number}${psecond_number}.mp3"
full_path="${FILES_DIR}/${filename}"
if [ -f "$full_path" ]; then
mpv $PLAYER_CONFIG "$full_path" > /dev/null 2>&1
fi
else
# Range of files case
# Clear or create playlist file
echo "#EXTM3U" > "$PLAYLIST_FILE"
for ((i=second_number; i<=end_range; i++)); do
current_number=$(printf "%03d" "$i")
filename="${pfirst_number}${current_number}.mp3"
full_path="${FILES_DIR}/${filename}"
if [ -f "$full_path" ]; then
echo "$full_path" >> "$PLAYLIST_FILE"
fi
done
if [ -s "$PLAYLIST_FILE" ]; then
mpv $PLAYER_CONFIG --playlist-start=0 "$PLAYLIST_FILE" > /dev/null 2>&1
fi
fi
Extending the Script: play-surah
The play-surah
script takes this functionality further by automating the playback of an entire surah. Instead of specifying individual ayat, you can simply pass the surah number as the mandatory argument, and it will play all available ayat for that surah.
•Arguments:
- surah_number
: The mandatory surah number (e.g., 001 for Al-Fatiha).
- [start_ayah]
: (Optional) The starting ayah number.
- [end_ayah]
: (Optional) The ending ayah number.
•Logic: If no start_ayah
and end_ayah
are provided, the script loops from Ayah 1 to a reasonable maximum (e.g., 300). Files are checked for existence, and a playlist is dynamically generated.
Code Example for play-surah
#!/bin/bash
# Check arguments
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <surah_number> [<start_ayah> <end_ayah>]"
exit 1
fi
surah_number=$1
start_ayah=${2:-1}
end_ayah=${3:-300}
FILES_DIR="$HOME/Music/Abdul Basit Mujawwad"
PLAYLIST_FILE="/tmp/surahplaylist.m3u"
PLAYER_CONFIG="--no-terminal --no-config --no-pause --no-loop-file"
# Format surah number
psurah_number=$(printf "%03d" "$surah_number")
# Create playlist
echo "#EXTM3U" > "$PLAYLIST_FILE"
for ((i=start_ayah; i<=end_ayah; i++)); do
payah_number=$(printf "%03d" "$i")
filename="${psurah_number}${payah_number}.mp3"
full_path="${FILES_DIR}/${filename}"
if [ -f "$full_path" ]; then
echo "$full_path" >> "$PLAYLIST_FILE"
fi
done
if [ -s "$PLAYLIST_FILE" ]; then
mpv $PLAYER_CONFIG --playlist-start=0 "$PLAYLIST_FILE" > /dev/null 2>&1
else
echo "No files found for Surah $surah_number in the specified range."
fi
Usage Example for play-surah
•Play all ayat of a surah:
./play-surah 1
•Play a specific range of ayat within a surah:
./play-surah 2 255 286
Conclusion
These scripts bring simplicity and flexibility to Quranic recitation playback. Whether you're studying, meditating, or simply enjoying the beauty of the recitation, play-ayat
and play-surah
automate the process, saving you from manually selecting files.
With a little creativity, you could further extend these scripts for more advanced features like randomizing ayat, creating thematic playlists, or even integrating them into a larger application. which I did, stay tuned for more
0 Comments, latest