Unverified Commit 12408a3f authored by Yohann MARTZOLFF's avatar Yohann MARTZOLFF Committed by GitHub

chore(examples): update with-apivideo (#39727)

Co-authored-by: 's avatarThibault Beyou <37510686+ThibaultBee@users.noreply.github.com>
parent 2cafe367
import Image from 'next/image'
import React from 'react'
import styled, { keyframes } from 'styled-components'
interface ILoaderProps {
done: boolean
}
const spin = keyframes`
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
`
const Spinner = styled.div`
border: 3px solid #f3f3f3;
border-top: 3px solid rgb(235, 137, 82);
border-radius: 50%;
width: 25px;
height: 25px;
animation: ${spin} 1s linear infinite;
`
const Loader: React.FC<ILoaderProps> = ({ done }): JSX.Element =>
done ? <Image src="/check.png" width={30} height={30} /> : <Spinner />
export default Loader
import styled from 'styled-components'
export const GlobalContainer = styled.div`
box-sizing: border-box;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
width: 100vw;
gap: 20px;
main {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
}
`
export const Header = styled.header`
font-size: 2.5rem;
margin-top: 2rem;
span {
font-weight: 700;
background: -webkit-linear-gradient(
45deg,
rgb(250, 91, 48) 0%,
rgb(128, 54, 255) 26.88%,
rgb(213, 63, 255) 50.44%,
rgb(235, 137, 82) 73.83%,
rgb(247, 181, 0) 100%
);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
`
export const TextsContainer = styled.div`
display: flex;
flex-direction: column;
gap: 20px;
padding: 3rem 5rem;
box-shadow: rgb(0 0 0 / 10%) 0px 2px 4px;
border-radius: 5px;
`
export const Text = styled.p`
text-align: center;
font-size: 1.1rem;
letter-spacing: 0.03rem;
a {
font-weight: 700;
}
`
export const Button = styled.button<{ $upload?: boolean }>`
background: ${(p) =>
p.$upload
? '-webkit-linear-gradient(45deg, rgb(250, 91, 48) 0%, rgb(235, 137, 82) 50%, rgb(247, 181, 0) 100%)'
: '-webkit-linear-gradient(45deg, rgb(247, 181, 0) 0%, rgb(235, 137, 82) 50%, rgb(250, 91, 48) 100%)'};
border: none;
padding: 0.8rem 1.2rem;
border-radius: 5px;
color: #ffffff;
cursor: pointer;
font-size: 1.2rem;
font-weight: 500;
`
export const StatusContainer = styled.div`
display: flex;
justify-content: center;
align-items: center;
gap: 2rem;
span {
width: 35px;
height: 5px;
border-radius: 5px;
background-color: rgb(235, 137, 82);
margin-top: 20px;
}
`
export const Footer = styled.footer`
margin-top: auto;
margin-bottom: 1rem;
display: flex;
align-items: center;
gap: 5px;
a:nth-of-type(2) {
font-weight: 600;
font-size: 1.1rem;
}
`
export const PlayerSdkContainer = styled.div<{
$width: number
$height: number
}>`
width: ${(p) => (p.$width && p.$width <= 800 ? p.$width : '800')}px;
height: ${(p) => (p.$height && p.$height <= 250 ? p.$height : '250')}px;
iframe {
height: ${(p) => (p.$height <= 250 ? p.$height : '250')}px !important;
}
`
export const InputsContainer = styled.div`
display: flex;
gap: 20px;
> div {
display: flex;
flex-direction: column;
gap: 5px;
label {
font-size: 0.6rem;
}
}
> div:last-child {
flex-direction: row;
align-items: center;
align-self: flex-end;
}
`
* {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}
html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
vertical-align: baseline;
}
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
display: block;
}
body {
line-height: 1;
}
ol,
ul {
list-style: none;
}
blockquote,
q {
quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
a {
text-decoration: none;
color: unset;
}
...@@ -4,28 +4,28 @@ This video uploader and playback app is built with Next.js and api.video, the vi ...@@ -4,28 +4,28 @@ This video uploader and playback app is built with Next.js and api.video, the vi
## Demo ## Demo
[https://apivideo-uploader.vercel.app/](https://apivideo-uploader.vercel.app/) [https://with-apivideo.vercel.app/videos](https://with-apivideo.vercel.app/videos)
## Deploy your own ## Deploy your own
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-apivideo-upload&project-name=with-apivideo-upload&repository-name=with-apivideo-upload) [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-apivideo&project-name=with-apivideo&repository-name=with-apivideo)
## How to use ## How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
```bash ```bash
npx create-next-app --example with-apivideo-upload with-apivideo-upload-app npx create-next-app --example with-apivideo-app
``` ```
```bash ```bash
yarn create next-app --example with-apivideo-upload with-apivideo-upload-app yarn create next-app --example with-apivideo-app
``` ```
```bash ```bash
pnpm create next-app --example with-apivideo-upload with-apivideo-upload-app pnpm create next-app --example with-apivideo-app
``` ```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
...@@ -34,19 +34,20 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut ...@@ -34,19 +34,20 @@ Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&ut
### 1. Create an api.video free account ### 1. Create an api.video free account
Go to [dashboard.api.video](https://dashboard.api.video/), log in or create a free account. 1. Go to [dashboard.api.video](https://dashboard.api.video/), log in or create a free account.
You can choose to stay in sandbox and have watermark over your videos, or enter in [production mode](https://api.video/pricing) and take advantage of all the features without limitations. 2. You can choose to stay in sandbox and have watermark over your videos, or enter in [production mode](https://api.video/pricing) and take advantage of all the features without limitations.
### 2. Get you API key ### 2. Get you API key
Once in the dashboard, find your API keys directly in the `/overview` or navigate to `/apikeys` with the "API Keys" button in the side navigation. 1. Once in the dashboard, find your API keys directly in the `/overview` or navigate to `/apikeys` with the "API Keys" button in the side navigation.
Copy your API key, and paste it in `.env.local.example` as value for `API_KEY`. 2. Copy your API key, and paste it in `.env.local.example` as value for `API_KEY`.
Rename `.env.local.example` to `.env.local`. 3. Rename `.env.local.example` to `.env.local`.
You can now try the application locally by running `npm run dev`, `yarn dev` or `pnpm dev` from the root directory. 4. Install the packages by running `npm install`, `yarn install` or `pnpm install`.
5. You can now try the application locally by running `npm run dev`, `yarn dev` or `pnpm dev` from the root directory.
### 3. Deployment ### 3. Deployment
First, push your app to GitHub/GitLab or Bitbucket 1. First, push your app to GitHub/GitLab or Bitbucket
The, go to [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) and import your new repository. 2. Then, go to [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) and import your new repository.
Add an environment variable with name `API_KEY` and your API key for value. 3. Add an environment variable with name `API_KEY` and your API key for value.
Click on deploy 🎉 4. Click on deploy 🎉
import React from 'react' .container {
import styled from 'styled-components'
import Image from 'next/image'
interface ICardProps {
content: string
url: string
method: 'get' | 'post'
}
const Container = styled.a`
border: 1px solid rgb(215, 219, 236); border: 1px solid rgb(215, 219, 236);
border-radius: 0.25rem; border-radius: 0.25rem;
background-color: #ffffff; background-color: #ffffff;
...@@ -20,42 +10,39 @@ const Container = styled.a` ...@@ -20,42 +10,39 @@ const Container = styled.a`
font-size: 0.5rem; font-size: 0.5rem;
position: relative; position: relative;
box-shadow: rgb(0 0 0 / 5%) 0px 2px 4px; box-shadow: rgb(0 0 0 / 5%) 0px 2px 4px;
` }
.container > p {
letter-spacing: 0.05rem;
}
const Method = styled.div<{ $method: 'get' | 'post' }>` .method {
color: #ffffff; color: #ffffff;
background-color: ${(p) => (p.$method === 'get' ? 'green' : 'blue')};
padding: 0.3rem; padding: 0.3rem;
border-radius: 2px; border-radius: 2px;
font-weight: 500; font-weight: 500;
` }
.get {
const Content = styled.p` composes: method;
letter-spacing: 0.05rem; background-color: green;
` }
.post {
composes: method;
background-color: blue;
}
const ImageContainer = styled.div` .image_container {
position: absolute; position: absolute;
bottom: -25px; bottom: -25px;
right: -105px; right: -105px;
display: flex; display: flex;
gap: 5px; gap: 5px;
align-items: flex-end; align-items: flex-end;
p { }
.image_container > p {
margin-bottom: -3px; margin-bottom: -3px;
font-size: 0.5rem; font-size: 0.5rem;
} }
`
const Card: React.FC<ICardProps> = ({ content, url, method }): JSX.Element => (
<Container target="_blank" href={url}>
<Method $method={method}>{method.toUpperCase()}</Method>
<Content>{content}</Content>
<ImageContainer>
<Image src="/arrow.png" alt="Sketch arrow" width={20} height={20} />
<p>Try it out with our API!</p>
</ImageContainer>
</Container>
)
export default Card #foo {
font-size: large;
}
import React from 'react'
import Image from 'next/image'
import styles from './Card.module.css'
interface ICardProps {
content: string
url: string
method: 'get' | 'post'
}
const Card: React.FC<ICardProps> = ({ content, url, method }): JSX.Element => (
<a className={styles.container} target="_blank" href={url} rel="noreferrer">
<div className={`${method === 'get' ? styles.get : styles.post}`}>
{method.toUpperCase()}
</div>
<p>{content}</p>
<div className={styles.image_container}>
<Image src="/arrow.png" alt="Sketch arrow" width={20} height={20} />
<p>Try it out with our API!</p>
</div>
</a>
)
export default Card
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
.spinner {
border: 3px solid #f3f3f3;
border-top: 3px solid rgb(235, 137, 82);
border-radius: 50%;
width: 25px;
height: 25px;
animation: spin 1s linear infinite;
}
import Image from 'next/image'
import React from 'react'
import styles from './Loader.module.css'
interface ILoaderProps {
done: boolean
}
const Loader: React.FC<ILoaderProps> = ({ done }): JSX.Element =>
done ? (
<Image src="/check.png" width={30} height={30} />
) : (
<div className={styles.spinner} />
)
export default Loader
.container {
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
}
import React from 'react' import React from 'react'
import styled from 'styled-components'
import Loader from '../Loader' import Loader from '../Loader'
import styles from './Status.module.css'
interface IStatusProps { interface IStatusProps {
done: boolean done: boolean
title: string title: string
} }
const Container = styled.div`
display: flex;
flex-direction: column;
align-items: center;
gap: 5px;
`
const Status: React.FC<IStatusProps> = ({ done, title }): JSX.Element => ( const Status: React.FC<IStatusProps> = ({ done, title }): JSX.Element => (
<Container> <div className={styles.container}>
<p>{title}</p> <p>{title}</p>
<Loader done={done} /> <Loader done={done} />
</Container> </div>
) )
export default Status export default Status
...@@ -4,6 +4,9 @@ const nextConfig = { ...@@ -4,6 +4,9 @@ const nextConfig = {
compiler: { compiler: {
styledComponents: true, styledComponents: true,
}, },
images: {
domains: ['cdn.api.video'],
},
} }
module.exports = nextConfig module.exports = nextConfig
...@@ -7,19 +7,17 @@ ...@@ -7,19 +7,17 @@
}, },
"dependencies": { "dependencies": {
"@api.video/nodejs-client": "2.2.5", "@api.video/nodejs-client": "2.2.5",
"@api.video/player-sdk": "1.2.13", "@api.video/react-player": "^1.0.1",
"@api.video/video-uploader": "1.0.4", "@api.video/video-uploader": "1.0.4",
"@types/styled-components": "5.1.24",
"next": "latest", "next": "latest",
"react": "18.1.0", "react": "18.1.0",
"react-dom": "18.1.0", "react-dom": "18.1.0"
"styled-components": "5.3.5",
"swr": "1.2.2"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "17.0.23", "@types/node": "17.0.23",
"@types/react": "17.0.43", "@types/react": "17.0.43",
"@types/react-dom": "17.0.14", "@types/react-dom": "17.0.14",
"eslint-config-next": "latest",
"typescript": "4.6.3" "typescript": "4.6.3"
} }
} }
import '../style/index.css' import '../style/index.css'
import '../style/common.css'
function MyApp({ Component, pageProps }: any) { function MyApp({ Component, pageProps }: any) {
return <Component {...pageProps} /> return <Component {...pageProps} />
......
import type { NextApiRequest, NextApiResponse } from 'next'
import ApiVideoClient from '@api.video/nodejs-client'
const getVideoStatus = async (req: NextApiRequest, res: NextApiResponse) => {
try {
const client = new ApiVideoClient({ apiKey: process.env.API_KEY })
const videos = await client.videos.list()
res.status(200).json({ videos })
} catch (error) {
res.status(400).end()
}
}
export default getVideoStatus
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import React from 'react'
const Home: NextPage = () => (
<div className="global-container">
<Head>
<title>api.video sample app</title>
<meta
name="description"
content="Generated by create next app & created by api.video"
/>
<link rel="icon" href="/favicon.ico" />
</Head>
<header>
<span>api.video sample app</span>
</header>
<main>
<div className="texts-container">
<p>
Hey fellow dev! 👋 <br />
In this basic sample app, you will find{' '}
<a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video
</a>{' '}
features examples such as <a href="/uploader">video uploader</a>,{' '}
<a href="/videos">videos list</a> and player components.
</p>
<p>
api.video provides APIs and clients to handle all your video needs.
<br />
This app is built with the api.video{' '}
<a
href="https://github.com/apivideo/api.video-nodejs-client"
target="_blank"
rel="noopener noreferrer"
>
Node.js client
</a>
,{' '}
<a
href="https://github.com/apivideo/api.video-typescript-uploader"
target="_blank"
rel="noopener noreferrer"
>
Typescript uploader
</a>{' '}
and{' '}
<a
target="_blank"
rel="noopener noreferrer"
href="https://github.com/apivideo/api.video-react-player"
>
React player component
</a>
.
</p>
<p>
You can{' '}
<a
href="https://github.com/vercel/next.js/tree/canary/examples/with-apivideo"
target="_blank"
rel="noopener noreferrer"
>
check the source code on GitHub
</a>
.
</p>
<p>Please, choose a feature to explore.</p>
</div>
<div className="status-container">
<a className="button" href="/uploader">
Upload a video
</a>
<a className="button" href="/videos">
See my videos
</a>
</div>
</main>
<footer>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
<span>and</span>
<a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video
</a>
</footer>
</div>
)
export default Home
...@@ -2,26 +2,15 @@ import type { NextPage } from 'next' ...@@ -2,26 +2,15 @@ import type { NextPage } from 'next'
import Head from 'next/head' import Head from 'next/head'
import Image from 'next/image' import Image from 'next/image'
import React, { ChangeEvent, useEffect, useRef, useState } from 'react' import React, { ChangeEvent, useEffect, useRef, useState } from 'react'
import Card from '../components/Card' import Card from '../../components/Card'
import { VideoUploader, VideoUploadResponse } from '@api.video/video-uploader' import { VideoUploader, VideoUploadResponse } from '@api.video/video-uploader'
import Status from '../components/Status' import Status from '../../components/Status'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import {
Button,
Footer,
GlobalContainer,
Header,
StatusContainer,
Text,
TextsContainer,
} from '../style/common'
import useSWR from 'swr'
const fetcher = async (url: string): Promise<any> => { const Uploader: NextPage = () => {
return fetch(url).then((res) => res.json()) const [uploadToken, setUploadToken] = useState<{ token: string } | undefined>(
} undefined
)
const Home: NextPage = () => {
const [uploadProgress, setUploadProgress] = useState<number | undefined>( const [uploadProgress, setUploadProgress] = useState<number | undefined>(
undefined undefined
) )
...@@ -31,47 +20,33 @@ const Home: NextPage = () => { ...@@ -31,47 +20,33 @@ const Home: NextPage = () => {
{ ingested: false, encoded: false } { ingested: false, encoded: false }
) )
const [interId, setInterId] = useState<number | undefined>(undefined) const [interId, setInterId] = useState<number | undefined>(undefined)
const [size, setSize] = useState<
{ width: number; height: number } | undefined
>(undefined)
const inputRef = useRef<HTMLInputElement>(null) const inputRef = useRef<HTMLInputElement>(null)
const router = useRouter() const router = useRouter()
const { data: uploadToken } = useSWR<{ token: string }>( useEffect(() => {
'/api/uploadToken', fetch('/api/uploadToken')
fetcher .then((res) => res.json())
) .then((res) => setUploadToken(res))
}, [])
useEffect(() => {
if (video) {
const fetchVideoStatus = async (videoId: string): Promise<void> => { const fetchVideoStatus = async (videoId: string): Promise<void> => {
const { status } = await fetcher(`/api/${videoId}`) const { status } = await fetch(`/api/${videoId}`).then((res) =>
res.json()
)
const { encoding, ingest } = status const { encoding, ingest } = status
setStatus({ setStatus({
ingested: ingest.status === 'uploaded', ingested: ingest.status === 'uploaded',
encoded: encoding.playable, encoded: encoding.playable,
}) })
if (ingest.status === 'uploaded' && encoding.playable) { if (ingest.status === 'uploaded' && encoding.playable) setReady(true)
setSize({
width: encoding.metadata.width,
height: encoding.metadata.height,
})
setReady(true)
}
}
const clearState = (): void => {
setReady(false)
setStatus({ ingested: false, encoded: false })
setVideo(undefined)
setUploadProgress(undefined)
setSize(undefined)
} }
useEffect(() => {
if (video) {
const intervalId = window.setInterval(() => { const intervalId = window.setInterval(() => {
fetchVideoStatus(video.videoId) fetchVideoStatus(video.videoId)
}, 1000) }, 1000)
setInterId(intervalId) setInterId(intervalId)
return () => window.clearInterval(intervalId)
} }
}, [video, ready]) }, [video, ready])
useEffect(() => { useEffect(() => {
...@@ -82,6 +57,13 @@ const Home: NextPage = () => { ...@@ -82,6 +57,13 @@ const Home: NextPage = () => {
e: ChangeEvent<HTMLInputElement> e: ChangeEvent<HTMLInputElement>
): Promise<void> => { ): Promise<void> => {
e.preventDefault() e.preventDefault()
if (!uploadToken || !uploadToken.token) return
const clearState = (): void => {
setReady(false)
setStatus({ ingested: false, encoded: false })
setVideo(undefined)
setUploadProgress(undefined)
}
clearState() clearState()
if (!e.target.files || !uploadToken) return if (!e.target.files || !uploadToken) return
const file = e.target.files[0] const file = e.target.files[0]
...@@ -98,11 +80,11 @@ const Home: NextPage = () => { ...@@ -98,11 +80,11 @@ const Home: NextPage = () => {
const handleNavigate = (): void => { const handleNavigate = (): void => {
if (!video) return if (!video) return
router.push(`/${video.videoId}?w=${size?.width}&h=${size?.height}`) router.push(`/videos/${video.videoId}?uploaded=1`)
} }
return ( return (
<GlobalContainer> <div className="global-container">
<Head> <Head>
<title>Video Uploader</title> <title>Video Uploader</title>
<meta <meta
...@@ -112,13 +94,13 @@ const Home: NextPage = () => { ...@@ -112,13 +94,13 @@ const Home: NextPage = () => {
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />
</Head> </Head>
<Header> <header>
<span>api.video uploader</span> 🚀 <span>api.video uploader</span> 🚀
</Header> </header>
<main> <main>
<TextsContainer> <div className="texts-container">
<Text> <p>
Hey fellow dev! 👋 <br /> Hey fellow dev! 👋 <br />
Welcome to this basic example of video uploader provided by{' '} Welcome to this basic example of video uploader provided by{' '}
<a <a
...@@ -137,8 +119,8 @@ const Home: NextPage = () => { ...@@ -137,8 +119,8 @@ const Home: NextPage = () => {
Vercel & Next.js Vercel & Next.js
</a> </a>
. .
</Text> </p>
<Text> <p>
api.video provides APIs and clients to handle all your video needs. api.video provides APIs and clients to handle all your video needs.
<br /> <br />
This app is built with the{' '} This app is built with the{' '}
...@@ -158,8 +140,8 @@ const Home: NextPage = () => { ...@@ -158,8 +140,8 @@ const Home: NextPage = () => {
Typescript uploader Typescript uploader
</a> </a>
. .
</Text> </p>
<Text> <p>
You can{' '} You can{' '}
<a <a
href="https://github.com/vercel/next.js/tree/canary/examples/with-apivideo-upload" href="https://github.com/vercel/next.js/tree/canary/examples/with-apivideo-upload"
...@@ -169,18 +151,21 @@ const Home: NextPage = () => { ...@@ -169,18 +151,21 @@ const Home: NextPage = () => {
check the source code on GitHub check the source code on GitHub
</a> </a>
. .
</Text> </p>
<Text> <p>
Please add a video to upload and let the power of the API do the Please add a video to upload and let the power of the API do the
rest 🎩 rest 🎩
</Text> </p>
</TextsContainer> </div>
{!uploadProgress ? ( {!uploadProgress ? (
<> <>
<Button $upload onClick={() => inputRef.current?.click()}> <button
className="upload"
onClick={() => inputRef.current?.click()}
>
Select a file Select a file
</Button> </button>
<input <input
ref={inputRef} ref={inputRef}
hidden hidden
...@@ -191,13 +176,13 @@ const Home: NextPage = () => { ...@@ -191,13 +176,13 @@ const Home: NextPage = () => {
</> </>
) : ( ) : (
<> <>
<StatusContainer> <div className="status-container">
<Status title="Uploaded" done={uploadProgress >= 100} /> <Status title="Uploaded" done={uploadProgress >= 100} />
<span /> <span />
<Status title="Ingested" done={status.ingested} /> <Status title="Ingested" done={status.ingested} />
<span /> <span />
<Status title="Playable" done={status.encoded} /> <Status title="Playable" done={status.encoded} />
</StatusContainer> </div>
<Card <Card
content="https://ws.api.video/videos/{videoId}/source" content="https://ws.api.video/videos/{videoId}/source"
url="https://docs.api.video/reference/post_videos-videoid-source" url="https://docs.api.video/reference/post_videos-videoid-source"
...@@ -207,11 +192,13 @@ const Home: NextPage = () => { ...@@ -207,11 +192,13 @@ const Home: NextPage = () => {
)} )}
{ready && video && ( {ready && video && (
<Button onClick={handleNavigate}>Watch it 🍿</Button> <button className="upload" onClick={handleNavigate}>
Watch it 🍿
</button>
)} )}
</main> </main>
<Footer> <footer>
<a <a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank" target="_blank"
...@@ -226,9 +213,9 @@ const Home: NextPage = () => { ...@@ -226,9 +213,9 @@ const Home: NextPage = () => {
<a href="https://api.video" target="_blank" rel="noopener noreferrer"> <a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video api.video
</a> </a>
</Footer> </footer>
</GlobalContainer> </div>
) )
} }
export default Home export default Uploader
import { PlayerSdk, PlayerTheme } from '@api.video/player-sdk'
import { GetServerSideProps, NextPage } from 'next' import { GetServerSideProps, NextPage } from 'next'
import Head from 'next/head' import Head from 'next/head'
import Image from 'next/image' import Image from 'next/image'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import React, { ChangeEvent, useEffect, useState } from 'react' import React, { ChangeEvent, useState } from 'react'
import { import ApiVideoPlayer, { PlayerTheme } from '@api.video/react-player'
Button,
Footer,
GlobalContainer,
Header,
InputsContainer,
PlayerSdkContainer,
Text,
TextsContainer,
} from '../style/common'
interface IVideoViewProps { interface IVideoViewProps {
children: React.ReactNode children: React.ReactNode
videoId: string videoId: string
width: string uploaded: string
height: string
} }
const VideoView: NextPage<IVideoViewProps> = ({ const VideoView: NextPage<IVideoViewProps> = ({
videoId, videoId,
width, uploaded,
height,
}): JSX.Element => { }): JSX.Element => {
const [player, setPlayer] = useState<PlayerSdk | undefined>(undefined) const [playerTheme, setPlayerTheme] = useState<PlayerTheme>({
const [playerSettings, setPlayerSettings] = useState<PlayerTheme>({
link: 'rgb(235, 137, 82)', link: 'rgb(235, 137, 82)',
linkHover: 'rgb(240, 95, 12)', linkHover: 'rgb(240, 95, 12)',
}) })
const [hideControls, setHideControls] = useState<boolean>(false) const [hideControls, setHideControls] = useState<boolean>(false)
const router = useRouter() const router = useRouter()
useEffect(() => { const handleChangeSetting = (e: ChangeEvent<HTMLInputElement>) =>
const player = new PlayerSdk('#player', { setPlayerTheme({
id: videoId, ...playerTheme,
[e.currentTarget.id]: e.currentTarget.value,
}) })
player.setTheme({
link: 'rgb(235, 137, 82)',
linkHover: 'rgb(240, 95, 12)',
})
setPlayer(player)
}, [videoId])
useEffect(() => {
player && player?.loadConfig({ id: videoId, hideControls: hideControls })
}, [hideControls, player, videoId])
const handleChangeSetting = (
e: ChangeEvent<HTMLInputElement>,
prop: string
) => {
const newSettings = { ...playerSettings, [prop]: e.currentTarget.value }
setPlayerSettings(newSettings)
player?.setTheme(newSettings)
}
return ( return (
<GlobalContainer> <div className="global-container">
<Head> <Head>
<title>Video view</title> <title>Video view</title>
<meta <meta
...@@ -67,44 +37,54 @@ const VideoView: NextPage<IVideoViewProps> = ({ ...@@ -67,44 +37,54 @@ const VideoView: NextPage<IVideoViewProps> = ({
/> />
<link rel="icon" href="/favicon.ico" /> <link rel="icon" href="/favicon.ico" />
</Head> </Head>
<Header> <header>
{uploaded ? (
<>
<span>Already there</span> 🎉 <span>Already there</span> 🎉
</Header> </>
) : (
<>
<span>Here's your player</span> 👀
</>
)}
</header>
<main> <main>
<TextsContainer> <div className="texts-container">
<Text> <p>
This player is generated by the{' '} This player is generated by the{' '}
<a <a
target="_blank" target="_blank"
rel="noopener noreferrer" rel="noopener noreferrer"
href="https://github.com/apivideo/api.video-player-sdk" href="https://github.com/apivideo/api.video-react-player"
> >
api.video's Player SDK api.video's React player component
</a> </a>
.<br /> .<br />
It provides multiple properties to customize your video player. It provides multiple properties to customize your video player.
</Text> </p>
<Text>Try 3 of them just bellow 👇</Text> <p>Try 3 of them just bellow 👇</p>
</TextsContainer> </div>
<InputsContainer> <div className="inputs-container">
<div> <div>
<label>Play button color</label> <label>Play button color</label>
<input <input
id="link"
type="text" type="text"
placeholder="Play button color" placeholder="Play button color"
value={playerSettings.link} value={playerTheme.link}
onChange={(e) => handleChangeSetting(e, 'link')} onChange={handleChangeSetting}
/> />
</div> </div>
<div> <div>
<label>Buttons hover color</label> <label>Buttons hover color</label>
<input <input
id="linkHover"
type="text" type="text"
placeholder="Buttons hover color" placeholder="Buttons hover color"
value={playerSettings.linkHover} value={playerTheme.linkHover}
onChange={(e) => handleChangeSetting(e, 'linkHover')} onChange={handleChangeSetting}
/> />
</div> </div>
<div> <div>
...@@ -117,17 +97,23 @@ const VideoView: NextPage<IVideoViewProps> = ({ ...@@ -117,17 +97,23 @@ const VideoView: NextPage<IVideoViewProps> = ({
/> />
<label>Hide controls</label> <label>Hide controls</label>
</div> </div>
</InputsContainer> </div>
<PlayerSdkContainer <ApiVideoPlayer
id="player" video={{ id: videoId }}
$width={parseInt(width)} style={{ width: '75%', height: 300 }}
$height={parseInt(height)} theme={playerTheme}
chromeless={hideControls}
/> />
<Button onClick={() => router.push('/')}>Another video?</Button> <button
className="upload"
onClick={() => router.push(uploaded ? '/uploader' : '/videos')}
>
{uploaded ? 'Another video?' : 'Videos list'}
</button>
</main> </main>
<Footer> <footer>
<a <a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app" href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank" target="_blank"
...@@ -142,14 +128,14 @@ const VideoView: NextPage<IVideoViewProps> = ({ ...@@ -142,14 +128,14 @@ const VideoView: NextPage<IVideoViewProps> = ({
<a href="https://api.video" target="_blank" rel="noopener noreferrer"> <a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video api.video
</a> </a>
</Footer> </footer>
</GlobalContainer> </div>
) )
} }
export default VideoView export default VideoView
export const getServerSideProps: GetServerSideProps = async (context) => { export const getServerSideProps: GetServerSideProps = async (context) => {
const { videoId, w, h } = context.query const { videoId, uploaded } = context.query
return { props: { videoId, width: w ?? null, height: h ?? null } } return { props: { videoId, uploaded: uploaded ?? null } }
} }
import type { NextPage } from 'next'
import Head from 'next/head'
import Image from 'next/image'
import React, { useEffect, useState } from 'react'
import VideosListResponse from '@api.video/nodejs-client/lib/model/VideosListResponse'
const Videos: NextPage = () => {
const [videosResponse, setVideosResponse] = useState<
VideosListResponse | undefined
>(undefined)
const [error, setError] = useState<boolean>(false)
useEffect(() => {
fetch('/api/videos')
.then((res) => res.json())
.then((res: { videos: VideosListResponse }) =>
setVideosResponse(res.videos)
)
.catch((_) => setError(true))
}, [])
return (
<div className="global-container">
<Head>
<title>Videos List</title>
<meta
name="description"
content="Generated by create next app & created by api.video"
/>
<link rel="icon" href="/favicon.ico" />
</Head>
<header>
<span>api.video videos list</span> 📚
</header>
<main>
<div className="texts-container">
<p>
Welcome to this basic example of videos list provided by{' '}
<a
href="https://api.video"
target="_blank"
rel="noopener noreferrer"
>
api.video
</a>
.
</p>
<p>
Please, add your api.video API key in your <i>.env</i> file and let
the power of the API do the rest 🎩
</p>
</div>
{!videosResponse && !error && <div>Loading...</div>}
{error && (
<div className="error">
An error occured trying to fetch your videos. Be sure to have you
API key set in your .env file this way: <i>API_KEY=YOUR_API_KEY</i>
</div>
)}
{videosResponse && videosResponse.data?.length > 0 && (
<div className="videos-list">
{videosResponse.data.map((video) => (
<a
className="video-card"
href={`/videos/${video.videoId}`}
key={video.videoId}
>
<h3>{video.title}</h3>
<Image
src={video.assets?.thumbnail ?? '/vercel.svg'}
alt="Video thumbmail"
width={150}
height={80}
/>
</a>
))}
</div>
)}
{videosResponse && videosResponse.data?.length === 0 && (
<>
<p>You don't have any video yet 🧐</p>
<a className="button" href="/uploader">
Upload my first video
</a>
</>
)}
</main>
<footer>
<a
href="https://vercel.com?utm_source=create-next-app&utm_medium=default-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Powered by{' '}
<span>
<Image src="/vercel.svg" alt="Vercel Logo" width={72} height={16} />
</span>
</a>
<span>and</span>
<a href="https://api.video" target="_blank" rel="noopener noreferrer">
api.video
</a>
</footer>
</div>
)
}
export default Videos
header {
font-size: 2.5rem;
margin-top: 2rem;
}
header > span {
font-weight: 700;
background: -webkit-linear-gradient(
45deg,
rgb(250, 91, 48) 0%,
rgb(128, 54, 255) 26.88%,
rgb(213, 63, 255) 50.44%,
rgb(235, 137, 82) 73.83%,
rgb(247, 181, 0) 100%
);
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
footer {
margin-top: auto;
margin-bottom: 1rem;
display: flex;
align-items: center;
gap: 5px;
}
footer > a:nth-child(3) {
font-weight: 600;
font-size: 1.1rem;
}
p {
text-align: center;
font-size: 1.1rem;
letter-spacing: 0.03rem;
}
p > a {
font-weight: 700;
text-decoration: underline;
}
p > a:hover {
color: rgb(2, 2, 102);
}
.global-container {
box-sizing: border-box;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
width: 100vw;
gap: 20px;
}
.global-container main {
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
gap: 20px;
}
.texts-container {
display: flex;
flex-direction: column;
gap: 20px;
padding: 3rem 5rem;
box-shadow: rgb(0 0 0 / 10%) 0px 2px 4px;
border-radius: 5px;
}
.texts-container > p {
text-align: center;
font-size: 1.1rem;
letter-spacing: 0.03rem;
}
.texts-container > p a {
font-weight: 700;
}
.videos-list {
display: flex;
flex-wrap: wrap;
justify-content: center;
align-items: stretch;
gap: 1rem;
}
.videos-list .video-card {
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 1rem 2rem;
box-shadow: rgb(0 0 0 / 10%) 0px 2px 4px;
border-radius: 5px;
max-width: 250px;
text-align: center;
transition: transform 200ms ease-in-out;
}
.videos-list .video-card:hover {
transform: scale(1.05);
}
button.upload,
a.button {
background: -webkit-linear-gradient(
45deg,
rgb(250, 91, 48) 0%,
rgb(235, 137, 82) 50%,
rgb(247, 181, 0) 100%
);
border: none;
padding: 0.8rem 1.2rem;
border-radius: 5px;
color: #ffffff;
cursor: pointer;
font-size: 1.2rem;
font-weight: 500;
}
.status-container {
display: flex;
justify-content: center;
align-items: center;
gap: 2rem;
}
.status-container > span {
width: 35px;
height: 5px;
border-radius: 5px;
background-color: rgb(235, 137, 82);
margin-top: 20px;
}
.inputs-container {
display: flex;
gap: 20px;
}
.inputs-container > div {
display: flex;
flex-direction: column;
gap: 5px;
}
.inputs-container > div label {
font-size: 0.6rem;
}
.inputs-container > div:last-child {
flex-direction: row;
align-items: center;
align-self: flex-end;
}
.error {
color: rgb(250, 91, 48);
}
.error i {
font-weight: 600;
}
html,
body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
}
a {
color: inherit;
text-decoration: none;
}
p {
margin: 0 !important;
}
* {
box-sizing: border-box;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment