参考了一下 StackOverflow https://stackoverflow.com/questions/49140159/extracting-audio-from-a-video-file/49182456#49182456 以下代码可以提取出音频,但是音频体积却比源视频大的问题却没解决。 或许我该用 offlineAudioContext.startRendering() 这个方法试试?
var videoSrc = 'http://otof18y9e.bkt.clouddn.com/frag_bunny.mp4'
function startSelect(){
var audioContext = new OfflineAudioContext(2, 44100 * 100, 44100);
// var audioContext = new window.AudioContext()
var videoFileAsBuffer = new Promise(requestVideo)
videoFileAsBuffer.then(function (data) {
console.log(data)
audioContext.decodeAudioData(data).then(function (decodedAudioData) {
mySoundBuffer = decodedAudioData
soundSource = audioContext.createBufferSource()
soundSource.buffer = mySoundBuffer
soundSource.connect(audioContext.destination)
// soundSource.start()
console.log(mySoundBuffer.length)
});
})
}
function requestVideo(resolve){
var xhr = new XMLHttpRequest()
xhr.open('GET', videoSrc, true)
xhr.responseType = 'arraybuffer' // 'blob'
xhr.onload = function(e) {
var binaryData = this.response
// console.log(binaryData)
resolve(binaryData)
}
xhr.send()
}
后来我知道了,音频抽取后经过了解码,成了未压缩的PCM数据,体积当然很大。