Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make a fake GOP when video dont have B/P frame #85

Merged
merged 1 commit into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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