CMAF (Common Media Application Format) is a media delivery format that enables efficient HTTP-based streaming of audio and video content.
It standardizes media segments for both MPEG-DASH and HLS streaming protocols, allowing content providers to create a single set of media segments that work with both delivery methods.
Uses fragmented MP4 (fMP4) container format
Supports efficient encoding with independent segments
Enables low-latency streaming through chunked encoding and transfer
Reduces storage requirements by using a single media format for multiple streaming protocols
This guide will walk you through creating CMAF-compliant templates for generating adaptive bitrate (ABR) streams for HLS and DASH.
As explained above, by using CMAF, you can create a single set of media segments that are compatible with both HLS and DASH streaming protocols, significantly reducing storage requirements and simplifying your media delivery workflow.
To create a CMAF template, follow these steps to configure the required parameters:
Template Name (template_name
):
"cmaf_abr_template"
.Output Format (format
):
"cmaf"
to comply with the CMAF standard.Segment Type (seg_type
):
"fmp4"
, as fragmented MP4 is a core component of CMAF.Segment Duration (seg_time
):
Duration of each segment in seconds. The recommended default value is 10
.
adaptive_streams
)Parameter Explanation:
vcodec
: Video codec format (e.g., "h264"
).
resolution
: Video resolution (e.g., "720p"
, "480p"
).
quality
: Video quality score (e.g., 5
for high quality, 4.5
for medium quality).
audio_bitrate
: Audio bitrate (e.g., 64000
bps).
Example Configuration:
[{"vcodec": "h264","resolution": "720p","video_bitrate": 2000000,"audio_bitrate": 64000},{"vcodec": "h264","resolution": "480p","video_bitrate": 2000000,"audio_bitrate": 64000}]
Audio Codec (acodec
):
"aac"
(default value) for best compatibility.Frame Rate (framerate
):
25
) to improve playback smoothness.GOP Size (gop_size
):
10
.Template Description (description
):
"CMAF ABR Streaming Template"
.Below is a complete Python example showing how to configure and send a request to create a CMAF template:
import requestsfrom typing import Optional, Dict, Any, Listdef create_template(api_key: str,api_secret: str,template_name: str,adaptive_streams: List[Dict[str, Any]],format: str = "cmaf",acodec: str = "aac",seg_time: int = 10,seg_type: str = "fmp4",framerate: Optional[int] = None,gop_size: Optional[int] = None,description: Optional[str] = None,**kwargs) -> bool:"""Create a streaming template with customizable parametersArgs:api_key: Access Key ID for API authenticationapi_secret: Access Key Secret for API authenticationtemplate_name: Name of the templateadaptive_streams: List of dictionaries containing stream configurationsformat: Output format (default: cmaf)acodec: Audio codec (default: aac)seg_time: Segment duration in seconds (default: 10)seg_type: Segment type (default: fmp4)framerate: Output frame rategop_size: GOP size in secondsdescription: Optional description for the template**kwargs: Additional optional parameters for the templateReturns:bool: True if creation successful, False otherwise"""url = "https://api.visionular.com/vodencoding/v1/add_template"headers = {"Content-Type": "application/json","auth-type": "use-basic"}payload = {"template_name": template_name,"format": format,"acodec": acodec,"seg_time": seg_time,"seg_type": seg_type,"adaptive_streams": adaptive_streams,**kwargs}if framerate:payload["framerate"] = framerateif gop_size:payload["gop_size"] = gop_size# Add description if providedif description:payload["description"] = descriptiontry:print("Sending request:", payload)response = requests.post(url,headers=headers,auth=(api_key, api_secret),json=payload,verify=True)# Print full response for debuggingprint("Response status:", response.status_code)print("Response headers:", response.headers)print("Response content:", response.text)response.raise_for_status()result = response.json()if result["code"] == 0:print(f"Template '{template_name}' created successfully!")return Trueelse:print(f"Template creation failed: {result['msg']}")return Falseexcept requests.exceptions.HTTPError as e:print(f"HTTP Error: {e}")print(f"Response content: {e.response.text if e.response else 'No response'}")return Falseexcept Exception as e:print(f"Unexpected error: {e}")return False# Example usageif __name__ == "__main__":api_key = "54F6A8740DEE4710BD4C6CDD17F260ED"api_secret = "69CB474C4F954BAF878BD4A59BC40461"template_name = "cmaf_abr_api"adaptive_streams = [{"vcodec": "h264","resolution": "720p","video_bitrate": 2000000,"audio_bitrate": 64000},{"vcodec": "h264","resolution": "480p","video_bitrate": 2000000,"audio_bitrate": 64000}]# Create the CMAF templateif create_template(api_key=api_key,api_secret=api_secret,template_name=template_name,adaptive_streams=adaptive_streams,format="cmaf",framerate=25,gop_size=10,description="cmaf_abr_api"):print("CMAF template created successfully!")else:print("Failed to create CMAF template.")