参考了一下 StackOverflow https://stackoverflow.com/questions/49140159/extracting-audio-from-a-video-file/49182456#49182456
以下代码可以提取出音频,但是音频体积却比源视频大的问题却没解决。
或许我该用 offlineAudioContext.startRendering() 这个方法试试?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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()
}