只获取mediaDevices.getDisplayMedia的音频如何做?

huangapple go评论70阅读模式
英文:

How do I capture only Audio from mediaDevices.getDisplayMedia?

问题

const desktopStream = await navigator.mediaDevices.getDisplayMedia({
      preferCurrentTab: true,
      video: true,
      audio: true,
    });
..
const rec = new MediaRecorder(stream, {
      mimeType: 'audio/webm; codecs=opus',
    });
...
const blob = new Blob(blobs, { type: 'audio/mpeg-3; codecs=opus' });

I'm trying to capture the mic and desktop audio to create an mp3 file. But the files that come are very large in size, so I'm trying to reduce the bloat.

Turning the video boolean false makes the whole thing non-operational.

英文:
const desktopStream = await navigator.mediaDevices.getDisplayMedia({
      preferCurrentTab: true,
      video: true,
      audio: true,
    });
..
const rec = new MediaRecorder(stream, {
      mimeType: 'audio/webm; codecs=opus',
    });
...
const blob = new Blob(blobs, { type: 'audio/mpeg-3; codecs=opus' });

I'm trying to capture the mic and desktop audio to create an mp3 file. But the files that come are very large in size, so I'm trying to reduce the bloat.

Turning the video boolean false makes the whole thing non-operational.

答案1

得分: 1

Never mind. I just get all the audio tracks from the stream and put it into a brand new stream.

const stream = new MediaStream(tracks);

console.log('Stream', stream);

const audioStream = new MediaStream();
for (const track of stream.getAudioTracks()) {
    audioStream.addTrack(track);
}

for (const track of stream.getVideoTracks()) {
    track.stop();
}

// eventually converted -> mpeg -> mp3
const rec = new MediaRecorder(audioStream, {
    mimeType: 'audio/webm; codecs=opus',
});

Files that used to be 10-40MB are now like 1-3MBs.

英文:

Never mind. I just get all the audio tracks from the stream and put it into a brand new stream.

const stream = new MediaStream(tracks);

console.log('Stream', stream);

const audioStream = new MediaStream();
for (const track of stream.getAudioTracks()) {
    audioStream.addTrack(track);
}

for (const track of stream.getVideoTracks()) {
    track.stop();
}

// eventually converted -> mpeg -> mp3
const rec = new MediaRecorder(audioStream, {
    mimeType: 'audio/webm; codecs=opus',
});


Files that used to be 10-40MB are now like 1-3MBs.

huangapple
  • 本文由 发表于 2023年3月1日 08:39:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75598622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定