Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions bin/omarchy-chromium-ytdlp-host
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ osd_progress() {
omarchy-osd -i 󰇚 -p "$1" -d 8000 >/dev/null 2>&1 || true
}

# yt-dlp downloads video and audio as separate streams, each running 0-100%, then
# merges them. Fold them into one bar: the streams before the last share the first
# 90%, and the last one, the audio and a fraction of the size, fills the rest.
overall_percent() { # overall_percent <part> <parts> <percent>
local part=$1 parts=$2 pct=$3
if ((parts < 2)); then
echo "$pct"
elif ((part < parts)); then
echo $((((part - 1) * 100 + pct) * 90 / ((parts - 1) * 100)))
else
echo $((90 + pct / 10))
fi
}

osd_close() {
omarchy-shell -q osd close >/dev/null 2>&1 || true
}
Expand All @@ -92,27 +106,49 @@ download_url() {
fi

osd_progress 0

# Stream the download: OMARCHY_PROG carries the percent (drives the OSD), and
# OMARCHY_FILE and OMARCHY_TITLE (printed only after a successful move) carry the
# path and the title. The title is JSON-encoded so metadata cannot forge a record,
# and the file is named after it: yt-dlp strips control characters from a filename
# with or without --restrict-filenames, so a record is still only ever one line.
local line pct intpct last="" er nowms lastms=0 title="" filepath="" resolved
local er=$EPOCHREALTIME
local lastms=$((${er%[.,]*} * 1000 + 10#${er##*[.,]} / 1000))

# Stream the download: OMARCHY_PARTS names the formats yt-dlp picked (616+251 is a
# video and an audio stream), OMARCHY_PROG carries each stream's status and percent
# (drives the OSD), and OMARCHY_FILE and OMARCHY_TITLE (printed only after a
# successful move) carry the path and the title. The title is JSON-encoded so
# metadata cannot forge a record, and the file is named after it: yt-dlp strips
# control characters from a filename with or without --restrict-filenames, so a
# record is still only ever one line.
local line status format pct intpct parts=1 part=0 current="" plus last=0 nowms title="" filepath="" resolved
while IFS= read -r line; do
case $line in
OMARCHY_PARTS*)
plus=${line//[^+]/}
parts=$((${#plus} + 1))
;;
OMARCHY_PROG*)
pct=${line#OMARCHY_PROG$'\t'}
IFS=$'\t' read -r _ status format pct <<<"$line"
if [[ $format != "$current" ]]; then
current=$format
((++part))
fi
intpct=${pct%%.*}
intpct=${intpct//[^0-9]/}
[[ -n $intpct && $intpct != "$last" ]] || continue # skip no-op repeats
[[ -n $intpct ]] || continue
# A chunked (HLS) stream's first report reads 100% before any chunk arrives.
# A real finish always comes as its own "finished" report.
[[ $status == downloading && $intpct == 100 ]] && continue
# Throttle to ~4 redraws/sec so fast downloads don't spawn a flurry of processes.
er=$EPOCHREALTIME
nowms=$((${er%[.,]*} * 1000 + 10#${er##*[.,]} / 1000))
((nowms - lastms >= 250)) || continue
last=$intpct
intpct=$(overall_percent "$part" "$parts" "$intpct")
# Never run the bar backwards. A stretch that doesn't move it still redraws
# every few seconds, before the OSD's 8s timeout hides a slow download.
if ((intpct > last)); then
last=$intpct
elif ((nowms - lastms < 4000)); then
continue
fi
lastms=$nowms
osd_progress "$intpct"
osd_progress "$last"
;;
OMARCHY_FILE*)
resolved=$(resolve_download_file "${line#OMARCHY_FILE$'\t'}") || continue
Expand All @@ -124,8 +160,9 @@ download_url() {
esac
done < <(PYTHONUNBUFFERED=1 yt-dlp --no-playlist --no-simulate \
--quiet --no-warnings --no-exec --no-exec-before-download --progress --newline \
--progress-template $'download:OMARCHY_PROG\t%(progress._percent_str)s' \
--progress-template $'download:OMARCHY_PROG\t%(progress.status)s\t%(info.format_id)s\t%(progress._percent_str)s' \
--paths "$DOWNLOAD_DIR" -o '%(title)s.%(ext)s' \
--print $'video:OMARCHY_PARTS\t%(format_id)s' \
--print $'after_move:OMARCHY_FILE\t%(filepath)s' \
--print $'after_move:OMARCHY_TITLE\t%(title)j' \
-- "$url" 2>&1)
Expand Down
46 changes: 45 additions & 1 deletion test/shell.d/chromium-ytdlp-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ cat >"$fake_root/bin/yt-dlp" <<'EOF'
printf '%s\n' "$*" >>"$YTDLP_ARGV_LOG"
for arg in "$@"; do
if [[ $arg == "--no-simulate" ]]; then
if [[ -n ${YTDLP_PROGRESS:-} ]]; then
# What YouTube serves: an HLS video whose first report is a bogus 100%,
# then a separate audio stream. Paced past the host's redraw throttle.
printf 'OMARCHY_PARTS\t616+251\n'
for record in 'downloading 616 100.0%' 'downloading 616 0.2%' 'downloading 616 50.0%' \
'downloading 616 40.0%' 'finished 616 100.0%' 'downloading 251 0.0%' \
'downloading 251 50.0%' 'finished 251 100.0%'; do
read -r status format pct <<<"$record"
printf 'OMARCHY_PROG\t%s\t%s\t%s\n' "$status" "$format" "$pct"
Comment thread
dhh marked this conversation as resolved.
Resolved
sleep 0.3
done
fi
printf 'OMARCHY_FILE\t%s\n' "$YTDLP_FAKE_FILE"
[[ -n ${YTDLP_SKIP_TITLE:-} ]] || printf 'OMARCHY_TITLE\t%s\n' '"My Great Clip"'
exit 0
Expand All @@ -182,7 +194,15 @@ cat >"$fake_root/bin/omarchy-notification-send" <<'EOF'
printf '%s\n' "$*" >>"$NOTIFY_ARGV_LOG"
EOF

for stub in omarchy-osd omarchy-shell ffmpeg; do
cat >"$fake_root/bin/omarchy-osd" <<'EOF'
#!/bin/bash
while (($#)); do
[[ $1 == -p ]] && printf '%s\n' "$2" >>"${OSD_LOG:-/dev/null}"
shift
done
EOF

for stub in omarchy-shell ffmpeg; do
printf '#!/bin/bash\nexit 0\n' >"$fake_root/bin/$stub"
done
chmod +x "$fake_root/bin/"*
Expand Down Expand Up @@ -237,3 +257,27 @@ YTDLP_ARGV_LOG="$ytdlp_argv" NOTIFY_ARGV_LOG="$notify_argv" YTDLP_FAKE_FILE="$fa
grep -qF -- "Download complete Real_Clip [id]" "$notify_argv" ||
fail "yt-dlp native host falls back to the filename when no title record arrives" "$(cat "$notify_argv")"
pass "yt-dlp native host falls back to the filename when no title record arrives"

# Video and audio download as separate streams, each 0-100%, and an HLS stream opens
# with a bogus 100%. The OSD shows them as one bar that only moves forward.
osd_log="$TMPDIR/osd-log"
YTDLP_ARGV_LOG="$ytdlp_argv" NOTIFY_ARGV_LOG="$notify_argv" YTDLP_FAKE_FILE="$fake_file" \
YTDLP_PROGRESS=1 OSD_LOG="$osd_log" OMARCHY_PATH="$fake_root" OMARCHY_YTDLP_DIR="$fake_dir" \
bash -c '
source "$1"
download_url "$2"
' bash "$ROOT/bin/omarchy-chromium-ytdlp-host" "https://example.test/watch" >/dev/null 2>&1

shown=$(paste -sd' ' "$osd_log")
[[ $shown == "0 45 90 95 100" ]] ||
fail "yt-dlp native host shows video and audio as one bar that only moves forward" "$shown"
pass "yt-dlp native host shows video and audio as one bar that only moves forward"

# The stub replays progress records whatever it is asked for, so assert the requests.
grep -qF -- $'video:OMARCHY_PARTS\t%(format_id)s' "$ytdlp_argv" ||
fail "yt-dlp native host asks which streams make up the download" "$(cat "$ytdlp_argv")"
pass "yt-dlp native host asks which streams make up the download"

grep -qF -- $'download:OMARCHY_PROG\t%(progress.status)s\t%(info.format_id)s\t%(progress._percent_str)s' "$ytdlp_argv" ||
fail "yt-dlp native host tags each progress report with its status and stream" "$(cat "$ytdlp_argv")"
pass "yt-dlp native host tags each progress report with its status and stream"
Loading