Files
fnx_web/svelte/src/filesystem/viewers/Video.svelte
T

212 lines
5.1 KiB
Svelte
Raw Normal View History

<script lang="ts">
2025-10-14 00:03:48 +02:00
import { onMount, tick } from "svelte";
import { video_position } from "lib/VideoPosition";
import { fs_path_url, FSNode } from "lib/FilesystemAPI.svelte";
2020-11-17 23:39:27 +01:00
2025-10-14 00:03:48 +02:00
let {
node,
open_sibling,
}: {
node: FSNode
open_sibling: (delta: number) => void
2025-10-13 16:05:50 +02:00
} = $props();
2023-05-19 17:17:05 +02:00
// Used to detect when the file path changes
let last_path = ""
2025-10-13 16:05:50 +02:00
let loaded = $state(false)
2023-05-19 17:17:05 +02:00
2025-10-13 16:05:50 +02:00
let player: HTMLVideoElement = $state()
let playing = $state(false)
2020-12-01 23:01:21 +01:00
let media_session = false
2025-10-13 16:05:50 +02:00
let loop = $state(false)
2020-12-01 23:01:21 +01:00
2023-05-19 17:17:05 +02:00
export const update = async () => {
2020-12-01 23:01:21 +01:00
if (media_session) {
navigator.mediaSession.metadata = new MediaMetadata({
2025-10-14 00:03:48 +02:00
title: node.name,
2026-06-10 23:53:03 +02:00
artist: "Nova",
2020-12-01 23:01:21 +01:00
album: "unknown",
});
2023-05-19 17:17:05 +02:00
console.debug("Updating media session")
}
2025-10-14 00:03:48 +02:00
loop = node.name.includes(".loop.")
2023-05-19 17:17:05 +02:00
// When the component receives a new ID the video track does not
// automatically start playing the new video. So we use this little hack to
// make sure that the video is unloaded and loaded when the ID changes
2025-10-14 00:03:48 +02:00
if (node.path != last_path) {
last_path = node.path
2023-05-19 17:17:05 +02:00
loaded = false
await tick()
loaded = true
2020-12-01 23:01:21 +01:00
}
}
2025-02-03 15:38:07 +01:00
export const toggle_playback = () => playing ? player.pause() : player.play()
export const toggle_mute = () => player.muted = !player.muted
2025-02-03 16:29:26 +01:00
export const toggle_fullscreen = () => {
if (document.fullscreenElement === null) {
player.requestFullscreen()
} else {
document.exitFullscreen()
}
}
2025-10-14 00:03:48 +02:00
export const seek = (delta: number) => {
2024-08-14 19:58:25 +02:00
// fastseek can be pretty imprecise, so we don't use it for small seeks
// below 5 seconds
if (player.fastSeek && delta > 5) {
player.fastSeek(player.currentTime + delta)
} else {
player.currentTime = player.currentTime + delta
}
}
2020-12-01 23:01:21 +01:00
onMount(() => {
if ('mediaSession' in navigator) {
media_session = true
navigator.mediaSession.setActionHandler('play', () => player.play());
navigator.mediaSession.setActionHandler('pause', () => player.pause());
navigator.mediaSession.setActionHandler('stop', () => player.pause());
2025-10-14 00:03:48 +02:00
navigator.mediaSession.setActionHandler('previoustrack', () => open_sibling(-1));
navigator.mediaSession.setActionHandler('nexttrack', () => open_sibling(1));
2020-12-01 23:01:21 +01:00
}
})
2023-05-19 17:17:05 +02:00
const video_keydown = (e: KeyboardEvent) => {
if (e.key === " ") {
// Prevent spacebar from pausing playback in Chromium. This conflicts
// with our own global key handler, causing the video to immediately
// pause again after unpausing.
e.stopPropagation()
}
2023-05-19 17:17:05 +02:00
}
2020-11-17 23:39:27 +01:00
</script>
<div class="container">
2023-11-15 14:04:48 +01:00
{#if
2025-10-14 00:03:48 +02:00
node.file_type === "video/x-matroska" ||
node.file_type === "video/quicktime" ||
node.file_type === "video/x-ms-asf"}
2023-11-15 14:04:48 +01:00
<div class="compatibility_warning">
This video file type is not compatible with every web
browser. If the video fails to play you can try downloading
the video and watching it locally.
</div>
{/if}
<div class="player_and_controls">
2023-05-19 17:17:05 +02:00
<div class="player">
{#if loaded}
2025-10-13 16:05:50 +02:00
<!-- svelte-ignore a11y_media_has_caption -->
2023-05-19 17:17:05 +02:00
<video
bind:this={player}
controls
playsinline
autoplay
loop={loop}
2023-11-15 14:04:48 +01:00
class="video"
2025-10-13 16:05:50 +02:00
onpause={() => playing = false}
onplay={() => playing = true}
onkeydown={video_keydown}
2025-10-14 00:03:48 +02:00
use:video_position={() => node.sha256_sum.substring(0, 8)}
2023-05-19 17:17:05 +02:00
>
2025-10-14 00:03:48 +02:00
<source src={fs_path_url(node.path)} type={node.file_type} />
2023-05-19 17:17:05 +02:00
</video>
{/if}
</div>
2023-11-15 14:04:48 +01:00
<div class="controls">
<div class="spacer"></div>
2025-10-14 00:03:48 +02:00
<button onclick={() => open_sibling(-1)}>
<i class="icon i_skip_previous"></i>
2023-11-15 14:04:48 +01:00
</button>
2025-10-13 16:05:50 +02:00
<button onclick={() => seek(-10)}>
<i class="icon i_replay_10"></i>
2023-11-15 14:04:48 +01:00
</button>
2025-10-13 16:05:50 +02:00
<button onclick={toggle_playback} class="button_highlight">
2023-11-15 14:04:48 +01:00
{#if playing}
<i class="icon i_pause"></i>
2023-11-15 14:04:48 +01:00
{:else}
<i class="icon i_play_arrow"></i>
2023-11-15 14:04:48 +01:00
{/if}
</button>
2025-10-13 16:05:50 +02:00
<button onclick={() => seek(10)}>
<i class="icon i_forward_10"></i>
2023-11-15 14:04:48 +01:00
</button>
2025-10-14 00:03:48 +02:00
<button onclick={() => open_sibling(1)}>
<i class="icon i_skip_next"></i>
2023-11-15 14:04:48 +01:00
</button>
<div style="width: 16px; height: 8px;"></div>
2025-10-13 16:05:50 +02:00
<button onclick={toggle_mute} class:button_red={player && player.muted}>
2023-11-15 14:04:48 +01:00
{#if player && player.muted}
<i class="icon i_volume_off"></i>
2023-11-15 14:04:48 +01:00
{:else}
<i class="icon i_volume_up"></i>
2023-11-15 14:04:48 +01:00
{/if}
</button>
2025-10-13 16:05:50 +02:00
<button onclick={toggle_fullscreen}>
<i class="icon i_fullscreen"></i>
2023-11-15 14:04:48 +01:00
</button>
<div class="spacer"></div>
</div>
2023-05-19 17:17:05 +02:00
</div>
2020-11-17 23:39:27 +01:00
</div>
<style>
.container {
2023-05-19 17:17:05 +02:00
display: flex;
flex-direction: column;
2020-11-17 23:39:27 +01:00
height: 100%;
width: 100%;
2023-05-19 17:17:05 +02:00
}
2023-11-15 14:04:48 +01:00
.player_and_controls {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
}
2023-05-19 17:17:05 +02:00
.player {
flex: 1 1 auto;
display: flex;
justify-content: center;
2020-11-17 23:39:27 +01:00
text-align: center;
overflow: hidden;
}
2023-05-19 17:17:05 +02:00
.controls {
flex: 0 0 auto;
display: flex;
flex-direction: row;
background-color: var(--shaded_background);
2024-11-19 15:31:51 +01:00
backdrop-filter: blur(4px);
2023-05-19 17:17:05 +02:00
padding: 0 2px 2px 2px;
align-items: center;
}
.controls > * {
flex: 0 0 auto;
}
.controls > .spacer {
flex: 1 1 auto;
}
.video {
2020-11-17 23:39:27 +01:00
position: relative;
display: block;
margin: auto;
max-width: 100%;
max-height: 100%;
2023-05-19 17:17:05 +02:00
}
@media(max-height: 500px) {
2023-11-15 14:04:48 +01:00
.player_and_controls {
2023-05-19 17:17:05 +02:00
flex-direction: row;
}
.controls {
flex-direction: column;
}
}
.compatibility_warning {
background-color: var(--shaded_background);
2024-11-19 15:31:51 +01:00
backdrop-filter: blur(4px);
2026-10-01 02:06:00 +02:00
border-bottom: 2px solid var(--blue_color);
2023-05-19 17:17:05 +02:00
padding: 4px;
2020-11-17 23:39:27 +01:00
}
</style>