open-audiovisual
Search GitHub

oav-p5 reference · Recording

startRecording()#

functionoav-p5
startRecording([o])

Also: OAVp5.record.start

Records the canvas and the sound your sketch makes into one video file, and returns a promise that says whether the recording began.

Start it from a key or a click: if (key === 'r') startRecording();. What goes into the video is the canvas alone, at its own pixel size (a retina screen doubles it), and everything your sketch sounds like: the instrument of startSound(), the drum machine, the pads, the microphone while it is on, and p5.sound 1.x, mixed into one track while the speakers play as before. The 🎹, the drum grid and the cards are part of the page, not of the canvas, so they are never in the video. A sound that comes on during the take joins it.

The file is MP4 (H.264 + AAC, which Instagram, YouTube and every editor take) where the browser can write it, and WebM where not; a card says Instagram will not take a WebM. It is named oav-20261015-183005.mp4, with name in the middle when you give one. When the take is done it is downloaded, and on a phone that can share files a Share button appears on the card. After stopRecording(), or when seconds has run out, recordingSaved(info) is called with the details. Without seconds a take stops by itself at the longest length (300 seconds unless the reclimit option says otherwise).

Without bitrate the quality follows the canvas: 0.16 bits per pixel per frame, kept between 4 and 16 megabits a second (24 above 30 frames a second). Calling startRecording() while a take is running starts nothing new. After every take a card says what was recorded and, when something looks wrong, what and why: nothing reached the sound track although a sound was on, WebM, a file that is almost empty, a black video of a canvas that was not black.

Before you call it

A browser that has MediaRecorder: where it is missing, a card says so and the promise resolves to false. The picture is recorded at once, but the page can only make sound after a click or key press on it, so a take that begins before the first one has nothing to hear at first. A canvas that shows an image from another site (no CORS) cannot be recorded, and a card says so.

Parameters

NameTypeDescription
oNumber | ObjectA number is the length in seconds: startRecording(10) stops by itself after ten. An object can hold seconds (the same, never more than the longest take, 300 by default), fps (frames a second, at most 60, 30 by default), mic (false keeps the microphone out of the video), name (a word for the file name), canvas (the canvas to record: a <canvas>, a p5 canvas or a createGraphics() buffer; the biggest p5 canvas on the page by default) and bitrate (bits a second). Leave it out to record until stopRecording(). optional

Returns

Promise<Boolean> Resolves to true once the recording is running (or when one already is). Resolves to false when the loading option record is off, when there is no canvas to record, when this browser has no MediaRecorder, or when it could not start; a card says why.

Notes

  • In the 🎹 panel, ⏺ starts and stops a take with no code, and 📷 saves the canvas as a PNG (see OAVp5.record.shot()). While a take runs the 🎹 button shows a red dot and the time. The loading options record (on, nomic or off), recfps, reclimit and title set the defaults.
  • The microphone is in the take while it is on: wear headphones, or the speakers are recorded twice. p5.sound for p5 2.x connects each sound straight to the speakers, so it cannot be recorded (a card says so); use p5 1.11, or startSound() and startDrums(). Sound that your sketch makes with its own Tone.js or Web Audio code is not recorded either.
  • Whatever you draw on the canvas is in the video, so show hints only while recordingIsOn is false.

Examples

jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
}

function keyPressed() {
  if (key === 'r') startRecording(5);   // five seconds, then it stops and downloads
}

function draw() {
  background(20);
  circle(200 + sin(frameCount * 0.05) * 120, 150, 50);
  if (!recordingIsOn) {
    fill(255);
    text('press r to record 5 seconds', 20, 30);   // not drawn while recording
  }
}
Press r: after five seconds an MP4 of the moving circle is downloaded, without the hint text.
jsOpenProcessing ↗
function setup() {
  createCanvas(400, 300);
  startDrums('four on floor', 110);
  startSound('synth/pluck');
}

function keyPressed() {
  if (key === 'r') {
    if (recordingIsOn) stopRecording();
    else startRecording({ name: 'my piece', fps: 60 });
  }
  if (key === 'a') midi.play(60, 0.8);
}

function kickDetected(level) {
  background(255 * level);
}

function draw() {
  background(20);
  if (!recordingIsOn) {
    fill(255);
    text('click once, then: a plays a note, r starts and stops a recording', 20, 30);
  }
}
Click once so the browser allows sound, then press r to start and r again to stop: the file holds the picture, the drums and the notes you played.
jsOpenProcessing ↗
let layer;

function setup() {
  createCanvas(400, 300);
  layer = createGraphics(200, 200);
}

function keyPressed() {
  if (key === 'g') startRecording({ canvas: layer, seconds: 3 });   // record the buffer, not the main canvas
}

function draw() {
  background(20);
  layer.background(40);
  layer.circle(100 + sin(frameCount * 0.1) * 60, 100, 40);
  image(layer, 100, 50);
  fill(255);
  text('press g to record the small square only (3 s)', 20, 30);
}
Press g: the video is 200 by 200, the buffer alone, not the 400 by 300 canvas.

Related

  • stopRecording() Stops the recording; the video is finished, downloaded, and handed to recordingSaved(info) and to the returned promise.
  • recordingIsOn Whether a recording is running right now.
  • recordingSaved() An event you define: it is called when a recording has been saved and checked, with an info object about the file.
  • OAVp5.record The object behind the recording names: the state, the time, the last file, the formats this browser can write and the screenshot function.
  • OAVp5.record.shot() Saves the canvas alone as a PNG picture (not the 🎹, not the cards) and downloads it.
  • startSound() Makes the keys you play heard through an instrument you name, and returns a promise that says whether sound is on.
  • startDrums() Starts a drum machine: a 16-step grid on the page that plays a preset, your own grid or one line of rhythm, and counts the bars for your sketch.
  • drawRhythm() Draws the rhythm on the canvas as a grid, a circle or a spiral, with a playhead and the hits lighting up, so a recording has it too.

p5/oav-p5.js, line 2335

↑ Back to top · All names