Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: auto thumbnail at 60 seconds #717

Merged
merged 4 commits into from
Sep 19, 2023
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mage-website",
"version": "0.1.0",
"version": "0.1.01",
"license": "GPL-3.0",
"private": true,
"type": "module",
Expand Down
1 change: 1 addition & 0 deletions src/lib/assets/svg-json/image_urls.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"Spring": "/category-optimized/web2/spring-logo.svg",
"SQL": "/category-optimized/web2/sql.svg",
"Stack Overflow": "/category-optimized/web2/stack-overflow.svg",
"Stripe": "/category-optimized/web2/stripe.svg",
"Svelte": "/category-optimized/web2/svelte.svg",
"Swift": "/category-optimized/web2/swift.svg",
"Symfony": "/category-optimized/web2/symfony.svg",
Expand Down
1 change: 1 addition & 0 deletions src/lib/assets/svg-json/web2.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"Spring": "/category-optimized/web2/spring-logo.svg",
"SQL": "/category-optimized/web2/sql.svg",
"Stack Overflow": "/category-optimized/web2/stack-overflow.svg",
"Stripe": "/category-optimized/web2/stripe.svg",
"Svelte": "/category-optimized/web2/svelte.svg",
"Swift": "/category-optimized/web2/swift.svg",
"Symfony": "/category-optimized/web2/symfony.svg",
Expand Down
28 changes: 3 additions & 25 deletions src/lib/components/Channel/Chat/DrawerEditChannel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { category_list } from '$lib/stores/channelStore'
import { emitChannelUpdate } from '$lib/websocket'
import IconChatScreenshot from '$lib/assets/icons/chat/IconChatScreenshot.svelte'
import { captureScreenShot } from '$lib/utils'

export let channel: any, showDrawer: boolean

Expand All @@ -18,7 +19,7 @@
showAddCategory = false,
maxTag = 3,
maxCategory = 4,
imageSrc = ''
imageSrc: string = ''

$: maxTagLabel = channel?.tags.length == maxTag ? 'max reached' : 'max ' + maxTag
$: maxCategoryLabel =
Expand Down Expand Up @@ -55,30 +56,7 @@
e.preventDefault()
showThumbnail = true
if (channel.videoItems.length > 0) {
let screenElement = document.getElementById(
`screen-${channel.videoItems[0]._id}`
) as HTMLVideoElement
let webcamElement = document.getElementById(
`webcam-${channel.videoItems[0]._id}`
) as HTMLVideoElement
let canvas = document.createElement('canvas')
canvas.width = 1920
canvas.height = 1080

let ctx = canvas.getContext('2d')

console.log(screenElement)
console.log(webcamElement)

if (ctx !== null && screenElement !== null && webcamElement !== null) {
ctx.drawImage(screenElement, 0, 0, canvas.width, canvas.height)
ctx.globalAlpha = 1
ctx.drawImage(webcamElement, 1400, 750, canvas.width - 1400, canvas.height - 750)
}

let screenshot = canvas.toDataURL('image/jpeg')
imageSrc = screenshot
//thumbnailRef.setAttribute('src', screenshot);
imageSrc = captureScreenShot(channel)
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/lib/components/Channel/Chat/DropdownViewChannel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@
</li>
</ul>
<ul tabindex="-1" class="dropdown-content menu shadow bg-base-200 rounded-box md:w-[23rem]">
{#if channel.thumbnail}
<li class="flex flex-row justify-center w-full">
<div class="w-full shadow-xl">
<div class="items-center cursor-pointer">
<img src={channel.thumbnail} alt="Preview" class="rounded-lg h-full" />
</div>
</div>
</li>
{/if}

<li on:click={() => copyToClipboard(channel.description)} on:keyup>
<a class="text-sm max-w-md flex">{channel.description || 'No description'}</a>
</li>
Expand Down
25 changes: 23 additions & 2 deletions src/lib/components/Channel/Stream/VideoItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
is_sharing_obs
} from '$lib/stores/streamStore'
import { emitChannelUpdate } from '$lib/websocket'
import { getColoredRole, setRole } from '$lib/utils'
import { captureScreenShot, dataURLtoFile, getColoredRole, setRole } from '$lib/utils'
import IconChatBan from '$lib/assets/icons/chat/IconChatBan.svelte'
import { is_feature_stats_enabled, is_feature_obs_enabled } from '$lib/stores/remoteConfigStore'
import { addScreen, getScreen, removeScreen } from '$lib/stream-utils'
import IconDrawerVerification from '$lib/assets/icons/drawer/IconDrawerVerification.svelte'
import { get, patch } from '$lib/api'
import { get, patch, putImage } from '$lib/api'
import LibLoader from '$lib/components/Global/LibLoader.svelte'

export let video: any, channel: any
Expand Down Expand Up @@ -438,6 +438,27 @@
}
)
}

