Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ $batchDeleted = $client->deleteRecordingBatch(['first-id', 'second-id']);

Deletion returns `DeleteRecordingSuccessResponse` when accepted. An empty `deleteRecordingBatch` list returns success without sending an HTTP request. Recording methods that send a request return the existing `ApiErrorResponse` on API errors and propagate PSR-18 transport failures.

Optional settings use the API defaults. Recordings have no audio. Creation returns an ID, status URL, and recording URL. The recording URL can be embedded or requested directly; the request waits until the recording is ready.
Optional settings use the API defaults. Creation returns an ID, status URL, and recording URL. The recording URL can be embedded or requested directly; the request waits until the recording is ready.

For an MP4 or WebM soundtrack, import `RecordingAssetAudio` or `RecordingUrlAudio` from `HtmlCssToImage\Recording`. Pass `audio: new RecordingAssetAudio(assetId: "YOUR_ASSET_ID", delayMs: 500)` or `audio: new RecordingUrlAudio(url: "https://cdn.example.com/audio/intro.mp3")` to either request constructor. [Audio sources and playback options](https://docs.htmlcsstoimage.com/parameters/audio/).

Checking status is optional. Use `fetchRecordingStatus` when a workflow needs lifecycle details:

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.1
1.1.0
25 changes: 25 additions & 0 deletions src/Recording/RecordingAssetAudio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace HtmlCssToImage\Recording;

/** A soundtrack from the Asset Library.
* @see https://docs.htmlcsstoimage.com/parameters/audio/
* @api
*/
final readonly class RecordingAssetAudio extends RecordingAudio
{
/** @param string $assetId The ID of a completed audio upload in the recording organization's Asset Library. Private files are supported.
* @see RecordingAudio::__construct() for playback options.
*/
public function __construct(
public string $assetId,
?int $sourceStartMs = null,
?int $delayMs = null,
?RecordingAudioEndBehavior $whenAudioEnds = null,
?bool $errorOnSourceInvalid = null,
) {
parent::__construct($sourceStartMs, $delayMs, $whenAudioEnds, $errorOnSourceInvalid);
}
}
27 changes: 27 additions & 0 deletions src/Recording/RecordingAudio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace HtmlCssToImage\Recording;

/** A single MP4 or WebM soundtrack. Choose RecordingAssetAudio or RecordingUrlAudio.
* @see https://docs.htmlcsstoimage.com/parameters/audio/
* @api
*/
abstract readonly class RecordingAudio
{
/**
* @param int|null $sourceStartMs Offset into the source file in milliseconds. Defaults to 0.
* @param int|null $delayMs Delay from the start of the finished video in milliseconds. Defaults to 0.
* @param RecordingAudioEndBehavior|null $whenAudioEnds Playback when the track ends. Defaults to Silence.
* @param bool|null $errorOnSourceInvalid Defaults to true. Set false to return the full silent video
* if the track cannot play at render time.
*/
protected function __construct(
public ?int $sourceStartMs,
public ?int $delayMs,
public ?RecordingAudioEndBehavior $whenAudioEnds,
public ?bool $errorOnSourceInvalid,
) {
}
}
18 changes: 18 additions & 0 deletions src/Recording/RecordingAudioEndBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace HtmlCssToImage\Recording;

/** Playback when the track ends before the video.
* @api
*/
enum RecordingAudioEndBehavior: string
{
/** Play the rest of the video silently (default). */
case Silence = 'silence';
/** Repeat the track from the selected source offset. */
case Loop = 'loop';
/** End the video with the track. */
case TrimVideo = 'trim_video';
}
2 changes: 1 addition & 1 deletion src/Recording/RecordingFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace HtmlCssToImage\Recording;

/** Recording media format. Recordings do not include audio.
/** Recording media format. MP4 and WebM support an optional soundtrack.
* @api
*/
enum RecordingFormat: string
Expand Down
25 changes: 25 additions & 0 deletions src/Recording/RecordingUrlAudio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace HtmlCssToImage\Recording;

/** A soundtrack from a public URL.
* @see https://docs.htmlcsstoimage.com/parameters/audio/
* @api
*/
final readonly class RecordingUrlAudio extends RecordingAudio
{
/** @param string $url A public HTTP or HTTPS URL serving the audio file at render time.
* @see RecordingAudio::__construct() for playback options.
*/
public function __construct(
public string $url,
?int $sourceStartMs = null,
?int $delayMs = null,
?RecordingAudioEndBehavior $whenAudioEnds = null,
?bool $errorOnSourceInvalid = null,
) {
parent::__construct($sourceStartMs, $delayMs, $whenAudioEnds, $errorOnSourceInvalid);
}
}
6 changes: 5 additions & 1 deletion src/Request/BaseCreateRecordingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace HtmlCssToImage\Request;

use HtmlCssToImage\Recording\RecordingAction;
use HtmlCssToImage\Recording\RecordingAudio;
use HtmlCssToImage\Recording\RecordingDurationMode;
use HtmlCssToImage\Recording\RecordingOutputOptions;
use HtmlCssToImage\Recording\RecordingTrigger;
Expand Down Expand Up @@ -56,7 +57,9 @@
* @param list<RecordingAction>|null $actions Ordered interactions. Leave null or empty to record the
* page without interactions. See https://docs.htmlcsstoimage.com/parameters/actions/
* @param RecordingOutputOptions|null $output Output format, frame rate, and scale. Defaults to MP4
* at 30 fps without audio. See https://docs.htmlcsstoimage.com/parameters/output/
* at 30 fps. See https://docs.htmlcsstoimage.com/parameters/output/
* @param RecordingAudio|null $audio Optional MP4 or WebM soundtrack from an asset ID or public URL.
* See https://docs.htmlcsstoimage.com/parameters/audio/
* @param RecordingDurationMode|null $durationMode How duration_ms limits the timeline. Defaults to
* at_most. See https://docs.htmlcsstoimage.com/parameters/duration_mode/
* @param bool|null $deferRender Wait to render until media or storage is requested. Defaults to
Expand Down Expand Up @@ -89,6 +92,7 @@ public function __construct(
public ?RecordingDurationMode $durationMode = null,
public ?bool $deferRender = null,
public ?bool $skipScrollPreload = null,
public ?RecordingAudio $audio = null,
) {
}
}
7 changes: 6 additions & 1 deletion src/Request/CreateHtmlCssRecordingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace HtmlCssToImage\Request;

use HtmlCssToImage\Recording\RecordingAction;
use HtmlCssToImage\Recording\RecordingAudio;
use HtmlCssToImage\Recording\RecordingDurationMode;
use HtmlCssToImage\Recording\RecordingOutputOptions;
use HtmlCssToImage\Recording\RecordingTrigger;
Expand Down Expand Up @@ -57,7 +58,9 @@
* @param list<RecordingAction>|null $actions Ordered interactions. Leave null or empty to record the
* page without interactions. See https://docs.htmlcsstoimage.com/parameters/actions/
* @param RecordingOutputOptions|null $output Output format, frame rate, and scale. Defaults to MP4
* at 30 fps without audio. See https://docs.htmlcsstoimage.com/parameters/output/
* at 30 fps. See https://docs.htmlcsstoimage.com/parameters/output/
* @param RecordingAudio|null $audio Optional MP4 or WebM soundtrack from an asset ID or public URL.
* See https://docs.htmlcsstoimage.com/parameters/audio/
* @param RecordingDurationMode|null $durationMode How duration_ms limits the timeline. Defaults to
* at_most. See https://docs.htmlcsstoimage.com/parameters/duration_mode/
* @param bool|null $deferRender Wait to render until media or storage is requested. Defaults to
Expand Down Expand Up @@ -92,6 +95,7 @@ public function __construct(
?RecordingDurationMode $durationMode = null,
?bool $deferRender = null,
?bool $skipScrollPreload = null,
?RecordingAudio $audio = null,
) {
parent::__construct(
durationMs: $durationMs,
Expand All @@ -116,6 +120,7 @@ public function __construct(
durationMode: $durationMode,
deferRender: $deferRender,
skipScrollPreload: $skipScrollPreload,
audio: $audio,
);
}
}
7 changes: 6 additions & 1 deletion src/Request/CreateUrlRecordingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace HtmlCssToImage\Request;

