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 @@ -146,7 +146,9 @@ batch_deleted = client.delete_recording_batch(["first-id", "second-id"])

Deletion returns a success response when accepted. `delete_recording_batch` accepts a list or tuple of strings; an empty list returns a successful result without sending an HTTP request. All recording methods return the existing `ApiErrorResponse` on API errors and propagate HTTPX 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, so checking status is optional.
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, so checking status is optional.

For an MP4 or WebM soundtrack, import `RecordingAssetAudio` or `RecordingUrlAudio` from `html_css_to_image`. Pass `audio=RecordingAssetAudio(asset_id="YOUR_ASSET_ID", delay_ms=500)` or `audio=RecordingUrlAudio(url="https://cdn.example.com/audio/intro.mp3")` on either recording request. [Audio sources and playback options](https://docs.htmlcsstoimage.com/parameters/audio/).

```python
status = client.fetch_recording_status("recording-id")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "html-css-to-image"
version = "1.0.1"
version = "1.1.0"
description = "Official Python client for the HTML/CSS to Image API"
readme = "README.md"
requires-python = ">=3.10"
Expand Down
8 changes: 8 additions & 0 deletions src/html_css_to_image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
DeleteRecordingSuccessResponse,
PositionScrollTarget,
RecordingAction,
RecordingAssetAudio,
RecordingAudio,
RecordingAudioEndBehavior,
RecordingClickAction,
RecordingDurationMode,
RecordingElementVisibleTrigger,
Expand All @@ -71,6 +74,7 @@
RecordingStatusResponse,
RecordingStatusSuccessResponse,
RecordingTrigger,
RecordingUrlAudio,
ScrollEasing,
ScrollPosition,
ScrollStep,
Expand All @@ -90,6 +94,9 @@
"DeleteRecordingSuccessResponse",
"PositionScrollTarget",
"RecordingAction",
"RecordingAssetAudio",
"RecordingAudio",
"RecordingAudioEndBehavior",
"RecordingClickAction",
"RecordingDurationMode",
"RecordingElementVisibleTrigger",
Expand All @@ -109,6 +116,7 @@
"RecordingStatusResponse",
"RecordingStatusSuccessResponse",
"RecordingTrigger",
"RecordingUrlAudio",
"ScrollEasing",
"ScrollPosition",
"ScrollStep",
Expand Down
49 changes: 47 additions & 2 deletions src/html_css_to_image/recordings.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

RecordingFormat: TypeAlias = Literal["mp4", "webm", "gif"]

RecordingAudioEndBehavior: TypeAlias = Literal["silence", "loop", "trim_video"]

RecordingDurationMode: TypeAlias = Literal["fixed", "target", "at_least", "at_most"]

RecordingMouseButton: TypeAlias = Literal["left", "right", "middle", "back", "forward"]
Expand Down Expand Up @@ -294,6 +296,46 @@ class SelectorScrollTarget:
if_not_found: list[RecordingAction] | None = None


@dataclass(slots=True, kw_only=True)
class _RecordingAudioOptions:
"""Playback options shared by both audio sources.

Attributes:
source_start_ms: Offset into the source file in milliseconds. Defaults to 0.
delay_ms: Delay from the start of the finished video in milliseconds.
Defaults to 0.
when_audio_ends: Keep playing silently (default), loop from the source offset,
or trim the video when the track ends.
error_on_source_invalid: Defaults to True. Set False to return the full
silent video if the track cannot play at render time.
"""

source_start_ms: int | None = None
delay_ms: int | None = None
when_audio_ends: RecordingAudioEndBehavior | None = None
error_on_source_invalid: bool | None = None


@dataclass(slots=True, kw_only=True)
class RecordingAssetAudio(_RecordingAudioOptions):
"""Use the asset_id of a completed audio upload from the recording organization.

Private Asset Library uploads are supported.
"""

asset_id: str


@dataclass(slots=True, kw_only=True)
class RecordingUrlAudio(_RecordingAudioOptions):
"""Use a public HTTP or HTTPS URL serving the audio file at render time."""

url: str


RecordingAudio: TypeAlias = RecordingAssetAudio | RecordingUrlAudio


@dataclass(slots=True, kw_only=True)
class RecordingOutputOptions:
"""Output format, frame rate, and scaling for a recording.
Expand Down Expand Up @@ -394,8 +436,10 @@ class BaseCreateRecordingRequest:
immediate. See https://docs.htmlcsstoimage.com/parameters/trigger/
actions: Ordered interactions. Leave None or empty to record the page without
interactions. See https://docs.htmlcsstoimage.com/parameters/actions/
output: Output format, frame rate, and scale. Defaults to MP4 at 30 fps without
audio. See https://docs.htmlcsstoimage.com/parameters/output/
audio: Optional MP4 or WebM soundtrack from an asset ID or public URL.
See https://docs.htmlcsstoimage.com/parameters/audio/
output: Output format, frame rate, and scale. Defaults to MP4 at 30 fps.
See https://docs.htmlcsstoimage.com/parameters/output/
duration_mode: How duration_ms limits the timeline. Defaults to at_most. See
https://docs.htmlcsstoimage.com/parameters/duration_mode/
defer_render: Wait to render until media or storage is requested. Defaults to
Expand Down Expand Up @@ -424,6 +468,7 @@ class BaseCreateRecordingRequest:
duration_ms: int
trigger: RecordingTrigger | None = None
actions: list[RecordingAction] | None = None
audio: RecordingAudio | None = None
output: RecordingOutputOptions | None = None
duration_mode: RecordingDurationMode | None = None
defer_render: bool | None = None
Expand Down
18 changes: 18 additions & 0 deletions tests/test_recordings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CreateUrlRecordingRequest,
HtmlCssToImageClient,
PositionScrollTarget,
RecordingAssetAudio,
RecordingClickAction,
RecordingElementVisibleTrigger,
RecordingErrorAction,
Expand All @@ -25,6 +26,7 @@
RecordingScrollAction,
RecordingSimpleScrollAction,
RecordingStallAction,
RecordingUrlAudio,
RequestOverride,
RequestOverrideResourceType,
ScrollStep,
Expand All @@ -49,6 +51,7 @@ def test_html_creation_maps_fonts_and_preserves_input(self):
request = CreateHtmlCssRecordingRequest(
html="<h1>Hello</h1>",
duration_ms=1000,
audio=RecordingAssetAudio(asset_id="asset_1"),
google_fonts=[" Roboto ", "Open Sans", "Open Sans"],
)
before = copy.deepcopy(request)
Expand All @@ -62,6 +65,7 @@ def handler(req):
json.loads(req.content),
{
"html": "<h1>Hello</h1>",
"audio": {"asset_id": "asset_1"},
"duration_ms": 1000,
"google_fonts": "Roboto|Open+Sans",
},
Expand All @@ -79,6 +83,13 @@ def test_url_options_and_all_nested_action_types(self):
request = CreateUrlRecordingRequest(
url="https://example.com",
duration_ms=5000,
audio=RecordingUrlAudio(
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,
),
css="body { color: red; }",
device_scale=2,
viewport_width=1280,
Expand Down Expand Up @@ -172,6 +183,13 @@ def handler(req):
"defer_render": False,
"skip_scroll_preload": True,
"duration_mode": "at_least",
"audio": {
"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,
},
"headers": {"Authorization": "Bearer test"},
"include_headers_on_subrequests": False,
"identify_as_hcti": True,
Expand Down