Skip to content
Closed
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ Ani-skip uses the external lua script function of mpv and as such – for now
<details>

* Can I change subtitle language or turn them off? - Subtitles are a separate english track handed to the player, so you can toggle them there (mpv: press `v`). Other languages are not offered.
* Can I use Spanish subtitles? - If the `opencode` CLI is installed and configured, ani-cli offers Spanish subtitles after you select episodes. It can translate for playback or prepare multiple episodes first. Translations are cached under `$XDG_CACHE_HOME/ani-cli/subtitles` (or `~/.cache/ani-cli/subtitles`) and reused on later plays. OpenCode must have a working provider/model configured.
* Can I watch dub? - Yes, use `--dub`.
* Can I change dub language? - No.
* Can I change media source? - No (unless you can scrape that source yourself).
Expand Down
82 changes: 73 additions & 9 deletions ani-cli
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

version_number="5.1.8"
version_number="5.1.9"

# UI

Expand Down Expand Up @@ -270,6 +270,46 @@ get_video_link() {
fi
}

# Translate an episode's default subtitle to Spanish and cache it per anime/episode.
translated_subtitle() {
_cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}/ani-cli/subtitles"
_translated="$_cache_dir/${anime_id}-${1}-es.vtt"
mkdir -p "$_cache_dir" || return 1
[ -s "$_translated" ] && { printf "%s" "$_translated"; return 0; }
[ -n "$sub_link" ] || return 1
_source="${TMPDIR:-/tmp}/ani-cli-subtitle-$$.vtt"
_result="${_translated}.tmp.$$"
if ! $curl_exe -fsSL -A "$agent" -e "$refr" --max-time 30 $cipher_flag "$sub_link" -o "$_source"; then
rm -f "$_source" "$_result"
printf "Could not download the source subtitle for episode %s.\n" "$1" >&2
return 1
fi
if ! opencode run "Translate every subtitle cue into Spanish. Preserve all WebVTT structure, timestamps, cue identifiers, and formatting. Return only a complete valid WebVTT file, beginning with WEBVTT. Do not add markdown fences or explanations." --file "$_source" >"$_result"; then
rm -f "$_source"
printf "OpenCode failed. Its output was saved to %s\n" "$_result" >&2
return 1
fi
rm -f "$_source"
# Remove accidental markdown fences, then reject incomplete or non-WebVTT output.
sed '1{/^```/d}; ${/^```/d}' "$_result" >"${_result}.clean" && mv "${_result}.clean" "$_result"
if ! head -n 1 "$_result" | grep -q '^WEBVTT'; then
printf "OpenCode did not return WebVTT for episode %s. Its response was saved to %s\n" "$1" "$_result" >&2
return 1
fi
mv "$_result" "$_translated" || { rm -f "$_result"; return 1; }
printf "%s" "$_translated"
}

prepare_translation() {
_prepare_ep="$1"
_cached="${XDG_CACHE_HOME:-$HOME/.cache}/ani-cli/subtitles/${anime_id}-${_prepare_ep}-es.vtt"
[ -s "$_cached" ] && { info "Spanish subtitles already prepared for episode $_prepare_ep"; return 0; }
info "Preparing Spanish subtitles for episode $_prepare_ep..."
hianime_m3u8 "$_prepare_ep" "$mode" || { info "Could not find episode $_prepare_ep sources"; return 1; }
_translated_file="$(translated_subtitle "$_prepare_ep")" || { info "Translation failed for episode $_prepare_ep"; return 1; }
info "Spanish subtitles ready: $_translated_file"
}

