@fra-san's comment reminded me of the while read
loop which you can use to process output line-by-line/procedurally (as opposed to functionally, with pipes), you can use the <
operator to pipe the output of a file to that loop with Bash Process Substitution of the youtube-dl command, and you can also send it to the background by adding an &
and the while loop still can read the output of it (it's just reading a file, which BPS made).
through testing I found that I can keep playing the video normally even after it finishes downloading and gets renamed, though this might be a feature of mpv.
#!/bin/bash
# ytdl-stream - use youtube-dl to stream videos i.e watch videos as they download
# usage: ytdl-stream [YOUTUBE_DL_OPTIONS] URL
# you can pipe into it a command to open you video player, e.g:
# echo mpv --mute=yes | ytdl-stream -f 'best[width<=1920,height<=1080]' --write-auto-sub [URL]
test ! -t 0 && player_cmd="$(cat /dev/stdin)" || player_cmd="mpv"
while IFS="" read -r line; do
filename=$(echo "$line" | sed -n 's/^\[download\] Destination: //p')
if [[ -z "$filename" ]]; then
filename=$(echo "$line" | sed -n 's/^\[download\] \(.*\) has already been downloade.*/\1/p')
[[ -z "$filename" ]] && continue || notify-send "file already downloaded, opening.."
else
notify-send "downloading.."
fi
withoutExtensions="${filename%.*}"
withoutExtensions="${withoutExtensions%.*}"
if [[ -e "$filename" ]]; then
sleep 0.5 && ($player_cmd "$filename")&
elif [[ -e "$filename".part ]]; then
sleep 2
if [[ -e "$filename".part ]]; then
notify-send "found .part after sleep again"
($player_cmd "$filename".part)&
else
sleep 0.5 && ($player_cmd "$withoutExtensions"*)&
fi
fi
done < <(youtube-dl "$@" 2> /dev/null)&
.part
file is replaced by a new one with a new inode (tested:toutube-dl
2020.03.08 on Arch Linux); if you open the.part
you end up playing a deleted file. Are you sure this is what you are looking for? 2) Piping the stdout of a foregroundyoutube-dl
won't prevent files from being downloaded, maybe you don't need to send it to the background. 3) Can something like this be of help?youtube-dl "$URL" | while read -r file; do if [[ "$file" =~ ^\[download\]\ Destination:.*\.mp4 ]]; then vlc "${file#\[download\] Destination: }.part"; fi; done
unlink
, which is the interface used byrm
). – fra-san Mar 10 '20 at 10:05