use HtmlCssToImage\Recording\RecordingAction;
use HtmlCssToImage\Recording\RecordingAudio;
use HtmlCssToImage\Recording\RecordingDurationMode;
use HtmlCssToImage\Recording\RecordingOutputOptions;
use HtmlCssToImage\Recording\RecordingTrigger;
Expand Down Expand Up @@ -67,7 +68,9 @@
* @param list<RecordingAction>|null $actions Ordered interactions. Leave null or empty to record the
* page without interactions. See https://docs.htmlcsstoimage.com/parameters/actions/
* @param RecordingOutputOptions|null $output Output format, frame rate, and scale. Defaults to MP4
* at 30 fps without audio. See https://docs.htmlcsstoimage.com/parameters/output/
* at 30 fps. See https://docs.htmlcsstoimage.com/parameters/output/
* @param RecordingAudio|null $audio Optional MP4 or WebM soundtrack from an asset ID or public URL.
* See https://docs.htmlcsstoimage.com/parameters/audio/
* @param RecordingDurationMode|null $durationMode How duration_ms limits the timeline. Defaults to
* at_most. See https://docs.htmlcsstoimage.com/parameters/duration_mode/
* @param bool|null $deferRender Wait to render until media or storage is requested. Defaults to
Expand Down Expand Up @@ -106,6 +109,7 @@ public function __construct(
?RecordingDurationMode $durationMode = null,
?bool $deferRender = null,
?bool $skipScrollPreload = null,
?RecordingAudio $audio = null,
) {
parent::__construct(
durationMs: $durationMs,
Expand All @@ -130,6 +134,7 @@ public function __construct(
durationMode: $durationMode,
deferRender: $deferRender,
skipScrollPreload: $skipScrollPreload,
audio: $audio,
);
}
}
14 changes: 13 additions & 1 deletion tests/RecordingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use HtmlCssToImage\HtmlCssToImageClientInterface;
use HtmlCssToImage\Recording\CoordinatesScrollTarget;
use HtmlCssToImage\Recording\PositionScrollTarget;
use HtmlCssToImage\Recording\RecordingAssetAudio;
use HtmlCssToImage\Recording\RecordingAudioEndBehavior;
use HtmlCssToImage\Recording\RecordingUrlAudio;
use HtmlCssToImage\Recording\RecordingClickAction;
use HtmlCssToImage\Recording\RecordingDurationMode;
use HtmlCssToImage\Recording\RecordingElementVisibleTrigger;
Expand Down Expand Up @@ -49,6 +52,7 @@ public function testHtmlCreationMapsFontsAndUsesExistingAuthentication(): void
$request = new CreateHtmlCssRecordingRequest(
html: '<h1>Hello</h1>', durationMs: 1000,
googleFonts: [' Roboto ', 'Open Sans', 'Open Sans'],
audio: new RecordingAssetAudio(assetId: 'asset_1'),
);
$result = $client->createRecording($request);
self::assertInstanceOf(CreateRecordingSuccessResponse::class, $result);
Expand All @@ -62,7 +66,7 @@ public function testHtmlCreationMapsFontsAndUsesExistingAuthentication(): void
self::assertSame('Basic aWQ6a2V5', $sent->getHeaderLine('Authorization'));
self::assertSame('application/json', $sent->getHeaderLine('Content-Type'));
self::assertSame([
'duration_ms' => 1000, 'html' => '<h1>Hello</h1>',
'duration_ms' => 1000, 'audio' => ['asset_id' => 'asset_1'], 'html' => '<h1>Hello</h1>',
'google_fonts' => 'Roboto|Open+Sans',
], json_decode((string) $sent->getBody(), true, flags: JSON_THROW_ON_ERROR));
self::assertSame([' Roboto ', 'Open Sans', 'Open Sans'], $request->googleFonts);
Expand All @@ -87,6 +91,10 @@ public function testUrlOptionsAndNestedTimelinePreserveEmptyBranches(): void
skipScrollPreload: true, durationMode: RecordingDurationMode::AtLeast,
requestOverrides: [new RequestOverride(resourceTypes: [RequestOverrideResourceType::Script])],
output: new RecordingOutputOptions(format: RecordingFormat::WebM, fps: 60, scale: 0.5),
audio: new RecordingUrlAudio(
url: 'https://cdn.example.com/audio/intro.mp3', sourceStartMs: 0, delayMs: 500,
whenAudioEnds: RecordingAudioEndBehavior::TrimVideo, errorOnSourceInvalid: false,
),
trigger: new RecordingElementVisibleTrigger(selector: '#ready'),
actions: [
new RecordingLocateSelectorAction(selector: '#modal', ifNotFound: [], ifFound: [
Expand All @@ -105,6 +113,10 @@ public function testUrlOptionsAndNestedTimelinePreserveEmptyBranches(): void
);
self::assertTrue($client->createRecording($request)->success);
$data = json_decode((string) $http->requests[0]->getBody(), true, flags: JSON_THROW_ON_ERROR);
self::assertEquals([
'url' => 'https://cdn.example.com/audio/intro.mp3', 'source_start_ms' => 0, 'delay_ms' => 500,
'when_audio_ends' => 'trim_video', 'error_on_source_invalid' => false,
], $data['audio']);
foreach ([
'viewport_width' => 1280, 'viewport_height' => 720, 'device_scale' => 2,
'dedupe_duration_s' => 0, 'disable_twemoji' => false, 'color_scheme' => 'dark',
Expand Down