Skip to content

Commit

Permalink
feat: support h265 for video embeds (#10)
Browse files Browse the repository at this point in the history
thanks @Valafi
  • Loading branch information
okdargy committed Aug 6, 2024
1 parent 8e86d8d commit e42dab9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dev": "wrangler dev src/index.ts --env=local",
"build": "wrangler build src/index.ts",
"deploy": "wrangler deploy --minify src/index.ts",
"deploy:staging": "wrangler deploy --env=staging --minify src/index.ts",
"test": "jest",
"prettier": "prettier . --write"
},
Expand Down
29 changes: 16 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,25 +210,28 @@ app.get('/generate/alternate', (c) => {

app.get('/generate/video/:videoId', async (c) => {
const { videoId } = c.req.param()
const forceLow = c.req.query('h264') === 'true' || c.req.query('encoder') === 'h264' || c.req.query('quality') === 'h264'

try {
// To ensure the video is valid, decrease load on TikWM by checking the video data first
const data = await scrapeVideoData(videoId)

/*
if (!(data instanceof Error)) {
if(data.video.playAddr) {
return c.redirect(data.video.playAddr)
} else {
return new Response('No video found', { status: 404,
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate',
}
})
}
if (data instanceof Error) {
return new Response((data as Error).message, {
status: 500,
headers: {
'Cache-Control': 'no-cache, no-store, must-revalidate'
}
*/
})
}

const highAvailable = data.video.bitrateInfo.find((bitrate) => bitrate.CodecType.includes('h265'))

return c.redirect(`https://tikwm.com/video/media/play/${videoId}.mp4`)
if (highAvailable && !forceLow) {
return c.redirect(`https://tikwm.com/video/media/hdplay/${videoId}.mp4`)
} else {
return c.redirect(`https://tikwm.com/video/media/play/${videoId}.mp4`)
}
} catch (e) {
return new Response((e as Error).message, {
status: 500,
Expand Down
34 changes: 34 additions & 0 deletions src/types/Web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2916,7 +2916,18 @@ export interface Video {
downloadAddr: string
shareCover: string[]
reflowCover: string
bitrate: number
encodedType: string
format: string
videoQuality: string
encodeUserTag: string
codecType: string
definition: string
subtitleInfos: any[]
zoomCover: ZoomCover
volumeInfo: VolumeInfo
bitrateInfo: BitrateInfo[]
VQScore: string
}

export interface ZoomCover {
Expand All @@ -2926,6 +2937,29 @@ export interface ZoomCover {
'960': string
}

export interface VolumeInfo {
Loudness: number
Peak: number
}

export interface BitrateInfo {
Bitrate: number
QualityType: number
GearName: string
PlayAddr: PlayAddr
CodecType: string
MVMAF: string
}

export interface PlayAddr {
DataSize: string
Uri: string
UrlList: string[]
UrlKey: string
FileHash: string
FileCs: string
}

export interface Author {
id: string
shortId: string
Expand Down
25 changes: 6 additions & 19 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@ name = "fxtiktok-rewrite"
compatibility_date = "2023-01-01"
main = "src/index.ts"

vars = { WORKER_ENV = "production" }
vars = { WORKER_ENV = "production", BASE_URL = "https://fxtiktok-rewrite.dargy.workers.dev" }

[env.local]
# these will be used only when --env=local
vars = { WORKER_ENV = "local" }

# [vars]
# MY_VARIABLE = "production_value"

# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
[env.staging]
name = "fxtiktok-rewrite-dev"
vars = { WORKER_ENV = "staging", BASE_URL = "https://fxtiktok-rewrite-dev.dargy.workers.dev" }

# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "my-bucket"

# [[d1_databases]]
# binding = "DB"
# database_name = "my-database"
# database_id = ""
[env.local]
vars = { WORKER_ENV = "local", BASE_URL = "http://127.0.0.1:8787" }

0 comments on commit e42dab9

Please sign in to comment.