Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update MediaRecorder preferences #99

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions components/record-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,23 +295,15 @@ const RecordPage: React.FC<NoProps> = () => {
logger('start recording');
try {
setStartRecordTime((new Date()).valueOf());
const preferredOptions = { mimeType: 'video/webm;codecs=vp9' };
const backupOptions = { mimeType: 'video/webm;codecs=vp8,opus' };
const lastResortOptions = { mimeType: 'video/mp4;codecs=avc1' };
let options = preferredOptions;
/*
* MediaRecorder.isTypeSupported is not a thing in safari,
* good thing safari supports the preferredOptions
*/
if (typeof MediaRecorder.isTypeSupported === 'function') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it okay to get rid of this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!MediaRecorder.isTypeSupported(preferredOptions.mimeType)) {
options = backupOptions;
if (!MediaRecorder.isTypeSupported(options.mimeType)) {
options = lastResortOptions;
}
}
}

const videoBitsPerSecond = 5000000; // Double the default quality from 2.5Mbps to 5Mbps
const audioBitsPerSecond = 128000; // 128kbps
const MIME_TYPE_STACK = ['video/mp4;codecs=avc1', 'video/webm;codecs=vp9', 'video/webm;codecs=vp8,opus'];
const supportedMimeType = MIME_TYPE_STACK.find((mimeType) =>
MediaRecorder.isTypeSupported(mimeType)
);

if (!supportedMimeType) throw new Error('Cannot find a supported mimeType');
const options = { videoBitsPerSecond, audioBitsPerSecond, mimeType: supportedMimeType };
const stream = streamRef.current;
if (!stream) throw new Error('Cannot record without a stream');
recorderRef.current = new MediaRecorder(stream, options);
Expand Down