Skip to content

Instantly share code, notes, and snippets.

@gabldotink
Last active May 4, 2024 04:04
Show Gist options
  • Save gabldotink/6f3090b5fe9797202d6e03c1c1727f5b to your computer and use it in GitHub Desktop.
Save gabldotink/6f3090b5fe9797202d6e03c1c1727f5b to your computer and use it in GitHub Desktop.
prints all best audio formats for a YouTube video (public domain)
#!/bin/sh
# SPDX-License-Identifier: CC0-1.0 OR 0BSD
readonly POSIXLY_CORRECT
export POSIXLY_CORRECT
# this script generates a yt-dlp audio formats list for YouTube videos with multiple languages
# you can use this list like so, for example:
# audio_formats_list="$(sh audio_formats_list.sh [url])"
# yt-dlp --use-extractors youtube --audio-multistreams --format 'bestvideo'+"$audio_formats_list" -- [url]
# caveats:
# • all language tracks are labeled as English in merged file (maybe different in other locales?)
# • only works for YouTube
# • slow (but bearable)
# • tested, but not very thoroughly
# • dependencies (relatively commonplace, but not ideal)
# • hacky
# • the default audio track likely won’t be the default
url="$1"
export url
# if the audio format has a hyphen, that means there are multiple languages
if yt-dlp --simulate --use-extractors youtube --print format_id --format bestaudio -- "$url" | \
grep --quiet --color=never --fixed-strings '-';then
pre_format="$(yt-dlp --simulate --use-extractors youtube --print format_id --format bestaudio -- "$url")"
format="${pre_format%-*}"
export format
pre_count="$(yt-dlp --simulate --use-extractors youtube --list-formats -- "$url"|grep --fixed-strings "$format"-|wc -l)"
count="$((pre_count-2))"
export count
pre_formats="$(for i in $(seq 1 "$count");do printf '%s+' "$format"-"$i";done)"
formats="${pre_formats%+}"
printf '%s\n' "$formats"
# there isn’t a hyphen in this format, so just print this one format
else
yt-dlp --simulate --use-extractors youtube --print format_id --format bestaudio -- "$url"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment