Skip to content

Commit

Permalink
fix: šŸ›append 15s instead of X GOPs
Browse files Browse the repository at this point in the history
  • Loading branch information
Banou26 committed Mar 10, 2024
1 parent 288b84e commit 5fa4b07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ video7.mkv
video8.mkv
video9.mkv
video10.mkv
video11.mkv
video11.mkv
video12.mkv
22 changes: 14 additions & 8 deletions src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Chunk = {

const BACKPRESSURE_STREAM_ENABLED = !navigator.userAgent.includes("Firefox")
const BUFFER_SIZE = 2_500_000
const VIDEO_URL = '../video10.mkv'
const VIDEO_URL = '../video12.mkv'
// const VIDEO_URL = '../spidey.mkv'

export default async function saveFile(plaintext: ArrayBuffer, fileName: string, fileType: string) {
Expand Down Expand Up @@ -87,9 +87,9 @@ fetch(VIDEO_URL, { headers: { Range: `bytes=0-1` } })
bufferSize: BUFFER_SIZE,
length: contentLength,
getStream: async (offset, size) => {
if (slow && size !== BUFFER_SIZE) {
await new Promise(resolve => setTimeout(resolve, 5000))
}
// if (slow && size !== BUFFER_SIZE) {
// await new Promise(resolve => setTimeout(resolve, 5000))
// }

return fetch(
VIDEO_URL,
Expand Down Expand Up @@ -214,7 +214,7 @@ fetch(VIDEO_URL, { headers: { Range: `bytes=0-1` } })
let chunks: Chunk[] = []

const PREVIOUS_BUFFER_COUNT = 1
const BUFFER_COUNT = 3
const NEEDED_TIME_IN_SECONDS = 15

await appendBuffer(headerChunk.buffer)

Expand All @@ -236,8 +236,14 @@ fetch(VIDEO_URL, { headers: { Range: `bytes=0-1` } })
const currentChunkIndex = chunks.findIndex(({ pts, duration }) => pts <= currentTime && pts + duration >= currentTime)
const sliceIndex = Math.max(0, currentChunkIndex - PREVIOUS_BUFFER_COUNT)

for (let i = 0; i < sliceIndex + BUFFER_COUNT; i++) {
if (chunks[i] || reachedEnd) continue
const getLastChunkEndTime = () => {
const lastChunk = chunks.at(-1)
if (!lastChunk) return 0
return lastChunk.pts + lastChunk.duration
}

// pull and append buffers up until the needed time
while (getLastChunkEndTime() < currentTime + NEEDED_TIME_IN_SECONDS){
const chunk = await pull()
await appendBuffer(chunk.buffer)
}
Expand Down Expand Up @@ -316,7 +322,7 @@ fetch(VIDEO_URL, { headers: { Range: `bytes=0-1` } })

setTimeout(async () => {
video.playbackRate = 1
video.currentTime = 1410
video.currentTime = 290

// slow = true
// video.currentTime = 400
Expand Down

0 comments on commit 5fa4b07

Please sign in to comment.