我正在创建一个MediaStream对象,并使用captureStream()函数从画布添加视频轨道。这很好。
但是我试图将音频作为单独的音轨添加到视频元素中。我似乎找不到从html视频元素中获取AudioTrack对象的方法。
Chrome目前不支持HTMLMediaElement.audioTracks。根据mozilla开发者网站,我应该能够使用HTMLMediaElement.captureStream()来返回一个MediaStream对象,我应该可以从中检索单独的轨道,但我只是得到'captureStream不是函数'错误。
也许我错过了一些非常明显的东西,但我非常感谢你提供的任何帮助。
以下是我目前的代码:
var stream = new MediaStream();
//Works fine for adding video source
var videotracks = myCanvas.captureStream().getTracks();
var videostream = videotracks[0];
stream.addTrack(videostream);
//Currently not supported in Chrome
var audiotracks = myVid.audioTracks;
var audiostream = audiotracks[0];
stream.addTrack(audiostream);
答案 0 :(得分:4)
以跨浏览器的方式从视频元素中获取音频流:
AudioContext API createMediaStreamDestination + createMediaElementSource。
// if all you need is the audio, then you should even probably load your video in an Audio element
var vid = document.createElement('video');
vid.onloadedmetadata = generateAudioStream;
vid.crossOrigin = 'anonymous';
vid.src = 'https://dl.dropboxusercontent.com/s/bch2j17v6ny4ako/movie720p.mp4';
function generateAudioStream() {
var audioCtx = new AudioContext();
// create a stream from our AudioContext
var dest = audioCtx.createMediaStreamDestination();
// connect our video element's output to the stream
var sourceNode = audioCtx.createMediaElementSource(this);
sourceNode.connect(dest)
// start the video
this.play();
// your audio stream
doSomethingWith(dest.stream)
}
function doSomethingWith(audioStream) {
// the audio element that will be shown in the doc
var output = new Audio();
output.srcObject = audioStream;
output.controls = true;
output.play();
document.body.appendChild(output);
}
要向画布流添加音频: