Skip to content

Youtube Clone

Updated: at 01:30 PM

yt clone cover image

Table of contents

Open Table of contents

Overview

Youtube in itself is a complex app. I have tried to understand and build the video processing model of youtube my means of backend communication. I am using Google cloud as cloud provider and firebase function for required actions. I am also using pub/sub feature of GCP to achieve the requirement

Tech Stack

System Design

system-design

Video Processing Service

# youtube-clone/video-processing-service
npm init -y
npx tsc --init
npm i express @types/express
npm i -g ts-node
npm i fluent-ffmpeg @types/fluent-ffmpeg
npm i @google-cloud/storage
npm i firebase-admin
# For mac using brew
brew install ffmpeg

Deploy to Firebase

Sign in to console.firebase.google.com and create a new project, analytics diabled, can be enabled for better experience

firebase-dashboard

Project ID: yt-devclone-d5b1f

Setup Cloud

  1. Enable Artifact Registry API on cloud to store docker image

  2. Login cloud CLO locally

# Login to account
gcloud auth login
  1. Set the project
# Set project id
gcloud config set project yt-devclone-d5b1f
  1. Update the components
gcloud components update

Create repository on Artifact

artifact-repo

Repository created

Create 2 buckets (should be unique all over GCP)

  1. ytclone01-raw-videos-bucket
  2. ytclone01-processed-videos-bucket

raw-bucket-config

processed-video-config

Video-processing-service Code

Create docker image and push to artifact

sudo docker build --platform=linux/amd64 -t asia-south1-docker.pkg.dev/yt-devclone-d5b1f/video-processing-repository/video-processing-service .
gcloud auth configure-docker asia-south1-docker.pkg.dev
docker push asia-south1-docker.pkg.dev/yt-devclone-d5b1f/video-processing-repository/video-processing-service

Image pushed successfully

final-image-pushed

Cloud Run and Pub/Sub Config

Create Cloud Run service to run the docker file

  1. Select the image from Artifact

cloudrun-image-add

  1. Set Authentication to “Allow unauthenticated invocation”
  2. Set CPU allocation and pricing to “CPU is only allocated during request processing”
  3. Revision autoscaling can be set as per requirement, I will go with min 0, max 1
  4. Set Ingress Control to “Internal” and create the function.

Copy the URL of the Cloud Run

url-cloudrun

Create a Pub/Sub

topic-creating

add-subscription

  1. Create a subscription ID
  2. Set Delivery Type to “Push”
  3. Add the Endpoint URL suffix with /processing-video
  4. Message retention duration “7 Days” by default values
  5. Expiration period set to “Never expire”
  6. Set Acknowledgement deadline to “600”
  7. Leave rest to default and create the subscription

Add a notification

VIP STEP

gsutil notification create -t video-processing-topic -f json OBJECT_FINALIZE gs://ytclone-raw-videos-bucket

WEB-APP

Generate Firebase SDK for the web-app

sdk-code

Add Authentication in Firebase

auth-firebase

Create Firestore Database

Firebase API Service

Install firebase-tools globally

$ npm install -g firebase-tools

Login to firebase

# youtube-clone/yt-api-service
firebase login

Init the firebase api service

firebase init functions
-- select the existing project
-- select Typescript
-- select EsLint yes

Install functions and admin

npm i firebase-functions@latest firebase-admin@latest

Deploy the functions

npm run deploy
# To separately deploy the functions
firebase deploy --only functions:GenerateUploadUrl

After deployment, it should reflect as this on cloud

generateuplaodurl

Copy the service url of GenerateUploadUrl function to access the raw bucket, copy it from here

url-generateuploadurl

Go to raw bucket → Permissions

raw-bucket-permission

Add the service url and add role Storage Object Admin

principle-raw-uploadurl

Got IAM & Admin

IAM

Grab the same service url and add one more role, Service Account Token Creator

iam-one-more-rule

Add cors policy

youtube-clone/utils/gcp-cors.json

[
    {
        "origin": ["*"],
        "responseHeader": ["Content-Type"],
        "method": ["PUT"],
        "maxAgeSeconds": 3600
    }
]

Implement this cors policy

gcloud storage buckets update gs://ytclone-raw-videos-bucket --cors-file=utils/gcs-cors.json

Enable IAM Service Account Credentials API

iam-api

Add .env.local file with following attributes in frontend app

NEXT_PUBLIC_FIREBASE_API_KEY=BCdeFgHiJ0kLmNOP1qR9sTuv2Wxy2Za8B2C5dEf
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=yt-clone-64927.firebaseapp.com
NEXT_PUBLIC_PROJECT_ID=yt-clone-64927
NEXT_PUBLIC_APPID=1:101057801427:web:g3b15bf505e28f54afd9db
NEXT_PUBLIC_VIDEO_PREFIX=https://storage.googleapis.com/ytclone-processed-videos-bucket/

You are good to go, deploy the frontend on platform like vercel, or you can create a docker image for same and deploy through Artifact on GCP.


Previous Post
NoteMe APP
Next Post
Google OAuth Service