# provide countdown for the next episode release both sub and dub
time_until_next_ep() {
animeschedule="https://animeschedule.net"
Expand Down Expand Up @@ -360,35 +400,39 @@ android_mpv() {
_conf_dir="/storage/emulated/0/mpv"
if [ -w "${_conf_dir%/mpv}" ]; then
[ -d "$_conf_dir" ] || mkdir "$_conf_dir"
printf "referrer=%s\n%s\n%s\n" "$refr" "${sub_link:+sub-file=$sub_link}" "${audio_link:+audio-file=$audio_link}" >"$_conf_dir/mpv.config.mp4"
printf "referrer=%s\n%s\n%s\n" "$refr" "${play_sub_link:+sub-file=$play_sub_link}" "${audio_link:+audio-file=$audio_link}" >"$_conf_dir/mpv.config.mp4"
fi
nohup am start --user 0 -a android.intent.action.VIEW -d "$video_link" -n is.xyz.mpv/.MPVActivity -e "title" "${anime_title} Episode ${ep_no}" $player_extra_flags >/dev/null 2>&1 &
}

play_episode() {
[ "$log_episode" = 1 ] && [ "$player_function" != "debug" ] && [ "$player_function" != "download" ] && command -v logger >/dev/null && logger -t ani-cli "${anime_title} ${ep_no}"
[ -z "$video_link" ] && get_video_link
play_sub_link="$sub_link"
if [ "$spanish_subtitles" = 1 ]; then
play_sub_link="$(translated_subtitle "$ep_no")" || die "Could not translate subtitles for episode $ep_no"
fi
[ "$skip_intro" = 1 ] && skip_flag="$(ani-skip -i "$mal_id" -e "$ep_no")"
# wait for the previous player to close (in range selection)
wait
# shellcheck disable=SC2086
case "$player_function" in
debug) printf "All links:\n%s\nSelected link:\n%s\nAudio:\n%s\nSubtitles:\n%s\n" "$links" "$video_link" "$audio_link" "$sub_link" ;;
debug) printf "All links:\n%s\nSelected link:\n%s\nAudio:\n%s\nSubtitles:\n%s\n" "$links" "$video_link" "$audio_link" "$play_sub_link" ;;
android_mpv) android_mpv ;;
android_vlc) nohup am start --user 0 -a android.intent.action.VIEW -d "$video_link" -n org.videolan.vlc/org.videolan.vlc.gui.video.VideoPlayerActivity -e "title" "${anime_title} Episode ${ep_no}" $player_extra_flags >/dev/null 2>&1 & ;;
*flatpak*mpv*) flatpak run io.mpv.Mpv --referrer="$refr" ${audio_link:+--audio-file="$audio_link"} ${sub_link:+--sub-file="$sub_link"} $skip_flag $player_extra_flags --force-media-title="${anime_title} Episode ${ep_no}" "$video_link" >/dev/null 2>&1 & ;;
*flatpak*mpv*) flatpak run io.mpv.Mpv --referrer="$refr" ${audio_link:+--audio-file="$audio_link"} ${play_sub_link:+--sub-file="$play_sub_link"} $skip_flag $player_extra_flags --force-media-title="${anime_title} Episode ${ep_no}" "$video_link" >/dev/null 2>&1 & ;;
*mpv*)
if [ "$no_detach" = 0 ]; then
nohup $player_function --referrer="$refr" ${audio_link:+--audio-file="$audio_link"} ${sub_link:+--sub-file="$sub_link"} $skip_flag $player_extra_flags --force-media-title="${anime_title} Episode ${ep_no}" "$video_link" >/dev/null 2>&1 &
nohup $player_function --referrer="$refr" ${audio_link:+--audio-file="$audio_link"} ${play_sub_link:+--sub-file="$play_sub_link"} $skip_flag $player_extra_flags --force-media-title="${anime_title} Episode ${ep_no}" "$video_link" >/dev/null 2>&1 &
else
$player_function --referrer="$refr" ${audio_link:+--audio-file="$audio_link"} ${sub_link:+--sub-file="$sub_link"} $skip_flag $player_extra_flags --force-media-title="${anime_title} Episode ${ep_no}" "$video_link"
$player_function --referrer="$refr" ${audio_link:+--audio-file="$audio_link"} ${play_sub_link:+--sub-file="$play_sub_link"} $skip_flag $player_extra_flags --force-media-title="${anime_title} Episode ${ep_no}" "$video_link"
mpv_exitcode=$?
[ "$exit_after_play" = 1 ] && [ -z "$_range" ] && exit "$mpv_exitcode"
fi
;;
*iina*)
# mpv splits sub-files on ':', so the url has to be escaped for iina
_sub_iina="$(printf "%s" "$sub_link" | sed 's|:|\\:|g')"
_sub_iina="$(printf "%s" "$play_sub_link" | sed 's|:|\\:|g')"
_audio_iina="$(printf "%s" "$audio_link" | sed 's|:|\\:|g')"
if pgrep -f "IINA" >/dev/null 2>&1; then
# omit --keep-running when an IINA instance exists to prevent hanging
Expand All @@ -397,8 +441,8 @@ play_episode() {
nohup $player_function --mpv-referrer="$refr" ${_audio_iina:+--mpv-audio-files="$_audio_iina"} ${_sub_iina:+--mpv-sub-files="$_sub_iina"} $player_extra_flags --no-stdin --keep-running --mpv-force-media-title="${anime_title} Episode ${ep_no}" "$video_link" >/dev/null 2>&1 &
fi
;;
*vlc*) nohup $player_function --http-referrer="$refr" $player_extra_flags --play-and-exit --meta-title="${anime_title} Episode ${ep_no}" "$video_link" :input-slave="${audio_link:+$audio_link#}$sub_link" >/dev/null 2>&1 & ;;
*yncpla*) nohup $player_function $player_extra_flags "$video_link" -- $skip_flag --referrer="$refr" ${audio_link:+--audio-file="$audio_link"} ${sub_link:+--sub-file="$sub_link"} --force-media-title="${anime_title} Episode ${ep_no}" >/dev/null 2>&1 & ;;
*vlc*) nohup $player_function --http-referrer="$refr" $player_extra_flags --play-and-exit --meta-title="${anime_title} Episode ${ep_no}" "$video_link" :input-slave="${audio_link:+$audio_link#}$play_sub_link" >/dev/null 2>&1 & ;;
*yncpla*) nohup $player_function $player_extra_flags "$video_link" -- $skip_flag --referrer="$refr" ${audio_link:+--audio-file="$audio_link"} ${play_sub_link:+--sub-file="$play_sub_link"} --force-media-title="${anime_title} Episode ${ep_no}" >/dev/null 2>&1 & ;;
download) "$player_function" "$video_link" "${anime_title} Episode ${ep_no}" $player_extra_flags ;;
catt) nohup catt cast $player_extra_flags "$video_link" >/dev/null 2>&1 & ;;
iSH)
Expand Down Expand Up @@ -639,6 +683,26 @@ case "$source" in
;;
esac