if (
!channel.thumbnail &&
video._id === channel.user &&
video._id === $page.data.user.userId &&
streamTime === 60
) {
const imageSrc = captureScreenShot(channel)
const file = dataURLtoFile(imageSrc, 'thumbnail-image')
if (file !== null && file.size > 0 && file.type !== '') {
await putImage(
`channels/thumbnail?channelId=${channel._id}&bucketName=thumbnails`,
file,
{
userId: $page.data.user?.userId,
token: $page.data.user?.token
}
)
channel.thumbnail = imageSrc
}
}
} catch (err) {
console.log('err', err)
}
Expand Down
38 changes: 38 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,41 @@ export const getNumberInThousands = (number: number) => {
return number.toString()
}
}

export const captureScreenShot = (channel: any) => {
const screenElement = document.getElementById(
`screen-${channel.videoItems[0]._id}`
) as HTMLVideoElement
const webcamElement = document.getElementById(
`webcam-${channel.videoItems[0]._id}`
) as HTMLVideoElement
const canvas = document.createElement('canvas')
canvas.width = 1920
canvas.height = 1080

const ctx = canvas.getContext('2d')

console.log(screenElement)
console.log(webcamElement)

if (ctx !== null && screenElement !== null && webcamElement !== null) {
ctx.drawImage(screenElement, 0, 0, canvas.width, canvas.height)
ctx.globalAlpha = 1
ctx.drawImage(webcamElement, 1400, 750, canvas.width - 1400, canvas.height - 750)
}

return canvas.toDataURL('image/jpeg')
//thumbnailRef.setAttribute('src', screenshot);
}

export const dataURLtoFile = (dataurl: string, filename: string) => {
const arr = dataurl.split(',')
const mime = (arr[0] && arr[0].match(/:(.*?);/)?.[1]) || ''
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], filename, { type: mime })
}
3 changes: 1 addition & 2 deletions src/routes/affiliate/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
<div class="mx-auto max-w-screen-md text-center mb-8 lg:mb-12">
<h2 class="mb-4 text-4xl tracking-tight font-extrabold">Designed for dedicated streamers</h2>
<p class="mb-5 font-light text-gray-500 sm:text-xl">
Collaborative streaming powered by AI. Our platform is designed to expand your reach and
grow your brand.
Collaborative streaming powered by AI. Expand your reach and grow your brand.
</p>
<a class="mb-5 link link-secondary" href={env.PUBLIC_STRIPE_BILLING_URL}
>Manage your subscription</a>
Expand Down
13 changes: 1 addition & 12 deletions src/routes/channel/[channelId]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import { putImage } from '$lib/api'
import { dataURLtoFile } from '$lib/utils'
import type { Actions } from './$types'

const dataURLtoFile = (dataurl: string, filename: string) => {
const arr = dataurl.split(',')
const mime = (arr[0] && arr[0].match(/:(.*?);/)?.[1]) || ''
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], filename, { type: mime })
}

export const actions = {
'edit-channel': async ({ request, locals }) => {
const data: FormData = await request.formData()
Expand Down
10 changes: 10 additions & 0 deletions static/category-optimized/web2/stripe.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading