Skip to content

Commit

Permalink
fix: make a fake GOP when video dont have B/P frame (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyuhen authored Apr 22, 2019
1 parent bcd054a commit 116c19a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getIntervalArray,
getVideoSamplesInterval,
getAudioSamplesInterval,
getNextVideoSamplesInterval,
Expand Down Expand Up @@ -42,4 +43,29 @@ describe('getSamplesInterval', () => {
timeInterVal: [56056, 86086],
})
})

it('should get the sample interval array when video has B/P frame', () => {
const mockStssBox = {
samples: [
{
sampleNumber: 1,
},
{
sampleNumber: 251,
},
{
sampleNumber: 501,
},
],
}
expect(getIntervalArray(mockStssBox)).toEqual([0, 250, 500])
})

it('should get the sample interval array when video has B/P frame', () => {
const mockStszBox = {
samples: [],
}
mockStszBox.samples.length = 21
expect(getIntervalArray(undefined, mockStszBox)).toEqual([0, 5, 10, 15, 20])
})
})
19 changes: 17 additions & 2 deletions packages/griffith-mp4/src/mp4/utils/getSamplesInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function getVideoSamplesInterval(mp4BoxTree, time = 0) {
const stszBox = findBox(mp4BoxTree, 'videoStsz')
const duration = getDuration(sttsBox, stszBox.samples.length)

const intervalArray = stssBox.samples.map(sample => sample.sampleNumber - 1)
const intervalArray = getIntervalArray(stssBox, stszBox)
const timeInterval = intervalArray.map(interval =>
getDuration(sttsBox, interval)
)
Expand Down Expand Up @@ -87,12 +87,13 @@ export function getAudioSamplesInterval(mp4BoxTree, videoInterval) {

export function getNextVideoSamplesInterval(mp4BoxTree, sample) {
const stssBox = cloneDeep(findBox(mp4BoxTree, 'videoStss'))
const intervalArray = stssBox.samples.map(sample => sample.sampleNumber - 1)
const sttsBox = cloneDeep(findBox(mp4BoxTree, 'videoStts'))
const stszBox = findBox(mp4BoxTree, 'videoStsz')
const sampleCount = stszBox.samples.length
const duration = getDuration(sttsBox, sampleCount)

const intervalArray = getIntervalArray(stssBox, stszBox)

const timeInterval = intervalArray.map(interval =>
getDuration(sttsBox, interval)
)
Expand All @@ -114,3 +115,17 @@ export function getNextVideoSamplesInterval(mp4BoxTree, sample) {
}
return result
}

export function getIntervalArray(stssBox, stszBox) {
let intervalArray = []
if (stssBox) {
intervalArray = stssBox.samples.map(sample => sample.sampleNumber - 1)
} else {
// make a fake GOP when video dont have B/P frame
for (let i = 0; i <= Math.floor(stszBox.samples.length / 5); i++) {
intervalArray.push(i * 5)
}
}

return intervalArray
}
2 changes: 2 additions & 0 deletions packages/griffith-mp4/src/mp4/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import getFragmentPosition from './getFragmentPosition'
import getBufferStart from './getBufferStart'
import {getVideoSamples, getAudioSamples} from './getSamples'
import {
getIntervalArray,
getAudioSamplesInterval,
getVideoSamplesInterval,
getNextVideoSamplesInterval,
Expand All @@ -15,6 +16,7 @@ import getDuration from './getDuration'
export {
findBox,
getDuration,
getIntervalArray,
getSamplesOffset,
getPerChunkArray,
getFragmentPosition,
Expand Down

0 comments on commit 116c19a

Please sign in to comment.