
Byted Tos Video Process
- 30 installs
- 411 repo stars
- Updated August 4, 2026
- bytedance/agentkit-samples
byted-tos-video-process is a Claude skill that inspects videos, extracts frames, and concatenates clips stored in Volcengine TOS.
About
This skill inspects videos, extracts frames, and concatenates clips stored in Volcengine TOS. A developer uses it to read video metadata, capture single or batch snapshots by timestamp or interval, and merge multiple video clips into one. Results can be saved locally or persisted back to TOS.
- Reads video metadata and extracts frames from TOS-stored videos
- Captures single or batch frames by timestamp or interval
- Concatenates multiple video clips via async media jobs
Byted Tos Video Process by the numbers
- 30 all-time installs (skills.sh)
- Ranked #969 of 1,335 Generative Media skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
byted-tos-video-process capabilities & compatibility
Requires Volcengine TOS credentials (access key, secret, endpoint, region, bucket)
- Capabilities
- video processing · frame extraction · video concat · media processing
- Use cases
- video generation
- Pricing
- Bring your own API key
What byted-tos-video-process says it does
Inspects videos, extracts frames, and concatenates video clips stored in Volcengine TOS
Uses TOS gateway `media_jobs` async API with `job_type=Concat`.
npx skills add https://github.com/bytedance/agentkit-samples --skill byted-tos-video-processAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 30 |
|---|---|
| repo stars | ★ 411 |
| Last updated | August 4, 2026 |
| Repository | bytedance/agentkit-samples ↗ |
What it does
Inspect Volcengine TOS videos, extract frames by timestamp or interval, and concatenate clips.
Who is it for?
Skip if: Full video transcoding pipelines, image-only or document-only workflows, or non-TOS storage providers.
When should I use this skill?
You need video duration or codec info, a poster or thumbnail, frame extraction at timestamps or intervals, or to merge video clips in TOS.
What you get
Returns video metadata, extracted frames, and concatenated clips saved locally or back to TOS.
By the numbers
- Bundles 4 video scripts
- All timestamp parameters are in milliseconds
Files
Volcengine TOS Video Process
Inspect videos, extract frames, and concatenate video clips stored in Volcengine TOS — metadata lookup, single snapshot, multi-frame capture, video concatenation, and TOS-to-TOS persistence.
Setup (once per environment)
Install dependencies on first use:
cd {baseDir}
pip install -r {baseDir}/requirements.txtThen run scripts with Python 3.7+:
python3 {baseDir}/scripts/<script>.py <args>If you see a ModuleNotFoundError for tos, reinstall dependencies.
Environment Variables
This skill relies on the TOS identity declared in the metadata block. Common runtime variables are:
| Environment Variable | Required | Description |
|---|---|---|
TOS_ACCESS_KEY | Yes | TOS access key ID |
TOS_SECRET_KEY | Yes | TOS secret access key |
TOS_ENDPOINT | Yes | TOS endpoint URL |
TOS_REGION | Yes | TOS region |
TOS_BUCKET | Yes | Source bucket that stores the video |
TOS_OBJECT_KEY | No | Source object key of the video. Some scripts accept alternatives, while video_info.py and video_snapshot.py read this value from the environment |
TOS_SECURITY_TOKEN | No | STS session token when using temporary credentials |
TOS_SAVEAS_BUCKET | No | Default target bucket for saving snapshots |
TOS_SAVEAS_OBJECT_PREFIX | No | Default key prefix for saving snapshots |
Quick start (common tasks)
# Read video metadata (resolution, duration, codec)
TOS_OBJECT_KEY=demo.mp4 python3 {baseDir}/scripts/video_info.py
# Capture a single frame at 5 seconds
TOS_OBJECT_KEY=demo.mp4 python3 {baseDir}/scripts/video_snapshot.py --time 5000 --output frame_5s.jpg
# Capture multiple frames and save locally
python3 {baseDir}/scripts/video_snapshots.py --key demo.mp4 --timestamps 1000 3000 5000
# Capture multiple frames by interval
python3 {baseDir}/scripts/video_snapshots.py --key demo.mp4 \
--interval-ms 5000 --duration-ms 60000 --output-dir snapshots
# Capture multiple frames and save to TOS
python3 {baseDir}/scripts/video_snapshots.py --key demo.mp4 --timestamps 1000 3000 5000 --save-to-tos
# Concatenate multiple video clips
python3 {baseDir}/scripts/video_concat.py --key clip1.mp4 --fragments "clip2.mp4,clip3.mp4" --output-key merged.mp4 --waitAvailable scripts
| Script | Purpose |
|---|---|
scripts/video_info.py | Read video metadata (format, duration, resolution, codec, frame rate). |
scripts/video_snapshot.py | Capture a single frame at a given timestamp. Supports local save or TOS-to-TOS persistence. |
scripts/video_snapshots.py | Batch-capture multiple frames across a timeline. Supports explicit timestamps, interval mode, concurrent execution, and TOS-to-TOS persistence. |
scripts/video_concat.py | Concatenate multiple video clips into one. Uses TOS gateway media_jobs async API with job_type=Concat. Supports --wait to poll until completion. |
video_snapshots.py and video_concat.py support --key-style object selection. video_info.py and video_snapshot.py currently read TOS_OBJECT_KEY from the environment. video_snapshots.py also supports --bucket override for multi-bucket testing. Run any script with -h for full usage.
Out of scope
- Full video transcoding pipelines (this skill covers metadata + frame extraction + concatenation only).
- Image-only or document-only workflows (use
byted-tos-image-processorbyted-tos-doc-process). - Non-TOS storage providers.
Rules
- Authentication: Authentication is provided by the TOS identity declared in the
metadatablock above. Object selection can be overridden per script with--key. - Timestamp unit: All timestamp parameters are in milliseconds (e.g.,
5000= 5 seconds). - Validate timestamps: Ensure snapshot timestamps do not exceed the video duration. Use
video_info.pyto read duration first when needed. - Save to TOS: Use
--save-to-toswithTOS_SAVEAS_BUCKET/TOS_SAVEAS_OBJECT_PREFIXto persist snapshots back to TOS without local download. - Parameter source of truth: Official Volcengine TOS documentation is authoritative for the full
video/snapshotparameter matrix. When uncertain, check REFERENCE.md. - Language: Reply in the user's preferred language.
Further reading
- Setup and environment: README.md
- Parameter reference: REFERENCE.md
- End-to-end workflows: WORKFLOWS.md
__pycache__/
*.pyc
*.pyo
*.pyd
.env
.DS_Store
*.jpg
*.png
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Bytedance TOS Video Process Skill
This skill provides a focused video-processing toolkit for files stored in Bytedance TOS. It covers the most common TOS video workflows: reading metadata, extracting single snapshots, and capturing multiple frames across a timeline.
When To Use
Use this skill when you need to:
- Read video metadata with
video/info - Generate poster images or thumbnails from videos in TOS
- Capture a frame at a specific timestamp
- Capture multiple frames for a timeline or sampling workflow
- Sample frames either by explicit timestamps or by interval
- Save snapshot results locally or back to TOS
Do not use this skill for:
- Full video transcoding pipelines
- Image-only or document-only workflows
- Generic object storage tasks unrelated to TOS video processing
How It Works
Video processing is performed by passing a formatted process string to the Volcengine TOS SDK. The main patterns are:
video/infovideo/snapshot,t_<time>,f_<format>,w_<width>,h_<height>
The scripts in this skill show how to combine those operations with local file output, TOS-to-TOS save flows, and repeated snapshot extraction across multiple timestamps.
Directory Layout
byted-tos-video-process/
├── SKILL.md
├── README.md
├── REFERENCE.md
├── WORKFLOWS.md
├── LICENSE
├── requirements.txt
├── .gitignore
└── scripts/
├── tos_jobs_client.py
├── video_info.py
├── video_snapshot.py
├── video_snapshots.py
└── video_concat.pyRequirements
- Python 3.7+
- Access to Volcengine TOS
- Valid AK/SK or STS credentials
- Network access to the target TOS endpoint
Install dependencies:
pip install -r requirements.txtEnvironment Variables
| Variable | Required | Description | Example |
|---|---|---|---|
TOS_ACCESS_KEY | Yes | TOS access key ID. | AK... |
TOS_SECRET_KEY | Yes | TOS secret access key. | your-secret-key |
TOS_ENDPOINT | Yes | TOS endpoint URL. | https://tos-cn-beijing.volces.com |
TOS_REGION | Yes | TOS region. | cn-beijing |
TOS_BUCKET | Yes | Source bucket that stores the video. | my-video-bucket |
TOS_OBJECT_KEY | Yes | Source object key of the video. | input/demos/wildlife.mp4 |
TOS_SECURITY_TOKEN | No | STS session token when using temporary credentials. | STS... |
TOS_SAVEAS_BUCKET | No | Default target bucket for saving snapshots. | my-snapshot-bucket |
TOS_SAVEAS_OBJECT_PREFIX | No | Default key prefix for saving snapshots. | processed/snapshots/ |
For production usage, prefer short-lived STS credentials. The SDK automatically uses TOS_SECURITY_TOKEN when it is present.
Quick Start
Export the minimum required configuration:
export TOS_ACCESS_KEY="YOUR_AK"
export TOS_SECRET_KEY="YOUR_SK"
export TOS_ENDPOINT="https://tos-cn-beijing.volces.com"
export TOS_REGION="cn-beijing"
export TOS_BUCKET="your-video-bucket"
export TOS_OBJECT_KEY="path/to/your/video.mp4"Run one of the ready-to-use examples:
Read video metadata:
python3 scripts/video_info.py --key path/to/your/video.mp4 --jsonCapture a single frame at 5 seconds:
python3 scripts/video_snapshot.py --key path/to/your/video.mp4 --time 5000 --output local_frame.jpgCapture multiple frames and save to TOS:
python3 scripts/video_snapshots.py \
--key path/to/your/video.mp4 \
--timestamps 1000 3000 5000 \
--saveas-bucket "your-output-bucket" \
--saveas-object "snapshots"Capture multiple frames by interval:
python3 scripts/video_snapshots.py \
--key test.mp4 \
--interval-ms 5000 \
--duration-ms 60000 \
--output snapshotsCreate a concat job:
python3 scripts/video_concat.py \
--key clips/part1.mp4 \
--fragments clips/part2.mp4,clips/part3.mp4 \
--saveas-object clips/final_concat.mp4 \
--waitDocument Roles
SKILL.md: trigger-oriented instructions for agents deciding whether to load this skillREADME.md: setup guide and runnable entry points for humans and agentsREFERENCE.md: parameter reference and result semanticsWORKFLOWS.md: common snapshot and metadata workflowsscripts/: executable examples for common video-processing tasks
Usage Notes
- This skill focuses on
video/infoandvideo/snapshot, not full video transcoding. - Validate timestamp and output size parameters before sending requests.
- Multi-frame extraction is a client-side orchestration pattern that issues repeated snapshot requests concurrently.
video_snapshots.pynow supports--keyand--bucketoverrides, which makes it consistent with the rest of the TOS skills during manual testing.- Saving snapshots back to TOS is often preferable for downstream workflows.
- Official Volcengine TOS documentation remains the source of truth for the full parameter matrix.
Related Files
- Parameter reference: REFERENCE.md
- Workflow guide: WORKFLOWS.md
License
This skill is licensed under the Apache License 2.0. See LICENSE.
Bytedance TOS Video Process SDK Reference
This document provides a detailed reference for the parameters and return values of the core video processing operations, as implemented via the Volcengine TOS Python SDK.
Table of Contents
- Authentication
- Core Operations
- 1. `videoInfo`
- 2. `videoSnapshot`
- 3. `videoSnapshots`
- 4. `videoConcat`
- Data Models
- VideoInfo Object
- SnapshotSaveResult Object
---
Authentication
Authentication is handled automatically by the tos.TosClientV2 client during initialization. Credentials should be provided via environment variables as described in the README.md. The SDK will construct the necessary request headers.
---
Core Operations
Video processing is invoked by passing a specially formatted process string to the get_object or get_object_to_file methods of the TOS SDK client.
1. videoInfo
Retrieves metadata for a specified video object in TOS.
SDK Method: client.get_object()
Key Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The name of the bucket containing the video. |
key | string | Yes | The full object key (path) of the video file. |
process | string | Yes | Must be set to the exact string "video/info". |
Example:
import tos
import json
# client = initialized tos.TosClientV2
# bucket_name = "your-bucket"
# object_key = "your-video.mp4"
try:
response = client.get_object(
bucket=bucket_name,
key=object_key,
process="video/info"
)
# The response body is a stream, read it to get the content
raw_data = response.read()
video_info = json.loads(raw_data.decode('utf-8'))
print(video_info)
except tos.exceptions.TosServerError as e:
print(f"Failed to get video info: {e}")
Success Response:
- The
response.read()call returns abytesobject containing a JSON string. - This JSON string represents the VideoInfo Object.
---
2. videoSnapshot
Captures a single frame from a video. The output can be either saved to a local file, returned as a data stream, or persisted directly within TOS.
Mode 1: Save to a Local File
SDK Method: client.get_object_to_file()
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The bucket where the video is located. |
key | string | Yes | The object key of the video file. |
file_path | string | Yes | The local path where the snapshot image will be saved. |
process | string | Yes | A composite string starting with video/snapshot. See below. |
Mode 2: Save directly to TOS
SDK Method: client.get_object()
| Parameter | Type | Required | Description |
|---|---|---|---|
bucket | string | Yes | The source bucket of the video. |
key | string | Yes | The source object key of the video. |
process | string | Yes | A composite string starting with video/snapshot. See below. |
save_bucket | string | Yes | The destination bucket to save the snapshot in. |
save_object | string | Yes | The destination object key for the saved snapshot. |
Constructing the process Parameter
The process string for videoSnapshot is built as video/snapshot followed by a comma-separated list of key_value options.
| Option | SDK Equivalent | Description |
|---|---|---|
t | time (int) | Timestamp in milliseconds (ms) to capture the frame. |
w | width (int) | Width of the snapshot in pixels. If 0, aspect ratio is maintained. |
h | height (int) | Height of the snapshot in pixels. If 0, aspect ratio is maintained. |
m | mode (string) | fast captures the nearest keyframe before time. Default is precise. |
f | output_format (string) | jpg (default) or png. |
ar | auto_rotate (string) | auto: rotate automatically. w: force landscape. h: force portrait. |
Example `process` string: "video/snapshot,t_5000,w_1280,f_jpg"
Example (Save to Local File):
# Captures frame at 10s and saves to "frame.jpg"
client.get_object_to_file(
bucket=bucket_name,
key=object_key,
file_path="frame.jpg",
process="video/snapshot,t_10000"
)Example (Save to TOS):
response = client.get_object(
bucket=bucket_name,
key=object_key,
process="video/snapshot,t_10000",
save_bucket="my-results-bucket",
save_object="snapshots/frame_at_10s.jpg"
)
# response.read() will contain a JSON string with the save result
save_result = json.loads(response.read().decode('utf-8'))
print(save_result)Success Response (Save to TOS):
- The
response.read()call returns abytesobject containing a JSON string. - This JSON string represents the SnapshotSaveResult Object.
---
3. videoSnapshots
This is not a single SDK method but a client-side orchestration pattern. It involves making multiple videoSnapshot calls in a loop or in parallel. The scripts/video_snapshots.py script provides a complete, concurrent implementation.
Functionality:
- Iterates through a list of user-provided or calculated timestamps.
- For each timestamp, it makes a
get_object_to_fileorget_object(withsave_bucket/save_object) request. - It can either save the resulting images locally or save them back to TOS.
- It supports both explicit timestamps and interval-derived timestamps.
- The current script also accepts
--bucketand--keyoverrides instead of relying only on environment variables.
Please refer to the scripts/video_snapshots.py for a reference implementation.
---
4. videoConcat
Concatenates multiple video clips into a single output video. This is an async operation submitted via the TOS gateway's media_jobs API.
API Endpoint: POST /{bucket}?media_jobs=&job_type=Concat
Job Payload:
{
"Input": {
"Object": "clip1.mp4"
},
"ConcatConfig": {
"ConcatFragments": [
{"Object": "clip2.mp4"},
{"Object": "clip3.mp4"}
],
"Container": {
"Format": "mp4"
},
"Video": {
"Codec": "h264"
},
"Audio": {
"Codec": "aac"
}
},
"Output": {
"Region": "cn-beijing",
"Bucket": "your-bucket",
"Object": "output/merged.mp4"
}
}CLI Mapping:
| CLI argument | Meaning | Notes |
|---|---|---|
--key | First video clip object key | Required |
--fragments | Additional clips, comma-separated | Optional |
--format | Output container format | Default mp4 |
--video-codec | Video codec | Default h264 |
--audio-codec | Audio codec | Default aac |
--output-key | Output object key | Default <key>_concat.<format> |
--output-bucket | Output bucket | Default same as source |
--wait | Poll until completion | Optional |
--timeout | Max wait time in seconds | Default 600 |
Script: scripts/video_concat.py
---
Data Models
VideoInfo Object
A JSON object containing detailed information about the video's format and streams, as returned by the video/info process.
| Field | Type | Description |
|---|---|---|
Format | object | Container format details (e.g., mov, mp4). Includes Duration, Size, BitRate. |
VideoStream | object | Video stream details. Includes Width, Height, CodecName, Duration, AvgFrameRate. |
AudioStream | object | (Optional) Audio stream details if present. Includes CodecName, SampleRate, Channels. |
Example Snippet:
{
"VideoStream": {
"Width": 1920,
"Height": 1080,
"Duration": "10.000000",
"AvgFrameRate": "25/1"
},
"Format": {
"FormatName": "mov,mp4,m4a,3gp,3g2,mj2",
"Duration": "10.024000",
"Size": "2506000"
}
}SnapshotSaveResult Object
A JSON object returned when a snapshot is successfully saved to TOS using the save_bucket and save_object parameters.
| Field | Type | Description |
|---|---|---|
ETag | string | The ETag of the saved snapshot object. |
Bucket | string | The bucket where the snapshot was saved. |
Object | string | The object key of the saved snapshot. |
VersionId | string | The version ID of the saved object if versioning is enabled. |
HashCrc64ecma | string | The CRC64 checksum of the object. |
tos
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import time
import urllib.request
import urllib.error
from typing import Optional
import tos
class TOSJobsClient:
def __init__(self, ak: str, sk: str, endpoint: str, region: str, bucket: str):
self.bucket = bucket
self.region = region
self.client = tos.TosClientV2(ak, sk, endpoint, region)
def _signed_request(
self, method: str, query: dict, body: Optional[bytes] = None
) -> dict:
url_out = self.client.pre_signed_url(
tos.HttpMethodType.Http_Method_Post
if method == "POST"
else tos.HttpMethodType.Http_Method_Get,
self.bucket,
"",
expires=900,
query=query,
)
req = urllib.request.Request(
url_out.signed_url,
data=body,
method=method,
)
for k, v in url_out.signed_header.items():
req.add_header(k, v)
if body is not None:
req.add_header("Content-Type", "application/json")
try:
with urllib.request.urlopen(req) as resp:
data = resp.read()
return json.loads(data)
except urllib.error.HTTPError as e:
body_text = e.read().decode("utf-8", errors="replace")
raise RuntimeError(f"HTTP {e.code}: {body_text}") from e
def create_job(self, job_category: str, job_type: str, job_detail: dict) -> dict:
query = {job_category: "", "job_type": job_type}
body = json.dumps(job_detail).encode("utf-8")
return self._signed_request("POST", query, body)
def get_job(
self, job_type: str, job_id: str, job_category: str = "media_jobs"
) -> dict:
query = {job_category: "", "job_type": job_type, "job_id": job_id}
return self._signed_request("GET", query)
def wait_for_job(
self,
job_type: str,
job_id: str,
timeout: int = 300,
interval: int = 5,
job_category: str = "media_jobs",
) -> dict:
deadline = time.time() + timeout
while time.time() < deadline:
result = self.get_job(job_type, job_id, job_category=job_category)
items = result.get("Items", [])
if items:
item = items[0]
else:
item = result
state = item.get("State", "")
if state in ("Success", "Failed"):
return item
time.sleep(interval)
raise TimeoutError(f"Job {job_id} did not complete within {timeout}s")
def get_env(name: str) -> str:
val = os.environ.get(name, "")
if not val:
raise ValueError(f"Environment variable {name} is required")
return val
def create_client_from_env() -> TOSJobsClient:
return TOSJobsClient(
ak=get_env("TOS_ACCESS_KEY"),
sk=get_env("TOS_SECRET_KEY"),
endpoint=get_env("TOS_ENDPOINT"),
region=get_env("TOS_REGION"),
bucket=get_env("TOS_BUCKET"),
)
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env python3
import argparse
import json
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from tos_jobs_client import create_client_from_env
JOB_TYPE = "Concat"
JOB_CATEGORY = "media_jobs"
def build_concat_detail(
first_object: str,
fragments: list,
output_bucket: str,
output_object: str,
region: str,
container_format: str = "mp4",
video_codec: str = "h264",
audio_codec: str = "aac",
) -> dict:
concat_fragments = [{"Object": f} for f in fragments]
return {
"Input": {"Object": first_object},
"ConcatConfig": {
"ConcatFragments": concat_fragments,
"Container": {"Format": container_format},
"Video": {"Codec": video_codec},
"Audio": {"Codec": audio_codec},
},
"Output": {
"Region": region,
"Bucket": output_bucket,
"Object": output_object,
},
}
def fail(message: str) -> None:
print(f"[ERROR] {message}", file=sys.stderr)
sys.exit(1)
def emit(payload: dict, json_only: bool, heading=None) -> None:
if json_only:
print(json.dumps(payload, ensure_ascii=False))
return
if heading:
print(heading)
print(json.dumps(payload, indent=2, ensure_ascii=False))
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Concatenate video clips via TOS media_jobs API"
)
parser.add_argument("--key", help="First video fragment object key (required)")
parser.add_argument("--bucket", help="TOS bucket name (default: TOS_BUCKET env)")
parser.add_argument(
"--fragments", help="Subsequent video fragment object keys, comma-separated"
)
parser.add_argument(
"--output-key", dest="output_key", help="Deprecated alias of --saveas-object"
)
parser.add_argument(
"--output-bucket",
dest="output_bucket",
help="Deprecated alias of --saveas-bucket",
)
parser.add_argument(
"--saveas-object", help="Output object key (default: <key>_concat.mp4)"
)
parser.add_argument(
"--saveas-bucket", help="Output bucket (default: same as source)"
)
parser.add_argument(
"--format", default="mp4", help="Output container format (default: mp4)"
)
parser.add_argument(
"--video-codec", default="h264", help="Video codec (default: h264)"
)
parser.add_argument(
"--audio-codec", default="aac", help="Audio codec (default: aac)"
)
parser.add_argument("--wait", action="store_true", help="Wait for job completion")
parser.add_argument(
"--timeout",
type=int,
default=600,
help="Max wait time in seconds (default: 600)",
)
parser.add_argument(
"--poll-interval",
type=int,
default=5,
help="Poll interval in seconds (default: 5)",
)
parser.add_argument(
"--job-id", help="Query an existing job instead of creating a new one"
)
parser.add_argument(
"--validate", action="store_true", help="Validate arguments and payload only"
)
parser.add_argument(
"--dry-run", action="store_true", help="Print resolved payload and exit"
)
parser.add_argument(
"--json", action="store_true", help="Print machine-readable JSON only"
)
return parser.parse_args()
def main():
args = parse_args()
bucket = args.bucket or os.environ.get("TOS_BUCKET", "")
if not bucket:
fail("--bucket or TOS_BUCKET env is required")
region = os.environ.get("TOS_REGION", "")
if not region:
fail("TOS_REGION env is required")
client = create_client_from_env()
client.bucket = bucket
client.region = region
if args.job_id:
result = client.get_job(JOB_TYPE, args.job_id, job_category=JOB_CATEGORY)
emit(
{
"ok": True,
"operation": "video_concat_query",
"bucket": bucket,
"job_id": args.job_id,
"job_type": JOB_TYPE,
"job_category": JOB_CATEGORY,
"result": result,
},
args.json,
"[OK] Job query result:",
)
return
key = args.key
if not key:
fail("--key is required")
fragments = []
if args.fragments:
fragments = [f.strip() for f in args.fragments.split(",") if f.strip()]
output_bucket = args.saveas_bucket or args.output_bucket or bucket
output_key = (
args.saveas_object
or args.output_key
or key.rsplit(".", 1)[0] + "_concat." + args.format
)
detail = build_concat_detail(
first_object=key,
fragments=fragments,
output_bucket=output_bucket,
output_object=output_key,
region=region,
container_format=args.format,
video_codec=args.video_codec,
audio_codec=args.audio_codec,
)
plan = {
"ok": True,
"operation": "video_concat",
"bucket": bucket,
"key": key,
"fragments": fragments,
"saveas_bucket": output_bucket,
"saveas_object": output_key,
"job_type": JOB_TYPE,
"job_category": JOB_CATEGORY,
"detail": detail,
}
if args.validate or args.dry_run:
emit(plan, args.json, "[OK] Resolved request:")
return
if not args.json:
all_clips = [key] + fragments
print(
f"[INFO] Creating Concat job: {' + '.join(all_clips)} -> {output_bucket}/{output_key}"
)
resp = client.create_job(JOB_CATEGORY, JOB_TYPE, detail)
job_id = resp.get("JobId", "")
if not job_id:
fail(f"No JobId in response: {resp}")
result_payload = {
**plan,
"job_id": job_id,
"create_response": resp,
}
if args.wait:
if not args.json:
print(f"[INFO] Waiting for job completion (timeout={args.timeout}s)...")
result = client.wait_for_job(
JOB_TYPE,
job_id,
timeout=args.timeout,
interval=args.poll_interval,
job_category=JOB_CATEGORY,
)
result_payload["final_result"] = result
result_payload["state"] = result.get("State", "Unknown")
emit(result_payload, args.json, "[OK] Video concatenation result:")
if result_payload["state"] != "Success":
sys.exit(1)
return
emit(result_payload, args.json, "[OK] Job created:")
if __name__ == "__main__":
main()
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env python3
"""Example script: get video information from TOS using the Python SDK.
Reads configuration from environment variables and/or CLI arguments and calls the
`videoInfo` operation by issuing a `get_object` request with `process="video/info"`.
Environment variables:
- TOS_ACCESS_KEY Access key ID (AK) or STS AccessKeyId
- TOS_SECRET_KEY Secret access key (SK) or STS SecretAccessKey
- TOS_SECURITY_TOKEN (optional) STS session token
- TOS_ENDPOINT TOS endpoint, e.g. https://tos-cn-beijing.volces.com
- TOS_REGION TOS region, e.g. cn-beijing
- TOS_BUCKET Bucket name that stores the video
- TOS_OBJECT_KEY Object key of the video file in the bucket
"""
import argparse
import json
import os
import sys
from typing import Optional
import tos
from tos.exceptions import TosClientError, TosServerError
def get_env(name: str, required: bool = True, default: Optional[str] = None) -> str:
"""Read an environment variable or exit with an error if required and missing."""
value = os.getenv(name, default)
if required and not value:
print(f"[ERROR] Environment variable {name} is required.", file=sys.stderr)
sys.exit(1)
return value # type: ignore[return-value]
def create_client() -> tos.TosClientV2:
"""Initialize a TosClientV2 using AK/SK (and optional STS token)."""
ak = get_env("TOS_ACCESS_KEY")
sk = get_env("TOS_SECRET_KEY")
endpoint = get_env("TOS_ENDPOINT")
region = get_env("TOS_REGION")
security_token = os.getenv("TOS_SECURITY_TOKEN")
return tos.TosClientV2(
ak=ak,
sk=sk,
endpoint=endpoint,
region=region,
security_token=security_token,
)
def main() -> None:
parser = argparse.ArgumentParser(description="Get video info via TOS process=video/info")
parser.add_argument("--bucket", type=str, default=None, help="Override TOS_BUCKET")
parser.add_argument("--key", type=str, default=None, help="Override TOS_OBJECT_KEY")
parser.add_argument("--json", action="store_true", help="Print machine-readable JSON only")
args = parser.parse_args()
client = create_client()
bucket = args.bucket or get_env("TOS_BUCKET")
key = args.key or get_env("TOS_OBJECT_KEY")
if not args.json:
print(f"[INFO] Requesting video info for {bucket}/{key} ...")
try:
output = client.get_object(bucket, key, process="video/info")
raw = output.read()
except TosServerError as e:
print(
f"[ERROR] TOS server error: code={e.code}, status={e.status_code}, "
f"request_id={e.request_id}, message={e.message}",
file=sys.stderr,
)
sys.exit(1)
except TosClientError as e:
print(f"[ERROR] TOS client error: {e}", file=sys.stderr)
sys.exit(1)
except Exception as exc: # noqa: BLE001
print(f"[ERROR] Unexpected error: {exc}", file=sys.stderr)
sys.exit(1)
try:
text = raw.decode("utf-8")
data = json.loads(text)
except Exception as exc: # noqa: BLE001
print("[ERROR] Failed to parse response as JSON:", file=sys.stderr)
print(exc, file=sys.stderr)
print(raw[:200], file=sys.stderr)
sys.exit(1)
if args.json:
print(
json.dumps(
{
"ok": True,
"operation": "video_info",
"bucket": bucket,
"key": key,
"process": "video/info",
"result": data,
},
ensure_ascii=False,
)
)
return
print("[OK] Video info:")
print(json.dumps(data, indent=2, ensure_ascii=False))
if __name__ == "__main__":
main()
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env python3
"""Example script: take a single snapshot from a TOS video using the Python SDK.
Reads configuration from environment variables and optional CLI args,
constructs a `process="video/snapshot,..."` rule string, and either:
- saves the returned image locally (default), or
- if `--saveas-bucket`/`--saveas-object` is provided, lets TOS persist
the snapshot to the specified object and prints the JSON result.
"""
import argparse
import base64
import json
import os
import sys
from typing import Any, Optional
import tos
from tos.exceptions import TosClientError, TosServerError
def get_env(name: str, required: bool = True, default: Optional[str] = None) -> str:
value = os.getenv(name, default)
if required and not value:
print(f"[ERROR] Environment variable {name} is required.", file=sys.stderr)
sys.exit(1)
return value # type: ignore[return-value]
def create_client() -> tos.TosClientV2:
ak = get_env("TOS_ACCESS_KEY")
sk = get_env("TOS_SECRET_KEY")
endpoint = get_env("TOS_ENDPOINT")
region = get_env("TOS_REGION")
security_token = os.getenv("TOS_SECURITY_TOKEN")
return tos.TosClientV2(
ak=ak,
sk=sk,
endpoint=endpoint,
region=region,
security_token=security_token,
)
def build_process_rule(args: argparse.Namespace) -> str:
parts: list[str] = []
if args.time is not None:
parts.append(f"t_{args.time}")
if args.width is not None:
parts.append(f"w_{args.width}")
if args.height is not None:
parts.append(f"h_{args.height}")
if args.mode:
parts.append(f"m_{args.mode}")
if args.output_format:
parts.append(f"f_{args.output_format}")
if args.auto_rotate:
parts.append(f"ar_{args.auto_rotate}")
if parts:
return "video/snapshot," + ",".join(parts)
return "video/snapshot"
def main() -> None:
parser = argparse.ArgumentParser(
description="Take a single video snapshot using the TOS Python SDK",
)
parser.add_argument("--bucket", type=str, default=None, help="Override TOS_BUCKET")
parser.add_argument("--key", type=str, default=None, help="Override TOS_OBJECT_KEY")
parser.add_argument("--time", type=int, help="Snapshot time in milliseconds")
parser.add_argument("--width", type=int, help="Snapshot width in pixels")
parser.add_argument("--height", type=int, help="Snapshot height in pixels")
parser.add_argument(
"--mode",
type=str,
choices=["fast"],
help="Snapshot mode: fast or precise (default precise)",
)
parser.add_argument(
"--output-format",
dest="output_format",
choices=["jpg", "png"],
help="Output image format",
)
parser.add_argument(
"--output",
type=str,
default=None,
help="Local output file (default: snapshot_<time>ms.<fmt>)",
)
parser.add_argument(
"--saveas-bucket",
type=str,
help="If set, persist snapshot to this bucket instead of saving locally",
)
parser.add_argument(
"--saveas-object",
type=str,
help="If set, persist snapshot as this object key instead of saving locally",
)
parser.add_argument(
"--auto-rotate",
dest="auto_rotate",
choices=["auto", "w", "h"],
help="Auto rotate mode",
)
parser.add_argument("--json", action="store_true", help="Print machine-readable JSON only")
parser.add_argument("--dry-run", action="store_true", help="Print resolved request and exit")
args = parser.parse_args()
bucket = args.bucket or get_env("TOS_BUCKET")
key = args.key or get_env("TOS_OBJECT_KEY")
process_value = build_process_rule(args)
saveas_bucket = args.saveas_bucket or os.getenv("TOS_SAVEAS_BUCKET")
saveas_object = args.saveas_object or os.getenv("TOS_SAVEAS_OBJECT")
persist_to_tos = bool(saveas_bucket or saveas_object)
time_part: Any = args.time if args.time is not None else "0"
fmt = args.output_format or "jpg"
if persist_to_tos:
resolved_bucket = saveas_bucket or bucket
resolved_object = saveas_object or f"snapshot_{time_part}ms.{fmt}"
resolved_output = None
else:
resolved_bucket = None
resolved_object = None
resolved_output = args.output or f"snapshot_{time_part}ms.{fmt}"
if args.dry_run:
payload = {
"ok": True,
"operation": "video_snapshot",
"bucket": bucket,
"key": key,
"process": process_value,
"mode": "save_to_tos" if persist_to_tos else "save_local",
"output_path": resolved_output,
"saveas_bucket": resolved_bucket,
"saveas_object": resolved_object,
}
if args.json:
print(json.dumps(payload, ensure_ascii=False))
else:
print(json.dumps(payload, indent=2, ensure_ascii=False))
return
client = create_client()
if persist_to_tos:
if not args.json:
print(
f"[INFO] Requesting snapshot for {bucket}/{key} -> {resolved_bucket}/{resolved_object}",
)
print(f"[INFO] process = {process_value}")
try:
encoded_bucket = base64.urlsafe_b64encode(resolved_bucket.encode()).decode()
encoded_object = base64.urlsafe_b64encode(resolved_object.encode()).decode()
output = client.get_object(
bucket=bucket,
key=key,
process=process_value,
save_bucket=encoded_bucket,
save_object=encoded_object,
)
raw = output.read()
except TosServerError as e:
print(
f"[ERROR] TOS server error: code={e.code}, status={e.status_code}, "
f"request_id={e.request_id}, message={e.message}",
file=sys.stderr,
)
sys.exit(1)
except TosClientError as e:
print(f"[ERROR] TOS client error: {e}", file=sys.stderr)
sys.exit(1)
except Exception as exc: # noqa: BLE001
print(f"[ERROR] Unexpected error: {exc}", file=sys.stderr)
sys.exit(1)
try:
data = json.loads(raw.decode("utf-8"))
except Exception as exc: # noqa: BLE001
print(
"[ERROR] Failed to parse snapshot save result as JSON:", file=sys.stderr
)
print(exc, file=sys.stderr)
print(raw[:200], file=sys.stderr)
sys.exit(1)
if args.json:
print(
json.dumps(
{
"ok": True,
"operation": "video_snapshot",
"bucket": bucket,
"key": key,
"process": process_value,
"mode": "save_to_tos",
"saveas_bucket": resolved_bucket,
"saveas_object": resolved_object,
"result": data,
},
ensure_ascii=False,
)
)
return
print("[OK] Snapshot saved to TOS:")
print(json.dumps(data, indent=2, ensure_ascii=False))
return
if not args.json:
print(f"[INFO] Requesting snapshot for {bucket}/{key} -> {resolved_output}")
print(f"[INFO] process = {process_value}")
try:
client.get_object_to_file(
bucket=bucket,
key=key,
file_path=resolved_output,
process=process_value,
)
except TosServerError as e:
print(
f"[ERROR] TOS server error: code={e.code}, status={e.status_code}, "
f"request_id={e.request_id}, message={e.message}",
file=sys.stderr,
)
sys.exit(1)
except TosClientError as e:
print(f"[ERROR] TOS client error: {e}", file=sys.stderr)
sys.exit(1)
except Exception as exc: # noqa: BLE001
print(f"[ERROR] Unexpected error: {exc}", file=sys.stderr)
sys.exit(1)
try:
size = os.path.getsize(resolved_output)
except OSError:
size = -1
if args.json:
print(
json.dumps(
{
"ok": True,
"operation": "video_snapshot",
"bucket": bucket,
"key": key,
"process": process_value,
"mode": "save_local",
"output_path": resolved_output,
"size": size,
},
ensure_ascii=False,
)
)
return
print(f"[OK] Snapshot saved to {resolved_output} ({size} bytes)")
if __name__ == "__main__":
main()
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#!/usr/bin/env python3
"""Example script: take multiple snapshots from a TOS video using the Python SDK.
Two modes are supported:
- Explicit timestamps: --timestamps 1000 5000 10000
- Interval-based: --interval-ms 5000 --duration-ms 60000 [--max-snapshots N]
Snapshots can be saved locally or directly to TOS using save-as parameters.
"""
import argparse
import base64
import json
import os
import sys
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import List, Optional
import tos
from tos.exceptions import TosClientError, TosServerError
def get_env(name: str, required: bool = True, default: Optional[str] = None) -> str:
value = os.getenv(name, default)
if required and not value:
print(f"[ERROR] Environment variable {name} is required.", file=sys.stderr)
sys.exit(1)
return value # type: ignore[return-value]
def create_client() -> tos.TosClientV2:
ak = get_env("TOS_ACCESS_KEY")
sk = get_env("TOS_SECRET_KEY")
endpoint = get_env("TOS_ENDPOINT")
region = get_env("TOS_REGION")
security_token = os.getenv("TOS_SECURITY_TOKEN")
return tos.TosClientV2(
ak=ak,
sk=sk,
endpoint=endpoint,
region=region,
security_token=security_token,
)
def build_timestamps(args: argparse.Namespace) -> List[int]:
if args.timestamps:
return [int(t) for t in args.timestamps]
if args.interval_ms and args.duration_ms:
interval = int(args.interval_ms)
duration = int(args.duration_ms)
if interval <= 0 or duration <= 0:
print(
"[ERROR] interval-ms and duration-ms must be positive.", file=sys.stderr
)
sys.exit(1)
max_snaps = int(args.max_snapshots) if args.max_snapshots else None
timestamps: List[int] = []
current = interval
while current < duration:
timestamps.append(current)
if max_snaps is not None and len(timestamps) >= max_snaps:
break
current += interval
return timestamps
print(
"[ERROR] Either --timestamps or (--interval-ms and --duration-ms) must be provided.",
file=sys.stderr,
)
sys.exit(1)
def build_process_value(
ts: int, width: Optional[int], height: Optional[int], fmt: Optional[str]
) -> str:
parts = [f"t_{ts}"]
if width is not None:
parts.append(f"w_{width}")
if height is not None:
parts.append(f"h_{height}")
if fmt:
parts.append(f"f_{fmt}")
return "video/snapshot," + ",".join(parts)
def do_snapshot(
ts: int,
client: tos.TosClientV2,
bucket: str,
key: str,
width: Optional[int],
height: Optional[int],
fmt: Optional[str],
save_to_tos: bool,
saveas_bucket: Optional[str],
saveas_prefix: Optional[str],
output_dir: str,
) -> dict:
process_value = build_process_value(ts, width, height, fmt)
if save_to_tos:
save_bucket = saveas_bucket or bucket
prefix = (saveas_prefix or "snapshots/").rstrip("/")
save_object = f"{prefix}/frame_{ts}ms.{fmt or 'jpg'}"
encoded_bucket = base64.urlsafe_b64encode(save_bucket.encode()).decode()
encoded_object = base64.urlsafe_b64encode(save_object.encode()).decode()
try:
output = client.get_object(
bucket=bucket,
key=key,
process=process_value,
save_bucket=encoded_bucket,
save_object=encoded_object,
)
raw = output.read()
data = json.loads(raw.decode("utf-8"))
return {
"ok": True,
"timestamp_ms": ts,
"process": process_value,
"mode": "save_to_tos",
"saveas_bucket": save_bucket,
"saveas_object": save_object,
"result": data,
}
except Exception as exc: # noqa: BLE001
return {
"ok": False,
"timestamp_ms": ts,
"process": process_value,
"mode": "save_to_tos",
"error": str(exc),
}
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"snapshot_{ts}ms.{fmt or 'jpg'}")
try:
client.get_object_to_file(
bucket=bucket,
key=key,
file_path=output_path,
process=process_value,
)
try:
size = os.path.getsize(output_path)
except OSError:
size = -1
return {
"ok": True,
"timestamp_ms": ts,
"process": process_value,
"mode": "save_local",
"output_path": output_path,
"size": size,
}
except Exception as exc: # noqa: BLE001
return {
"ok": False,
"timestamp_ms": ts,
"process": process_value,
"mode": "save_local",
"output_path": output_path,
"error": str(exc),
}
def main() -> None:
parser = argparse.ArgumentParser(
description="Take multiple video snapshots using the TOS Python SDK",
)
parser.add_argument("--bucket", type=str, default=None, help="Override TOS_BUCKET")
parser.add_argument("--key", type=str, default=None, help="Override TOS_OBJECT_KEY")
parser.add_argument(
"--timestamps",
nargs="*",
help="Explicit timestamps in ms, e.g. 1000 5000 10000",
)
parser.add_argument(
"--interval-ms", type=int, help="Interval in ms between snapshots"
)
parser.add_argument(
"--duration-ms", type=int, help="Total video duration in ms for interval mode"
)
parser.add_argument(
"--max-snapshots", type=int, help="Maximum number of snapshots in interval mode"
)
parser.add_argument("--width", type=int, help="Snapshot width in pixels")
parser.add_argument("--height", type=int, help="Snapshot height in pixels")
parser.add_argument(
"--format",
choices=["jpg", "png"],
default="jpg",
help="Snapshot image format",
)
parser.add_argument(
"--output-dir",
dest="output_dir",
default="snapshots",
help="Deprecated alias of --output",
)
parser.add_argument(
"--output",
dest="output",
default=None,
help="Local directory to store snapshots",
)
parser.add_argument(
"--concurrency",
type=int,
default=4,
help="Number of concurrent requests",
)
parser.add_argument("--save-to-tos", action="store_true", help="Deprecated flag; use --saveas-bucket/--saveas-object")
parser.add_argument("--saveas-bucket", type=str, default=None, help="Save snapshots directly to this TOS bucket")
parser.add_argument("--saveas-object", type=str, default=None, help="Save snapshots directly to this TOS key prefix")
parser.add_argument("--json", action="store_true", help="Print machine-readable JSON only")
parser.add_argument("--dry-run", action="store_true", help="Print resolved request and exit")
args = parser.parse_args()
timestamps = build_timestamps(args)
bucket = args.bucket or get_env("TOS_BUCKET")
key = args.key or get_env("TOS_OBJECT_KEY")
output_dir = args.output or args.output_dir
saveas_bucket = args.saveas_bucket or os.getenv("TOS_SAVEAS_BUCKET")
saveas_prefix = args.saveas_object or os.getenv("TOS_SAVEAS_OBJECT_PREFIX")
save_to_tos = args.save_to_tos or bool(saveas_bucket or saveas_prefix)
plan = {
"ok": True,
"operation": "video_snapshots",
"bucket": bucket,
"key": key,
"timestamps": timestamps,
"format": args.format,
"concurrency": args.concurrency,
"mode": "save_to_tos" if save_to_tos else "save_local",
"output": None if save_to_tos else output_dir,
"saveas_bucket": saveas_bucket if save_to_tos else None,
"saveas_object": saveas_prefix if save_to_tos else None,
}
if args.dry_run:
if args.json:
print(json.dumps(plan, ensure_ascii=False))
else:
print(json.dumps(plan, indent=2, ensure_ascii=False))
return
client = create_client()
if not args.json:
print(f"[INFO] Planning snapshots at timestamps (ms): {timestamps}")
print(f"[INFO] Concurrency: {args.concurrency}, save_to_tos={save_to_tos}")
results = []
with ThreadPoolExecutor(max_workers=args.concurrency) as executor:
futures = [
executor.submit(
do_snapshot,
ts,
client,
bucket,
key,
args.width,
args.height,
args.format,
save_to_tos,
saveas_bucket,
saveas_prefix,
output_dir,
)
for ts in timestamps
]
for future in as_completed(futures):
result = future.result()
results.append(result)
if not args.json:
if result.get("ok"):
if result["mode"] == "save_to_tos":
print(f"[OK] ts={result['timestamp_ms']}ms saved to TOS: {result['saveas_bucket']}/{result['saveas_object']}")
else:
print(f"[OK] ts={result['timestamp_ms']}ms saved locally to {result['output_path']} ({result['size']} bytes)")
else:
print(f"[ERROR] ts={result['timestamp_ms']}ms failed: {result['error']}")
if args.json:
print(
json.dumps(
{
**plan,
"results": sorted(results, key=lambda item: item["timestamp_ms"]),
},
ensure_ascii=False,
)
)
if __name__ == "__main__":
main()
Bytedance TOS Video Process Workflows
This document illustrates common workflows for using the TOS Video Process skill with the Volcengine TOS Python SDK. These examples demonstrate how to combine environment setup, SDK calls, and result handling for practical use cases.
Table of Contents
- Workflow 1: Get Video Information
- Workflow 2: Take a Single Snapshot and Save Locally
- Workflow 3: Take a Single Snapshot and Save to TOS
- Workflow 4: Batch Snapshotting with a Time Interval
- Workflow 5: Handling Errors
- Workflow 6: Concatenating Video Clips
---
Prerequisite: Client Initialization
All workflows assume you have a tos.TosClientV2 instance initialized as shown below. See README.md for details on environment variables.
import os
import tos
from tos.exceptions import TosClientError, TosServerError
def create_client() -> tos.TosClientV2:
"""Initializes a TosClientV2 from environment variables."""
ak = os.getenv('TOS_ACCESS_KEY')
sk = os.getenv('TOS_SECRET_KEY')
endpoint = os.getenv('TOS_ENDPOINT')
region = os.getenv('TOS_REGION')
security_token = os.getenv('TOS_SECURITY_TOKEN')
if not all([ak, sk, endpoint, region]):
raise ValueError("Missing required environment variables for TOS client.")
return tos.TosClientV2(
ak=ak,
sk=sk,
endpoint=endpoint,
region=region,
security_token=security_token,
)
client = create_client()
bucket_name = os.getenv("TOS_BUCKET")
object_key = os.getenv("TOS_OBJECT_KEY")---
Workflow 1: Get Video Information
Goal: Retrieve the format and stream information for a video file.
Script: scripts/video_info.py
python3 scripts/video_info.py --key path/to/video.mp4 --jsonBehavior Notes:
--bucket/--keyoverride environment defaults.--jsonreturns a machine-readable payload withoperation,bucket,key,process, andresult.- Without
--json, the script prints formatted JSON for interactive use.
---
Workflow 6: Concatenating Video Clips
Goal: Merge multiple video clips stored in TOS into a single output video.
Script: scripts/video_concat.py
1. Set Environment: (Same as Workflow 1)
2. Execute:
python3 scripts/video_concat.py \
--key clip1.mp4 \
--fragments "clip2.mp4,clip3.mp4" \
--saveas-object output/merged.mp4 \
--wait3. Behavior Notes:
--keyis the first video clip.--fragmentsspecifies additional clips to append, comma-separated.- The script submits an async
Concatjob via the TOS gateway'smedia_jobsAPI. - Use
--validate/--dry-runto inspect the resolved payload before submission. - Use
--job-id <id>to query an existing concat job. - Use
--jsonfor machine-readable output. - Use
--waitto poll until the job completes. Without--wait, the script returns the created job payload. - Default output format is
mp4withh264video codec andaacaudio codec. Override with--format,--video-codec, and--audio-codec. - A single clip (no
--fragments) effectively re-encodes the input video.
---
Workflow 2: Take a Single Snapshot and Save Locally
Goal: Capture a single frame at a specific timestamp and save it as a local image file.
Steps: 1. Define the snapshot parameters (time, dimensions, format). 2. Construct the process string (e.g., "video/snapshot,t_10000,w_1280,f_jpg"). 3. Call client.get_object_to_file() with the process string and a local file path.
Python Example (`scripts/video_snapshot.py`):
# Assumes 'client', 'bucket_name', 'object_key' are initialized
time_ms = 10000 # Snapshot at 10 seconds
output_filename = f"snapshot_{time_ms}ms.jpg"
process_rule = f"video/snapshot,t_{time_ms},w_1280,f_jpg"
print(f"Requesting snapshot at {time_ms}ms...")
try:
client.get_object_to_file(
bucket=bucket_name,
key=object_key,
file_path=output_filename,
process=process_rule
)
print(f"Snapshot successfully saved to {output_filename}")
except (TosClientError, TosServerError) as e:
print(f"An error occurred: {e}")---
Workflow 3: Take a Single Snapshot and Save to TOS
Goal: Capture a single frame and have the TOS server save it directly to another object in the same or a different bucket.
Script: scripts/video_snapshot.py
python3 scripts/video_snapshot.py \
--key path/to/video.mp4 \
--time 15000 \
--saveas-bucket my-output-bucket \
--saveas-object snapshots/frame_15000ms.jpg \
--jsonBehavior Notes:
--saveas-bucket/--saveas-objectswitch the script into TOS-to-TOS save mode.--dry-runprints the resolved request without calling TOS.--jsonreturns a structured payload including save target and TOS response.
---
Workflow 4: Batch Snapshotting with a Time Interval
Goal: Generate multiple snapshots every N milliseconds of a video and save them locally or to TOS.
Script: scripts/video_snapshots.py
Save to TOS:
python3 scripts/video_snapshots.py \
--key test.mp4 \
--timestamps 1000 3000 5000 \
--saveas-bucket my-output-bucket \
--saveas-object snapshots \
--jsonGenerate by interval and save locally:
python3 scripts/video_snapshots.py \
--key test.mp4 \
--interval-ms 5000 \
--duration-ms 60000 \
--output snapshotsThe shipped script additionally supports:
--timestamps 1000 3000 5000for explicit sampling--interval-ms ... --duration-ms ...for generated sampling--bucket/--keyoverrides for easier test execution--outputfor local batch save, with--output-dirretained as a deprecated alias--saveas-bucket/--saveas-objectfor TOS persistence--jsonfor machine-readable output and--dry-runfor request preview
---
Workflow 5: Handling Errors
Goal: Gracefully handle common errors like invalid credentials, missing files, or server-side issues when using the SDK.
Error Handling Strategy:
- Wrap all SDK calls in
try...exceptblocks. - Catch
tos.exceptions.TosServerErrorfor API errors returned by the TOS service. The exception object contains.code,.status_code,.message, and.request_id. - Catch
tos.exceptions.TosClientErrorfor client-side issues like network problems or invalid input. - Check for missing environment variables before initializing the client.
Python Example:
# (Assumes 'client' is initialized)
try:
# An example call that might fail
response = client.get_object(
bucket="non-existent-bucket",
key="non-existent-key",
process="video/info"
)
# ... process successful response
except TosServerError as e:
print(f"TOS Server Error occurred:")
print(f" - Status Code: {e.status_code}")
print(f" - Error Code: {e.code}")
print(f" - Message: {e.message}")
print(f" - Request ID: {e.request_id}")
except TosClientError as e:
print(f"TOS Client Error: {e.message}")
except Exception as e:
print(f"An unexpected error occurred: {e}")