summaryrefslogtreecommitdiff
path: root/sh/mpvy
diff options
context:
space:
mode:
authorXiao Pan <gky44px1999@gmail.com>2024-02-23 03:08:52 -0800
committerXiao Pan <gky44px1999@gmail.com>2024-02-23 03:08:52 -0800
commitdd78faaf40862bd1552ec36c41374b527f747284 (patch)
treeb914f890c545db7168984f7bcc15cfc7c976c1d3 /sh/mpvy
parentc808bb6ee7f77f077308a1f06f63dbee6a928f6d (diff)
Reorganize shell scripts into sh dir, consider in the future fsh will have different kinds of files
Diffstat (limited to 'sh/mpvy')
-rwxr-xr-xsh/mpvy45
1 files changed, 45 insertions, 0 deletions
diff --git a/sh/mpvy b/sh/mpvy
new file mode 100755
index 0000000..eb935cc
--- /dev/null
+++ b/sh/mpvy
@@ -0,0 +1,45 @@
+#!/bin/sh
+# MPV Yank/Youtube videos
+# references:
+# https://www.rockyourcode.com/til-how-to-watch-youtube-videos-with-mpv-and-keyboard-shortcuts/
+
+fps=30
+height=$SCR_HEIGHT
+url="$(xsel -ob)"
+flag=s
+
+# option f and h may do nothing if redownload? since same filename exist. yt-dlp won't download same file even without --auto-file-renameing=false. how improve? pass argument to aria2c?
+while getopts Aabd:f:h:su: opt; do
+ case $opt in
+ # s: streaming, a: aria2c then mpv, A: aria2c
+ A|a|s) flag=$opt;;
+ b) format='bestvideo+bestaudio/best';;
+ d) download_dir="$OPTARG";;
+ f) fps="$OPTARG";;
+ h) height="$OPTARG";;
+ u) url="$OPTARG";;
+ \?) exit 1;;
+ esac
+done
+if [ -z "$format" ]; then
+ # xyzstudio GPU
+ #vformat="[height<=?$height][fps<=?$fps][vcodec!^=?vp9][vcodec!^=?av01]"
+ vformat="[height<=?$height][fps<=?$fps][vcodec!^=?av01]"
+ # usually get .mp4 video + .webm audio on youtube, yt-dlp needs to merge them to .mkv
+ # it used to cause problem, but I forget
+ format="bestvideo$vformat+bestaudio/best$vformat"
+fi
+
+case $flag in
+ # here if use --write-sub, mpv doesn't recognize subtitles?
+ # --embed-subs is a little bit better, but still worse then direct streaming
+ # --sponsorblock-remove will make audio/video goes out of sync, need --force-keyframes-at-cuts which need re-encode which is slow, more see comments at the bottom of https://github.com/yt-dlp/yt-dlp/issues/871
+ # another way is using mpv-sponsorblock-minimal-git for streaming and sponsorblock-mpv-local for local videos
+ A) yt-dlp -f "$format" --embed-subs -P "${download_dir:-"$XDG_DOWNLOAD_DIR/mpvy/"}" --sponsorblock-remove default --sponsorblock-mark default "$url";;
+ a) yt-dlp -f "$format" --embed-subs -P "${download_dir:-"$XDG_DOWNLOAD_DIR/mpvy/"}" --sponsorblock-remove default --sponsorblock-mark default "$url" --exec 'mpv --fs --speed=2';;
+ s) mpv --ytdl-format="$format" --ytdl-raw-options='write-sub=' --fs --speed=2 "$url";;
+esac
+# not sure if this is the best practice, but it seems working ;)
+status=$?
+[ $status -ne 0 ] && notify-send 'mpvy failed'
+exit $status