From 1f28fdd1e16e9b88e4da74261721678cc0c98a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Sat, 11 Feb 2023 01:40:15 -0500 Subject: [PATCH 01/11] userscripts/view_in_mpv: using yt-dlp to download video --- misc/userscripts/view_in_mpv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 4f371c6b5..6e79da01c 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -139,4 +139,4 @@ printjs() { echo "jseval -q -w main $(printjs)" >> "$QUTE_FIFO" msg info "Opening $QUTE_URL with mpv" -"${video_command[@]}" "$@" "$QUTE_URL" +yt-dlp -o - "$QUTE_URL" | "${video_command[@]}" "$@" - From eb56106633dc4861d933d24cb7d1ff20cf4606c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Sat, 11 Feb 2023 02:12:08 -0500 Subject: [PATCH 02/11] userscripts/view_in_mpv: enabling YT_DLP_FLAGS --- misc/userscripts/view_in_mpv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 6e79da01c..a6ce10fbe 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -139,4 +139,4 @@ printjs() { echo "jseval -q -w main $(printjs)" >> "$QUTE_FIFO" msg info "Opening $QUTE_URL with mpv" -yt-dlp -o - "$QUTE_URL" | "${video_command[@]}" "$@" - +yt-dlp "$YT_DLP_FLAGS" -o - "$QUTE_URL" | "${video_command[@]}" "$@" - From fc186db09edc936c9d164fd650792c27cadbc45c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Sat, 11 Feb 2023 02:24:45 -0500 Subject: [PATCH 03/11] userscripts/view_in_mpv: option for enabling yt-dlp download --- misc/userscripts/view_in_mpv | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index a6ce10fbe..6dfc542cc 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -139,4 +139,8 @@ printjs() { echo "jseval -q -w main $(printjs)" >> "$QUTE_FIFO" msg info "Opening $QUTE_URL with mpv" -yt-dlp "$YT_DLP_FLAGS" -o - "$QUTE_URL" | "${video_command[@]}" "$@" - +if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then + yt-dlp "$YT_DLP_FLAGS" -o - "$QUTE_URL" | "${video_command[@]}" "$@" - +else + "${video_command[@]}" "$@" "$QUTE_URL" +fi From a4f4337ed7b76f285062995cb99647b29f44c13f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Sat, 11 Feb 2023 04:02:57 -0500 Subject: [PATCH 04/11] userscripts/view_in_mpv: using --title for appropriate window title --- misc/userscripts/view_in_mpv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 6dfc542cc..91d730c32 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -140,7 +140,7 @@ echo "jseval -q -w main $(printjs)" >> "$QUTE_FIFO" msg info "Opening $QUTE_URL with mpv" if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then - yt-dlp "$YT_DLP_FLAGS" -o - "$QUTE_URL" | "${video_command[@]}" "$@" - + yt-dlp "$YT_DLP_FLAGS" -o - "$QUTE_URL" | "${video_command[@]}" --title="$(yt-dlp --get-title "$QUTE_URL") - mpv" "$@" - else "${video_command[@]}" "$@" "$QUTE_URL" fi From 909d850630466f974c30ea538a43056580617e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Sat, 11 Feb 2023 15:05:17 -0500 Subject: [PATCH 05/11] userscripts/view_in_mpv: use read to get YT_DLP_FLAGS --- misc/userscripts/view_in_mpv | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 91d730c32..5df97e6d0 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -48,9 +48,11 @@ msg() { } MPV_COMMAND=${MPV_COMMAND:-mpv} +YTDLP_COMMAND=${YTDLP_COMMAND:-yt-dlp} # Warning: spaces in single flags are not supported MPV_FLAGS=${MPV_FLAGS:- --force-window --quiet --keep-open=yes --ytdl} IFS=" " read -r -a video_command <<< "$MPV_COMMAND $MPV_FLAGS" +IFS=" " read -r -a yt_dlp_video_command <<< "$YTDLP_COMMAND $YT_DLP_FLAGS" js() { cat <> "$QUTE_FIFO" msg info "Opening $QUTE_URL with mpv" if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then - yt-dlp "$YT_DLP_FLAGS" -o - "$QUTE_URL" | "${video_command[@]}" --title="$(yt-dlp --get-title "$QUTE_URL") - mpv" "$@" - + "${yt_dlp_video_command[@]}" -o - "$QUTE_URL" | "${video_command[@]}" --title="$(yt-dlp --get-title "$QUTE_URL") - mpv" "$@" - else "${video_command[@]}" "$@" "$QUTE_URL" fi From 49842bf09dc167662e6ad8c3d723642fc06c6da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Thu, 15 May 2025 08:09:21 -0400 Subject: [PATCH 06/11] view_in_mpv: adding -f,--fast flags for enabling --- misc/userscripts/view_in_mpv | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 5df97e6d0..0e08e7f3d 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -21,6 +21,13 @@ # (comments and video suggestions), i.e. only the videos should disappear # when mpv is started. And that's precisely what the present script does. # +# Arguments: +# * `-f`: Fast download, spawn yt-dlp to download the video, instead of +# letting mpv do it. This allows for much faster video downloads but is a +# bit more fragile: a) if you seek past the end of the mpv cache it'll +# likely break b) if you have custom formats set for yt-dlp the download +# speed will regress again. +# # Thorsten Wißmann, 2015 (thorsten` on Libera Chat) # Any feedback is welcome! @@ -54,6 +61,28 @@ MPV_FLAGS=${MPV_FLAGS:- --force-window --quiet --keep-open=yes --ytdl} IFS=" " read -r -a video_command <<< "$MPV_COMMAND $MPV_FLAGS" IFS=" " read -r -a yt_dlp_video_command <<< "$YTDLP_COMMAND $YT_DLP_FLAGS" +program_name=$0 options='f' loptions='fast' +getopt_out=$(getopt --name $program_name --options $options --longoptions $loptions -- "$@") +if (( $? != 0 )); then exit 1; fi + +#sets the positionnal parameters with getopt's output +eval set -- "$getopt_out" + +while [[ $1 != "--" ]]; do + case "$1" in + "-f") + USE_YTDLP_FOR_DOWNLOAD=${USE_YTDLP_FOR_DOWNLOAD:-"yes"} + shift + ;; + *) + msg error "$0: Unknown arg '$1'" + exit 2 + ;; + esac +done +# shift away from the last optional parameter (--) +shift + js() { cat < Date: Thu, 15 May 2025 09:24:47 -0400 Subject: [PATCH 07/11] view_in_mpv: allow passing URL as argument (for hint-url call method) --- misc/userscripts/view_in_mpv | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 0e08e7f3d..9e443a05c 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -61,16 +61,23 @@ MPV_FLAGS=${MPV_FLAGS:- --force-window --quiet --keep-open=yes --ytdl} IFS=" " read -r -a video_command <<< "$MPV_COMMAND $MPV_FLAGS" IFS=" " read -r -a yt_dlp_video_command <<< "$YTDLP_COMMAND $YT_DLP_FLAGS" -program_name=$0 options='f' loptions='fast' +program_name=$0 options='fu:' loptions='fast,url' getopt_out=$(getopt --name $program_name --options $options --longoptions $loptions -- "$@") if (( $? != 0 )); then exit 1; fi #sets the positionnal parameters with getopt's output eval set -- "$getopt_out" +URL="$QUTE_URL" +NO_JS=false while [[ $1 != "--" ]]; do case "$1" in - "-f") + -u|--url) + URL=$2 + NO_JS=true + shift 2 + ;; + -f|--fast) USE_YTDLP_FOR_DOWNLOAD=${USE_YTDLP_FOR_DOWNLOAD:-"yes"} shift ;; @@ -167,11 +174,13 @@ EOF printjs() { js | sed 's,//.*$,,' | tr '\n' ' ' } -echo "jseval -q -w main $(printjs)" >> "$QUTE_FIFO" - -msg info "Opening $QUTE_URL with mpv" -if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then - "${yt_dlp_video_command[@]}" -o - "$QUTE_URL" | "${video_command[@]}" --title="$(yt-dlp --get-title "$QUTE_URL") - mpv" "$@" - -else - "${video_command[@]}" "$@" "$QUTE_URL" +if ! [[ $NO_JS == "true" ]]; then + echo "jseval -q -w main $(printjs)" >> "$QUTE_FIFO" +fi + +msg info "Opening $URL with mpv" +if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then + "${yt_dlp_video_command[@]}" -o - "$URL" | "${video_command[@]}" --title="$(yt-dlp --get-title "$URL") - mpv" "$@" - +else + "${video_command[@]}" "$@" "$URL" fi From ab4c4371662606a4eb699a9b41e499df2ae68bdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Thu, 15 May 2025 09:25:57 -0400 Subject: [PATCH 08/11] view_in_mpv: --downloader=http for fastest results and downloading audio separately --- misc/userscripts/view_in_mpv | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 9e443a05c..7b9268b74 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -57,9 +57,12 @@ msg() { MPV_COMMAND=${MPV_COMMAND:-mpv} YTDLP_COMMAND=${YTDLP_COMMAND:-yt-dlp} # Warning: spaces in single flags are not supported +YT_DLP_FLAGS=${YT_DLP_AUDIO_FLAGS:- --cookies-from-browser chromium:~/.local/share/qutebrowser -N 8 --downloader=http -f 'bestvideo*+bestaudio/best'} +YT_DLP_AUDIO_FLAGS=${YT_DLP_AUDIO_FLAGS:- --cookies-from-browser chromium:~/.local/share/qutebrowser --downloader=aria2c -f bestaudio} MPV_FLAGS=${MPV_FLAGS:- --force-window --quiet --keep-open=yes --ytdl} IFS=" " read -r -a video_command <<< "$MPV_COMMAND $MPV_FLAGS" IFS=" " read -r -a yt_dlp_video_command <<< "$YTDLP_COMMAND $YT_DLP_FLAGS" +IFS=" " read -r -a yt_dlp_audio_command <<< "$YTDLP_COMMAND $YT_DLP_AUDIO_FLAGS" program_name=$0 options='fu:' loptions='fast,url' getopt_out=$(getopt --name $program_name --options $options --longoptions $loptions -- "$@") @@ -180,7 +183,19 @@ fi msg info "Opening $URL with mpv" if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then - "${yt_dlp_video_command[@]}" -o - "$URL" | "${video_command[@]}" --title="$(yt-dlp --get-title "$URL") - mpv" "$@" - + mkdir -p $HOME/.cache/qutebrowser/view_in_mpv + tmpdir=$(mktemp -p $HOME/.cache/qutebrowser/view_in_mpv -d) + ( cd $tmpdir && "${yt_dlp_audio_command[@]}" -o - "$URL" >$tmpdir/ytdlp_audio ) & + audio_ytdlp_pid=$! + sleep 2 + + MPV_FLAGS="$MPV_FLAGS --audio-file=$tmpdir/ytdlp_audio" + IFS=" " read -r -a video_command <<< "$MPV_COMMAND $MPV_FLAGS" + + "${yt_dlp_video_command[@]}" -o - "$URL" | "${video_command[@]}" --title="$(yt-dlp --cookies-from-browser chromium:~/.local/share/qutebrowser --get-title "$URL") - mpv" "$@" - + + ps $audio_ytdlp_pid >/dev/null && kill $audio_ytdlp_pid + rm -rf $tmpdir else "${video_command[@]}" "$@" "$URL" fi From cabe44b9fecdc0fbd36254f7fad17ebdc06e5ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Thu, 15 May 2025 09:42:38 -0400 Subject: [PATCH 09/11] view_in_mpv: update arguments documentation --- misc/userscripts/view_in_mpv | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 7b9268b74..9b4e0d184 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -22,11 +22,15 @@ # when mpv is started. And that's precisely what the present script does. # # Arguments: -# * `-f`: Fast download, spawn yt-dlp to download the video, instead of +# -f +# Fast download, spawn yt-dlp to download the video, instead of # letting mpv do it. This allows for much faster video downloads but is a # bit more fragile: a) if you seek past the end of the mpv cache it'll # likely break b) if you have custom formats set for yt-dlp the download # speed will regress again. +# -u {url} +# Pass an URL as argument. This is meant to be used with the `{hint-url}` +# argument from qutebrowser keymap system. # # Thorsten Wißmann, 2015 (thorsten` on Libera Chat) # Any feedback is welcome! From 5a0f01c7d8e4feb975eb2a8864392b542db92a9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Thu, 15 May 2025 12:49:41 -0400 Subject: [PATCH 10/11] view_in_mpv: fix errors from shellcheck (ci) --- misc/userscripts/view_in_mpv | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index 9b4e0d184..e815d4ab8 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -69,8 +69,7 @@ IFS=" " read -r -a yt_dlp_video_command <<< "$YTDLP_COMMAND $YT_DLP_FLAGS" IFS=" " read -r -a yt_dlp_audio_command <<< "$YTDLP_COMMAND $YT_DLP_AUDIO_FLAGS" program_name=$0 options='fu:' loptions='fast,url' -getopt_out=$(getopt --name $program_name --options $options --longoptions $loptions -- "$@") -if (( $? != 0 )); then exit 1; fi +if ! getopt_out=$(getopt --name "$program_name" --options "$options" --longoptions "$loptions" -- "$@"); then exit 1; fi #sets the positionnal parameters with getopt's output eval set -- "$getopt_out" @@ -187,9 +186,9 @@ fi msg info "Opening $URL with mpv" if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then - mkdir -p $HOME/.cache/qutebrowser/view_in_mpv - tmpdir=$(mktemp -p $HOME/.cache/qutebrowser/view_in_mpv -d) - ( cd $tmpdir && "${yt_dlp_audio_command[@]}" -o - "$URL" >$tmpdir/ytdlp_audio ) & + mkdir -p "$HOME/.cache/qutebrowser/view_in_mpv" + tmpdir=$(mktemp -p "$HOME/.cache/qutebrowser/view_in_mpv" -d) + ( cd "$tmpdir" && "${yt_dlp_audio_command[@]}" -o - "$URL" >"$tmpdir/ytdlp_audio" ) & audio_ytdlp_pid=$! sleep 2 @@ -199,7 +198,7 @@ if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then "${yt_dlp_video_command[@]}" -o - "$URL" | "${video_command[@]}" --title="$(yt-dlp --cookies-from-browser chromium:~/.local/share/qutebrowser --get-title "$URL") - mpv" "$@" - ps $audio_ytdlp_pid >/dev/null && kill $audio_ytdlp_pid - rm -rf $tmpdir + rm -rf "$tmpdir" else "${video_command[@]}" "$@" "$URL" fi From e6abb8e1fd7337a59e958f8c07aedb998caa4570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20D=C3=A9saulniers?= Date: Thu, 18 Dec 2025 18:06:58 -0500 Subject: [PATCH 11/11] view_in_mpv: avoid creating --Frag files in ~/ These files created by yt-dlp are gonna spawn in the temp directory instead. --- misc/userscripts/view_in_mpv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/userscripts/view_in_mpv b/misc/userscripts/view_in_mpv index e815d4ab8..0a4516c72 100755 --- a/misc/userscripts/view_in_mpv +++ b/misc/userscripts/view_in_mpv @@ -195,7 +195,7 @@ if [[ $USE_YTDLP_FOR_DOWNLOAD == "yes" ]]; then MPV_FLAGS="$MPV_FLAGS --audio-file=$tmpdir/ytdlp_audio" IFS=" " read -r -a video_command <<< "$MPV_COMMAND $MPV_FLAGS" - "${yt_dlp_video_command[@]}" -o - "$URL" | "${video_command[@]}" --title="$(yt-dlp --cookies-from-browser chromium:~/.local/share/qutebrowser --get-title "$URL") - mpv" "$@" - + ( cd "$tmpdir" && "${yt_dlp_video_command[@]}" -o - "$URL" | "${video_command[@]}" --title="$(yt-dlp --cookies-from-browser chromium:~/.local/share/qutebrowser --get-title "$URL") - mpv" "$@" - ) ps $audio_ytdlp_pid >/dev/null && kill $audio_ytdlp_pid rm -rf "$tmpdir"