Skip to content

Commit

Permalink
VLC ani-skip
Browse files Browse the repository at this point in the history
  • Loading branch information
Commandcracker committed May 22, 2024
1 parent 1a9116e commit bc65fd6
Show file tree
Hide file tree
Showing 13 changed files with 335 additions and 70 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
python-version: 3.x

- name: Install dependencies 🧰
env:
PIP_ROOT_USER_ACTION: ignore
run: |
python -m pip install --upgrade pip
pip install build
Expand All @@ -26,7 +28,7 @@ jobs:
run: python -m build

- name: Publish package 🚀
uses: Commandcracker/pypi-publish@32e78ea691b666534c641470e9d74e4deca05bcc
uses: Commandcracker/pypi-publish@8d13f542c4bb425036897e69001b670654c41a8d
with:
password: ${{ secrets.PYPI_API_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ Place your custom CSS in `user_config_path("gucken").joinpath("custom.css")` and
- [ ] FIX TYPING SOMETIMES CAUSES CRASH
- [ ] Syncplay on Android
- [ ] More CLI args
- [ ] reverse proxy
- [ ] Chapters for VLC

[Anime4k]: https://github.com/bloc97/Anime4K
[MPV]: https://mpv.io/
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ maintainers = [{name="Commandcracker"}]
license = {file = "LICENSE.txt"}
readme = "README.md"
dependencies = [
"textual>=0.60.1",
"textual>=0.62.0",
"beautifulsoup4>=4.12.3",
"httpx>=0.27.0",
"pypresence>=4.3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/gucken/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.4"
__version__ = "0.1.5"
98 changes: 72 additions & 26 deletions src/gucken/gucken.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import logging
from asyncio import gather
from atexit import register as register_atexit
from os import name as os_name
from os import remove
from os import remove, name as os_name
from os.path import join
from pathlib import Path
from random import choice
from shutil import which
Expand Down Expand Up @@ -56,7 +56,7 @@
from .provider.serienstream import SerienStreamProvider
from .settings import gucken_settings_manager
from .update import check
from .utils import detect_player, is_android
from .utils import detect_player, is_android, set_default_vlc_interface_cfg, get_vlc_intf_user_path


def sort_favorite_lang(
Expand Down Expand Up @@ -207,7 +207,7 @@ class GuckenApp(App):
TITLE = "Gucken TUI"
# TODO: color theme https://textual.textualize.io/guide/design/#designing-with-colors

CSS_PATH = ["gucken.css"]
CSS_PATH = [join("resources", "gucken.css")]
custom_css = user_config_path("gucken").joinpath("custom.css")
if custom_css.exists():
CSS_PATH.append(custom_css)
Expand Down Expand Up @@ -312,7 +312,7 @@ def compose(self) -> ComposeResult:
Select.BLANK if player == "AutomaticPlayer" else player
),
)
with Collapsible(title="ani-skip (only for MPV)", collapsed=False):
with Collapsible(title="ani-skip (only for MPV and VLC)", collapsed=False):
yield RadioButton(
"Skip opening",
id="ani_skip_opening",
Expand All @@ -324,7 +324,7 @@ def compose(self) -> ComposeResult:
value=settings["ani_skip"]["skip_ending"],
)
yield RadioButton(
"Get chapters",
"Get chapters (only MPV)",
id="ani_skip_chapters",
value=settings["ani_skip"]["chapters"],
)
Expand Down Expand Up @@ -460,7 +460,8 @@ async def disable_RPC(self):
self.RPC = None

# TODO: https://textual.textualize.io/guide/workers/#thread-workers
@work(exclusive=True)
# TODO: Exit on error when debug = true
@work(exclusive=True, exit_on_error=False)
async def lookup_anime(self, keyword: str) -> None:
search_providers = []
if self.query_one("#aniworld_to", Checkbox).value:
Expand Down Expand Up @@ -659,12 +660,21 @@ async def update():

self.app.call_later(update)

if self._debug:
logs_path = user_log_path("gucken", ensure_exists=True)
if isinstance(_player, MPVPlayer):
args.append("--log-file=" + str(logs_path.joinpath("mpv.log")))
elif isinstance(_player, VLCPlayer):
args.append("--file-logging")
args.append("--log-verbose=3")
args.append("--logfile=" + str(logs_path.joinpath("vlc.log")))

chapters_file = None

# TODO: cache more
# TODO: Support based on mpv
# TODO: recover start --start=00:56
if isinstance(_player, MPVPlayer):
if isinstance(_player, MPVPlayer) or isinstance(_player, VLCPlayer):
ani_skip_opening = self.query_one("#ani_skip_opening", RadioButton).value
ani_skip_ending = self.query_one("#ani_skip_ending", RadioButton).value
ani_skip_chapters = self.query_one("#ani_skip_chapters", RadioButton).value
Expand All @@ -674,29 +684,65 @@ async def update():
series_search_result.name, index + 1
)
if timings:
if ani_skip_chapters:
chapters_file = generate_chapters_file(timings)
if isinstance(_player, MPVPlayer):
if ani_skip_chapters:
chapters_file = generate_chapters_file(timings)

def delete_chapters_file():
try:
remove(chapters_file.name)
except FileNotFoundError:
pass

register_atexit(delete_chapters_file)

args.append(get_chapters_file_mpv_option(chapters_file.name))

if ani_skip_opening:
args.append(opening_timings_to_mpv_option(timings))

if ani_skip_ending:
args.append(ending_timings_to_mpv_option(timings))

args.append("--script=" + str(Path(__file__).parent.joinpath("resources", "mpv_skip.lua")))

if isinstance(_player, VLCPlayer):
# cant use --lua-config because it would override syncplay cfg
# cant use --extraintf and --lua-intf because it is already used by syncplay
"""
args = [
"vlc",
"--extraintf=luaintf",
"--lua-intf=skip",
"--lua-config=skip={" + f"op_start={op_start},op_end={op_end},ed_start={ed_start},ed_end={ed_end}" +"}",
url
]
"""
prepend_data = ["-- Generated"]

if ani_skip_opening:
prepend_data.append(set_default_vlc_interface_cfg("op_start", timings["op_start_time"]))
prepend_data.append(set_default_vlc_interface_cfg("op_end", timings["op_end_time"]))

if ani_skip_ending:
prepend_data.append(set_default_vlc_interface_cfg("ed_start", timings["ed_start_time"]))
prepend_data.append(set_default_vlc_interface_cfg("ed_end", timings["ed_end_time"]))

def delete_chapters_file():
try:
remove(chapters_file.name)
except FileNotFoundError:
pass
prepend_data.append("-- Generated\n")

register_atexit(delete_chapters_file)
vlc_intf_user_path = get_vlc_intf_user_path(_player.executable).vlc_intf_user_path
Path(vlc_intf_user_path).mkdir(mode=0o755, parents=True, exist_ok=True)

args.append(get_chapters_file_mpv_option(chapters_file.name))
vlc_skip_plugin = Path(__file__).parent.joinpath("resources", "vlc_skip.lua")
copyTo = join(vlc_intf_user_path, "vlc_skip.lua")

if ani_skip_opening:
args.append(opening_timings_to_mpv_option(timings))
with open(vlc_skip_plugin, 'r') as f:
original_content = f.read()

if ani_skip_ending:
args.append(ending_timings_to_mpv_option(timings))
with open(copyTo, 'w') as f:
f.write("\n".join(prepend_data) + original_content)

args.append("--script=" + str(Path(__file__).parent.joinpath("skip.lua")))
if self._debug:
logs_path = user_log_path("gucken", ensure_exists=True)
args.append("--log-file=" + str(logs_path.joinpath("mpv.log")))
args.append("--control=luaintf{intf=vlc_skip}")

if syncplay:
# TODO: make work with flatpak
Expand Down Expand Up @@ -746,7 +792,7 @@ def delete_chapters_file():

resume_time = None

# only if mpv
# only if mpv WIP
while not self.app._exit:
output = process.stderr.readline()
if process.poll() is not None:
Expand Down
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions src/gucken/resources/mpv_skip.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local mpv_utils = require("mp.utils")

-- Stop script if skip.lua is inside scripts folder
local scripts_dir = mp.find_config_file("scripts")
if mpv_utils.file_info(mpv_utils.join_path(scripts_dir, "skip.lua")) ~= nil then
mp.msg.info("Disabling, another skip.lua is already present in scripts dir")
return
end

local mpv_options = require("mp.options")

local options = {
op_start = 0,
op_end = 0,
ed_start = 0,
ed_end = 0,
}
mpv_options.read_options(options, "skip")

local skipped_op = false
local skipped_ed = false

local function check_skip()
local current_time = mp.get_property_number("time-pos")
if not current_time then
return
end

-- Opening
if not skipped_op and current_time >= options.op_start and current_time < options.op_end then
mp.set_property_number("time-pos", options.op_end)
skipped_op = true
end

-- Ending
if not skipped_ed and current_time >= options.ed_start and current_time < options.ed_end then
mp.set_property_number("time-pos", options.ed_end)
skipped_ed = true
end
end

mp.observe_property("time-pos", "number", check_skip)
Loading

0 comments on commit bc65fd6

Please sign in to comment.