-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
307 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { MediaProviderAdapter } from "@vidstack/react"; | ||
import { isVideoProvider } from "@vidstack/react"; | ||
import { Platform } from "obsidian"; | ||
import { WebiviewMediaProvider } from "@/lib/remote-player/provider"; | ||
import { captureScreenshot } from "@/lib/screenshot"; | ||
|
||
export function canProviderScreenshot(provider: MediaProviderAdapter | null) { | ||
return isVideoProvider(provider) || provider instanceof WebiviewMediaProvider; | ||
} | ||
|
||
export async function takeScreenshot(provider: MediaProviderAdapter) { | ||
const mimeType = Platform.isSafari ? "image/jpeg" : "image/webp"; | ||
if (isVideoProvider(provider)) { | ||
return await captureScreenshot(provider.video, mimeType); | ||
} else if (provider instanceof WebiviewMediaProvider) { | ||
return await provider.media.methods.screenshot(mimeType); | ||
} else { | ||
throw new Error("Unsupported provider for screenshot"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import mime from "mime"; | ||
import { Notice } from "obsidian"; | ||
import { | ||
canProviderScreenshot, | ||
takeScreenshot, | ||
} from "@/components/player/screenshot"; | ||
import { formatDuration, toDurationISOString } from "@/lib/hash/format"; | ||
import type { PlayerComponent } from "@/media-view/base"; | ||
import type { MediaInfo } from "../note-index"; | ||
import { | ||
createTimestampGen, | ||
insertTimestamp, | ||
mediaTitle, | ||
openOrCreateMediaNote, | ||
} from "./utils"; | ||
|
||
declare module "obsidian" { | ||
interface Vault { | ||
getAvailablePathForAttachments( | ||
fileName: string, | ||
extension: string, | ||
activeFile: TFile | null, | ||
): Promise<string>; | ||
} | ||
} | ||
|
||
export async function saveScreenshot<T extends PlayerComponent>( | ||
playerComponent: T, | ||
getSource: (player: T) => MediaInfo | null, | ||
) { | ||
const { fileManager, vault } = playerComponent.plugin.app; | ||
const player = playerComponent.store.getState().player; | ||
if (!player) { | ||
new Notice("Player not initialized"); | ||
return; | ||
} | ||
const mediaInfo = getSource(playerComponent); | ||
if (!mediaInfo) { | ||
new Notice("No media is opened"); | ||
return; | ||
} | ||
if (!player?.provider || !canProviderScreenshot(player.provider)) { | ||
new Notice("Screenshot is not supported for this media"); | ||
return; | ||
} | ||
const { blob, time } = await takeScreenshot(player.provider); | ||
const genTimestamp = createTimestampGen(time, mediaInfo, playerComponent); | ||
|
||
const ext = mime.getExtension(blob.type); | ||
if (!ext) { | ||
new Notice("Unknown mime type: " + blob.type); | ||
return; | ||
} | ||
|
||
const title = mediaTitle(mediaInfo, player.state); | ||
const screenshotName = title + toDurationISOString(time); | ||
const humanizedDuration = time > 0 ? ` - ${formatDuration(time)}` : ""; | ||
|
||
const { file: newNote, editor } = await openOrCreateMediaNote( | ||
mediaInfo, | ||
playerComponent, | ||
); | ||
const screenshotPath = await vault.getAvailablePathForAttachments( | ||
screenshotName, | ||
ext, | ||
newNote, | ||
); | ||
const screenshotFile = await vault.createBinary( | ||
screenshotPath, | ||
blob.arrayBuffer, | ||
); | ||
new Notice("Screenshot saved to " + screenshotFile.path); | ||
const timestamp = genTimestamp(newNote.path), | ||
screenshotEmbed = fileManager.generateMarkdownLink( | ||
screenshotFile, | ||
newNote.path, | ||
"", | ||
`${title}${humanizedDuration}|50`, | ||
); | ||
|
||
insertTimestamp(`- ${screenshotEmbed} ${timestamp}`, editor, () => | ||
playerComponent.containerEl.focus(), | ||
); | ||
} |
Oops, something went wrong.