asciinema-cast-format
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseasciinema-cast-format
Asciinema Cast格式参考文档
Reference documentation for the asciinema v3 .cast file format (asciicast v2 specification).
Platform: All platforms (documentation only)
Asciinema v3 .cast文件格式(即asciicast v2规范)的参考文档。
平台: 所有平台(仅为文档)
When to Use This Skill
何时使用本技能
Use this skill when:
- Parsing or inspecting .cast file structure
- Understanding NDJSON header and event formats
- Building tools that read or write .cast files
- Debugging recording issues or format errors
- Learning the asciicast v2 specification
使用本技能的场景:
- 解析或检查.cast文件结构
- 理解NDJSON头部和事件格式
- 构建读取或写入.cast文件的工具
- 调试录制问题或格式错误
- 学习asciicast v2规范
Format Overview
格式概述
Asciinema v3 uses NDJSON (Newline Delimited JSON) format:
- Line 1: Header object with recording metadata
- Lines 2+: Event arrays with timestamp, type, and data
Asciinema v3采用NDJSON(换行分隔JSON)格式:
- 第1行:包含录制元数据的头部对象
- 第2行及以后:包含时间戳、类型和数据的事件数组
Header Specification
头部规范
The first line is a JSON object with these fields:
| Field | Type | Required | Description |
|---|---|---|---|
| int | Yes | Format version (always 2 for v3 recordings) |
| int | Yes | Terminal width in columns |
| int | Yes | Terminal height in rows |
| int | No | Unix timestamp of recording start |
| float | No | Total duration in seconds |
| string | No | Recording title |
| object | No | Environment variables (SHELL, TERM) |
| object | No | Terminal color theme |
第一行是一个JSON对象,包含以下字段:
| 字段名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| 整数 | 是 | 格式版本(v3录制始终为2) |
| 整数 | 是 | 终端宽度(列数) |
| 整数 | 是 | 终端高度(行数) |
| 整数 | 否 | 录制开始的Unix时间戳 |
| 浮点数 | 否 | 总时长(秒) |
| 字符串 | 否 | 录制标题 |
| 对象 | 否 | 环境变量(如SHELL、TERM) |
| 对象 | 否 | 终端配色主题 |
Example Header
头部示例
json
{
"version": 2,
"width": 120,
"height": 40,
"timestamp": 1703462400,
"duration": 3600.5,
"title": "Claude Code Session",
"env": { "SHELL": "/bin/zsh", "TERM": "xterm-256color" }
}json
{
"version": 2,
"width": 120,
"height": 40,
"timestamp": 1703462400,
"duration": 3600.5,
"title": "Claude Code Session",
"env": { "SHELL": "/bin/zsh", "TERM": "xterm-256color" }
}Event Codes
事件代码
Each event after the header is a 3-element array:
json
[timestamp, event_type, data]| Code | Name | Description | Data Format |
|---|---|---|---|
| Output | Terminal output (stdout) | String |
| Input | Terminal input (stdin) | String |
| Marker | Named marker for navigation | String (marker name) |
| Resize | Terminal resize event | |
| Exit | Extension for custom data | Varies |
头部之后的每个事件都是一个包含3个元素的数组:
json
[timestamp, event_type, data]| 代码 | 名称 | 描述 | 数据格式 |
|---|---|---|---|
| 输出 | 终端输出(stdout) | 字符串 |
| 输入 | 终端输入(stdin) | 字符串 |
| 标记 | 用于导航的命名标记 | 字符串(标记名称) |
| 调整大小 | 终端窗口大小调整事件 | |
| 扩展 | 自定义数据扩展 | 格式各异 |
Event Examples
事件示例
json
[0.5, "o", "$ ls -la\r\n"]
[1.2, "o", "total 48\r\n"]
[1.3, "o", "drwxr-xr-x 12 user staff 384 Dec 24 10:00 .\r\n"]
[5.0, "m", "file-listing-complete"]
[10.5, "r", "80x24"]json
[0.5, "o", "$ ls -la\r\n"]
[1.2, "o", "total 48\r\n"]
[1.3, "o", "drwxr-xr-x 12 user staff 384 Dec 24 10:00 .\r\n"]
[5.0, "m", "file-listing-complete"]
[10.5, "r", "80x24"]Timestamp Behavior
时间戳规则
- Timestamps are relative to recording start (first event is 0.0)
- Measured in seconds with millisecond precision
- Used for playback timing and navigation
- 时间戳是相对于录制开始时间的(第一个事件为0.0)
- 以秒为单位,精确到毫秒
- 用于播放计时和导航
Calculating Absolute Time
计算绝对时间
bash
/usr/bin/env bash << 'CALC_TIME_EOF'
HEADER_TIMESTAMP=$(head -1 recording.cast | jq -r '.timestamp')
EVENT_OFFSET=1234.5 # From event array
ABSOLUTE=$(echo "$HEADER_TIMESTAMP + $EVENT_OFFSET" | bc)
date -r "$ABSOLUTE" # macOSbash
/usr/bin/env bash << 'CALC_TIME_EOF'
HEADER_TIMESTAMP=$(head -1 recording.cast | jq -r '.timestamp')
EVENT_OFFSET=1234.5 # From event array
ABSOLUTE=$(echo "$HEADER_TIMESTAMP + $EVENT_OFFSET" | bc)
date -r "$ABSOLUTE" # macOSdate -d "@$ABSOLUTE" # Linux
date -d "@$ABSOLUTE" # Linux
CALC_TIME_EOF
---CALC_TIME_EOF
---Parsing Examples
解析示例
Extract Header with jq
使用jq提取头部
bash
/usr/bin/env bash << 'HEADER_EOF'
head -1 recording.cast | jq '.'
HEADER_EOFbash
/usr/bin/env bash << 'HEADER_EOF'
head -1 recording.cast | jq '.'
HEADER_EOFGet Recording Duration
获取录制时长
bash
/usr/bin/env bash << 'DURATION_EOF'
head -1 recording.cast | jq -r '.duration // "unknown"'
DURATION_EOFbash
/usr/bin/env bash << 'DURATION_EOF'
head -1 recording.cast | jq -r '.duration // "unknown"'
DURATION_EOFCount Events by Type
按类型统计事件数量
bash
/usr/bin/env bash << 'COUNT_EOF'
tail -n +2 recording.cast | jq -r '.[1]' | sort | uniq -c
COUNT_EOFbash
/usr/bin/env bash << 'COUNT_EOF'
tail -n +2 recording.cast | jq -r '.[1]' | sort | uniq -c
COUNT_EOFExtract All Output Events
提取所有输出事件
bash
/usr/bin/env bash << 'OUTPUT_EOF'
tail -n +2 recording.cast | jq -r 'select(.[1] == "o") | .[2]'
OUTPUT_EOFbash
/usr/bin/env bash << 'OUTPUT_EOF'
tail -n +2 recording.cast | jq -r 'select(.[1] == "o") | .[2]'
OUTPUT_EOFFind Markers
查找标记
bash
/usr/bin/env bash << 'MARKERS_EOF'
tail -n +2 recording.cast | jq -r 'select(.[1] == "m") | "\(.[0])s: \(.[2])"'
MARKERS_EOFbash
/usr/bin/env bash << 'MARKERS_EOF'
tail -n +2 recording.cast | jq -r 'select(.[1] == "m") | "\(.[0])s: \(.[2])"'
MARKERS_EOFGet Event at Specific Time
获取特定时间点的事件
bash
/usr/bin/env bash << 'TIME_EOF'
TARGET_TIME=60 # seconds
tail -n +2 recording.cast | jq -r "select(.[0] >= $TARGET_TIME and .[0] < $((TARGET_TIME + 1))) | .[2]"
TIME_EOFbash
/usr/bin/env bash << 'TIME_EOF'
TARGET_TIME=60 # seconds
tail -n +2 recording.cast | jq -r "select(.[0] >= $TARGET_TIME and .[0] < $((TARGET_TIME + 1))) | .[2]"
TIME_EOFLarge File Considerations
大文件处理注意事项
For recordings >100MB:
| File Size | Line Count | Approach |
|---|---|---|
| <100MB | <1M | jq streaming works fine |
| 100-500MB | 1-5M | Use |
| 500MB+ | 5M+ | Convert to .txt first with asciinema |
对于大于100MB的录制文件:
| 文件大小 | 行数 | 处理方法 |
|---|---|---|
| <100MB | <100万 | jq流式处理运行正常 |
| 100-500MB | 100万-500万 | 使用 |
| 500MB+ | 500万+ | 先使用asciinema转换为.txt格式 |
Memory-Efficient Streaming
内存高效的流式处理
bash
/usr/bin/env bash << 'STREAM_EOF'bash
/usr/bin/env bash << 'STREAM_EOF'Stream process large files
Stream process large files
jq --stream -n 'fromstream(1|truncate_stream(inputs))' recording.cast | head -1000
STREAM_EOF
undefinedjq --stream -n 'fromstream(1|truncate_stream(inputs))' recording.cast | head -1000
STREAM_EOF
undefinedUse asciinema convert
使用asciinema convert工具
For very large files, convert to plain text first:
bash
asciinema convert -f txt recording.cast recording.txtThis strips ANSI codes and produces clean text (typically 950:1 compression).
对于超大文件,先转换为纯文本:
bash
asciinema convert -f txt recording.cast recording.txt这会去除ANSI代码并生成干净的文本(通常压缩比为950:1)。
TodoWrite Task Template
TodoWrite任务模板
1. [Reference] Identify .cast file to analyze
2. [Header] Extract and display header metadata
3. [Events] Count events by type (o, i, m, r)
4. [Analysis] Extract relevant event data based on user need
5. [Navigation] Find markers or specific timestamps if needed1. [参考] 确定要分析的.cast文件
2. [头部] 提取并显示头部元数据
3. [事件] 按类型统计事件数量(o、i、m、r)
4. [分析] 根据用户需求提取相关事件数据
5. [导航] 如有需要,查找标记或特定时间点Post-Change Checklist
变更后检查清单
After modifying this skill:
- Event code table matches asciinema v2 specification
- Parsing examples use heredoc wrapper for bash compatibility
- Large file guidance reflects actual performance characteristics
- All jq commands tested with sample .cast files
修改本技能后:
- 事件代码表与asciinema v2规范一致
- 解析示例使用heredoc包装以保证bash兼容性
- 大文件处理指南反映实际性能特征
- 所有jq命令均已通过示例.cast文件测试
Reference Documentation
参考文档
Troubleshooting
故障排除
| Issue | Cause | Solution |
|---|---|---|
| jq parse error | Invalid NDJSON in .cast file | Check each line is valid JSON with |
| Header missing duration | Recording in progress | Duration added when recording ends |
| Unknown event type | Custom extension event | Check for |
| Timestamp out of order | Corrupted file | Events should be monotonically increasing |
| Large file jq timeout | File too big for in-memory | Use |
| Markers not found | No markers in recording | Markers are optional; not all recordings have them |
| Wrong version number | Older cast format | This skill covers v2 format (asciinema v3+) |
| Empty output from tail | File has only header | Recording may be empty or single-line |
| 问题 | 原因 | 解决方案 |
|---|---|---|
| jq解析错误 | .cast文件中存在无效的NDJSON | 使用 |
| 头部缺少duration字段 | 录制尚未完成 | 录制结束后会自动添加时长 |
| 未知事件类型 | 自定义扩展事件 | 检查是否为 |
| 时间戳顺序混乱 | 文件损坏 | 事件时间戳应单调递增 |
| 大文件jq超时 | 文件过大无法放入内存 | 使用 |
| 未找到标记 | 录制中未添加标记 | 标记为可选功能;并非所有录制都包含标记 |
| 版本号错误 | 较旧的cast格式 | 本技能仅覆盖v2格式(对应Asciinema v3及以上版本) |
| tail命令输出为空 | 文件仅有头部 | 录制可能为空或只有单行 |