gpt4 book ai didi

javascript - 有没有办法在同一个视频中分别播放音频和视频

转载 作者:行者123 更新时间:2023-12-03 20:28:25 26 4
gpt4 key购买 nike

<video id="video1" width="320" height="176" controls="controls">
<source src="mov_bbb.mp4" type="video/mp4">

<source src="mov_bbb.m4a" type="video/m4a">

</video>


我有一段无声视频,想在视频播放中添加他的音频文件

最佳答案

MediaSource API让我们能够做到这一点。
请注意,要使其正常工作,您需要以与该 API 兼容的方式准备媒体 Assets 。通读this MDN article这很好地解释了如何做到这一点。
一旦你有了你的 Assets ,剩下的就很简单了:

(async() => {

const fetching = Promise.all( [
// the video "only" file
fetchData( "https://dl.dropboxusercontent.com/s/u9ycdfwy8fig4dl/bbb_video.mp4" ),
// the audio "only" file
fetchData( "https://dl.dropboxusercontent.com/s/rj4dh32vxwi1iv5/bbb_audio.mp4" )
] );

const video_mime = "video/mp4; codecs=avc1.64000c";
const audio_mime = "audio/mp4; codecs=mp4a.40.2";
if(
!MediaSource.isTypeSupported( video_mime ) ||
!MediaSource.isTypeSupported( audio_mime )
) {
throw "unsupported codecs";
}

const source = new MediaSource();
document.querySelector( "video" ).src = URL.createObjectURL( source );
await waitForEvent( source, "sourceopen" );

const video_buffer = source.addSourceBuffer( video_mime );
const audio_buffer = source.addSourceBuffer( audio_mime );
video_buffer.mode = audio_buffer.mode = "sequence";

const [ video_data, audio_data ] = await fetching;
// There is a 'variable' limit as to how much
// data we can append in on go, 10MB seems quite safe
const chunk_size = 10 * 1024 * 1024;
let i = 0;
while (
i < video_data.length &&
i < audio_data.length
) {
const next_i = i + chunk_size;
const events = Promise.all( [
waitForEvent( video_buffer, "updateend" ),
waitForEvent( audio_buffer, "updateend" )
] );
video_buffer.appendBuffer( video_data.subarray( i, next_i ) );
audio_buffer.appendBuffer( audio_data.subarray( i, next_i ) );
await events;
i = next_i;
}

source.endOfStream();

})().catch( console.error );

function fetchData( url ) {
return fetch( url )
.then( (resp) => resp.ok && resp.arrayBuffer() )
// we return an Uint8 view to be able to do a zero-cost subarray()
.then( (buf) => new Uint8Array( buf ) );
}
function waitForEvent( target, event_name ) {
return new Promise( (res) => {
target.addEventListener( event_name, res, { once: true } );
} );
}
<video controls></video>

关于javascript - 有没有办法在同一个视频中分别播放音频和视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55888717/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com