vue 流式加载mp4文件

const props = defineProps<{
assetURL: string
}>()

const rangeVideo = () => {
const totalSize = 112702286
const chunkSize = 1000000
const numChunks = Math.ceil(totalSize / chunkSize)
let index = 0
const mimeCodec = ‘video/mp4; codecs=“avc1.42E01E, mp4a.40.2”’

if (‘MediaSource’ in window && MediaSource.isTypeSupported(mimeCodec)) {
let mediaSource = new MediaSource()
video.value.src = URL.createObjectURL(mediaSource)
mediaSource.addEventListener(‘sourceopen’, sourceOpen)
} else {
console.error('Unsupported MIME type or codec: ', mimeCodec)
}

function sourceOpen(e) {
let mediaSource = e.target
let sourceBuffer = mediaSource.addSourceBuffer(mimeCodec)
const send = async () => {
if (index >= numChunks) {
sourceBuffer.addEventListener(‘updateend’, function (_) {
mediaSource.endOfStream()
})
} else {
const start = index * chunkSize
const end = Math.min(start + chunkSize - 1, totalSize - 1)
fetch(‘/video-api/’ + props.assetURL, {
headers: {
Range: bytes=${start}-${end},
responseType: ‘arraybuffer’
}
}).then(async (response) => {
const res = await response.arrayBuffer()
index++
sourceBuffer.appendBuffer(res)
send()
// video.value.play()
})
}
}
send()
}
}

const getRef = () => {
return video
}

defineExpose({ getRef })

onMounted(() => {
rangeVideo()
})

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值