控制Firefox中视频/音频流的音量增益

时间:2016-01-25 10:45:53

标签: firefox audio video-streaming web-mediarecorder

我正在尝试使用MediaRecorder API for Firefox录制视频/音频文件。

当我使用网络音频API创建节点时(来源 - >收益 - >目的地)

录制文件的输出仅为音频,因为来自目标节点的返回流是仅参考此文档的音频流 https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamAudioDestinationNode

有关在目标输出中获取音频/视频流以记录音频/视频而非仅音频的任何建议。

    var mediastream;

    var ctx = new AudioContext();

    var mediaStreamSource = ctx.createMediaStreamSource(mediaStream); 

    var destination = ctx.createMediaStreamDestination();

    ObjectStore.VolumeGainNode = ctx.createGain();
    ObjectStore.VolumeGainNode.gain.value = 0.5;

    mediaStreamSource.connect(ObjectStore.VolumeGainNode);

    ObjectStore.VolumeGainNode.connect(destination);

    mediaStream = destination.stream;

1 个答案:

答案 0 :(得分:3)

您需要一个由gum视频轨道和增益修改音频轨道组成的流。

遵循标准,Firefox允许您使用stream.addTrackstream.removeTrack修改流中的曲目,并使用new MediaStream([tracks])从曲目中创建新流。

这可让您通过将gum音频轨道替换为增益操纵的音轨来解决您的问题:

var constraints = { video: true, audio: true };

var start = () => navigator.mediaDevices.getUserMedia(constraints)
  .then(stream => modifyGain(video.srcObject = stream, 0.5))
  .catch(e => console.error(e));

var modifyGain = (stream, gainValue) => {
    var audioTrack = stream.getAudioTracks()[0];
    var ctx = new AudioContext();
    var src = ctx.createMediaStreamSource(new MediaStream([audioTrack]));
    var dst = ctx.createMediaStreamDestination();
    var gainNode = ctx.createGain();
    gainNode.gain.value = gainValue;
    [src, gainNode, dst].reduce((a, b) => a && a.connect(b));
    stream.removeTrack(audioTrack);
    stream.addTrack(dst.stream.getAudioTracks()[0]);
};

这是小提琴(Firefox 44或更新版):https://jsfiddle.net/7wd2z8rz/

再次使用MediaRecorder:https://jsfiddle.net/j33xmkcq/