# OpenCode is optional. When present, let the user choose Spanish subtitles or
# prepare a batch in advance; the translated files are reused from cache.
spanish_subtitles=0
if command -v opencode >/dev/null 2>&1 && [ "$player_function" != "download" ]; then
subtitle_action="$(printf '%s\n' 'Play with original subtitles' 'Play with Spanish subtitles' 'Prepare Spanish subtitles for episodes' | menu 'Subtitle option: ' '' "$menu_extra_flags")"
case "$subtitle_action" in
'Play with Spanish subtitles') spanish_subtitles=1 ;;
'Prepare Spanish subtitles for episodes')
_prepare_selection="$(printf '%s\n' "$ep_list" | menu 'Select episodes to prepare: ' "$menu_multi_flag" "$menu_extra_flags")"
[ -n "$_prepare_selection" ] || die "No episodes selected"
for _prepare_ep in $_prepare_selection; do
prepare_translation "$_prepare_ep" || exit 1
done
exit 0
;;
'Play with original subtitles') ;;
*) die "No subtitle option selected" ;;
esac
fi

# moves the cursor up one line and clears that line
tput cuu1 && tput el
# stores the position of cursor
Expand Down
2 changes: 2 additions & 0 deletions ani-cli.1
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Exit the player, and return the player exit code (useful for non interactive sce
ENVIRONMENT VARIABLES
.PP
ani-cli v5 uses environment variables to control unstable/untested and niche features in addition to everything that has an option. Command-line options take precedence over env vars. Note that these are all subject to change.
.PP
If the OpenCode CLI is installed and configured, ani-cli offers playback with Spanish subtitles and batch subtitle preparation after episode selection. Translated subtitles are cached under $XDG_CACHE_HOME/ani-cli/subtitles, or ~/.cache/ani-cli/subtitles when XDG_CACHE_HOME is unset.
.TP
\fBANI_CLI_BRANCH\fR
Controls the Branch upgdated to with the -U option. Default is master.
Expand Down