I don't know if this helps. I use yad to provide a GUI for downloading instructional or copyright-free YouTube videos using yt-dlp. My script allows the option to choose both the video and audio formats from a yad list, and depending on the resolution and format of the video owner's original
file, different formats are available to download. This means that the yad lists can be of
different sizes for different videos.
I first issue the yt-dlp command with the switch -F to get a list of the video's available formats and store them in the array "formats". I split the output into the video and audio formats and present them separately so that I can choose exactly what I want to download.
I use arrays to present the format data to the yad command.
Here is the code snippet for creating a yad list command for the audio streams available from a video, which is not elegant but it works in bash:
************************************************************************************************************
# Get formats available
mapfile -t formats < <( (yt-dlp -F "$link") )
# Send all available formats to the log
echo "Formats available:" >> "$log";printf '%s\n' "${formats[@]}" >> "$log"
# Extract audio formats from the formats array
ifs=$IFS;IFS=$'\n'; aformats=($(printf '%s\n' "${formats[@]}" |sed '/audio/!d'|sort -k1 -n));IFS=$ifs
# Get number of indices in the array and create the command suffix (asuf) for yad. This adds the indices required
for i in ${!aformats[@]}; do asuf="$asuf""\$element$i $i "; done
# Remove trailing index number in command (else the yad list shows a blank entry at the end)
asuf="$(echo "$asuf"|rev|cut -d" " -f3-|rev)"
# Start to create command for the audio list. In "yad_command_1a" the "1a," 1 refers to the first part of the command, a stands for audio
yad_command_1a="yad --list --center --height="400" --width="800" --title=\"$format_title\" --window-icon="$yt_arrow" --radiolist --column=\"Select\" --column=\"Choose the audio format (normally 140, m4a):\" --button=\" Cancel\"\!\"$cancel_icon\":252 --button=\" OK\"\!\"$ok_icon\":0 TEXT "
# Add the suffix to the command
yad_command=${yad_command_1a}${asuf}
# Set variable (element{x}) for each element
ifs=$IFS;IFS=""
for i in "${!aformats[@]}"; do declare "element${i}"=$(printf '%s\n' "${aformats[i]}"); done
# Put the available formats into yad
while [[ -z "$aselected" ]]; do aselected=$(eval "$yad_command");rc=$?;[[ $rc -eq 252 ]] && { echo -e "Cancelled\n\n" >> "$log";exit 0; };done;aselected=$(echo $aselected|awk -F '[| ]' '{print $2}')
echo "Audio format selected = $aselected" >> "$log"
aformat="$aselected"
************************************************************************************************************
The formatting of the printed text is not perfect (which it is in the log file!), but it works.
I hope this helps. I am sure there are better ways of managing the arrays!