Skip to content

Instantly share code, notes, and snippets.

@iwconfig
Created June 14, 2024 18:28
Show Gist options
  • Save iwconfig/2bdecc42e7b9c55bde05d1222027f57e to your computer and use it in GitHub Desktop.
Save iwconfig/2bdecc42e7b9c55bde05d1222027f57e to your computer and use it in GitHub Desktop.
A more manageable yt-dlp/youtube-dl format filtering for svtplay.se. Defaults to the best quality, dash and hevc, and skips accessibilities.
#!/usr/bin/env bash
# A more manageable yt-dlp/youtube-dl format filtering for svtplay.se. It outputs a
# complete format filter string. Defaults to the best quality, dash and hevc, and
# avoids all audio accessibilities. It handles both 2-channel and 6-channel audio.
# Use --audio-multistream to merge both tracks.
# It takes zero up to three arguments, each of which is one of "best" or "worst".
# The first is for video, the second for audio and the third argument is for the
# third "generic" fallback if both the first and the second filters fail. By default
# the third argument gets the same argument as the first argument (video), if not
# specified. 6-channel audio could have its own argument as well but, for now at
# least, I don't think there is any other 6-channel audio qualities to choose from.
# Usage examples:
# $ yt-dlp --no-download -f $(bash svtplay-format-filters.sh) https://www.svtplay.se/video/8ZmYP5D/testbild/avsnitt-2
# [...]
# [info] jXYyr4A: Downloading 1 format(s): dash-hbbtv-avc-0+dash-full-6
# $ yt-dlp --no-download -f $(bash svtplay-format-filters.sh worst) --audio-multistream https://www.svtplay.se/video/8ZmYP5D/testbild/avsnitt-2
# [...]
# [info] jXYyr4A: Downloading 1 format(s): dash-4+dash-full-5+hls-cmaf-full-AC-3-450-6ch-Svenska
# $ yt-dlp --no-download -f $(bash svtplay-format-filters.sh worst best best) https://www.svtplay.se/video/8ZmYP5D/testbild/avsnitt-2
# [...]
# [info] jXYyr4A: Downloading 1 format(s): dash-4+dash-full-6
#
# The first example grabs the best video and best 2-channel audio, but not the
# 6-channel audio, because --audio-multistream was not specified.
#
# The second example grabs the worst video, the worst 2-channel audio, and the
# only 6-channel audio available.
#
# The third example grabs the worst video, the best 2-channel audio, and no
# 6-channel audio (--audio-multistream omitted). If both first and second
# filters had failed, it would've used the generic "best" filter (third argument).
#
# If you want, you can use $combined_filters_string and execute yt-dlp or youtube-dl
# in this script directly
video_best_worst="${1:-best}"
audio_best_worst="${2:-best}"
best_worst="${3:-$video_best_worst}"
join() {
local IFS="$1"
shift
echo "$*"
}
parenthesize() {
printf '(%s)' "$*"
}
video_default_filter=${video_best_worst}video
video_filters=(
'[protocol=http_dash_segments][format_id*=hevc]'
'[protocol=http_dash_segments]'
'[protocol=m3u8_native][format_id*=cmaf]'
'[protocol=m3u8_native]'
)
accessibility_audio_filter='[language!=?sv-x-tal][language!=?sv-x-tydligaretal]'
audio_2ch_default_filter=${audio_best_worst}audio${accessibility_audio_filter}'[format_id!*=lb]'
audio_2ch_filters=(
'[protocol=http_dash_segments][format_id*=hevc]'
'[protocol=http_dash_segments]'
'[protocol=m3u8_native][format_id*=cmaf][format_id*=2ch]'
'[protocol=m3u8_native][format_id*=cmaf]'
'[protocol=m3u8_native]'
)
audio_6ch_default_filter=$audio_2ch_default_filter'[format_id*=6ch]'
audio_6ch_filters=(
'[protocol=m3u8_native][format_id*=cmaf]'
'[protocol=m3u8_native]'
)
video_filter_string=$(
parenthesize $(
join / ${video_filters[@]/#/$video_default_filter} $video_default_filter
)
)
audio_2ch_filter_string=$(
parenthesize $(
join / ${audio_2ch_filters[@]/#/$audio_2ch_default_filter} $audio_2ch_default_filter
)
)
audio_6ch_filter_string=$(
parenthesize $(
join / ${audio_6ch_filters[@]/#/$audio_6ch_default_filter} $audio_6ch_default_filter
)
)
filters=(
$video_filter_string
$audio_2ch_filter_string
$audio_6ch_filter_string
)
printf -v combined_filters_string '(%s)/'${best_worst} $(
join / $(
join + ${filters[@]}
join + ${filters[@]:0:${#filters[@]}-1} # Fallback, skipping 6-channel audio
)
)
printf $combined_filters_string
# Or do whatever with $combined_filters_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment