What is TS File Format and How to Play TS Videos?
Many people may not be familiar with the TS file format, as it is not as popular as the MP4 format. TS is often found in M3U8 video files, a format widely used in online video playback and live streaming, and serves as the carrier for a series of small video fragments. If you are looking to transcode, distribute, and host TS videos, you might want to consider our services.
To help you better understand TS videos, this article will provide detailed information about the origin of the TS video format, its applications, how to open it, and how to edit and convert it.
What is a TS Video?
A TS file, short for Transport Stream, is a type of video file format used for storing, transmitting, and playing digital video and audio streams. TS is an encapsulated format, also known as MPEG2-TS. MPEG2-TS is a standard data container format for transmitting and storing audio and video, as well as program and system information protocol data. It is mainly used in digital broadcasting systems, such as DVB, ATSC, and IPTV. The transport stream was originally designed for broadcasting. Later, by adding a 4-byte time code (TC) to the standard 188-byte data packet, making it a 192-byte data packet, it was adapted for use with digital cameras, video recorders, and players.
MPEG2 is a multimedia transmission and multiplexing technology that can provide hundreds of program channels in digital TV broadcasting. Multiplexing means that multiple layers of programs can be transmitted simultaneously. In the MPEG-2 standard, there are two different types of bitstreams output to the channel: one is the program stream (PS), which is suitable for scenarios without transmission errors, and the other is the transport stream (TS), which is suitable for scenarios with channel noise.
The program stream is used for stable and reliable media, such as optical discs (e.g. DVD/CD), while the transport stream is used for unstable transmission, such as terrestrial or satellite broadcasting. In addition, the transport stream can transmit multiple programs, and they do not have a hierarchical relationship. Logically, they are all composed of PES (Packetized Elementary Stream) packets, so they can be easily converted between each other.
What is TS File Format?
TS videos (streams) can be divided into three layers: the TS layer (Transport Stream), the PES layer (Packetized Elementary Stream), and the ES layer (Elementary Stream).
The ES layer contains the audio and video data, the PES layer adds information about the data frames, such as time stamps, to the audio and video data, and the TS layer adds necessary information for data stream identification and transmission to the PES layer. A TS video (stream) is composed of multiple TS packets.
TS Layer
The size of a TS packet is fixed at 188 bytes. The TS layer is divided into three parts: TS header, Adaptation Field, and Payload. The TS header is fixed at 4 bytes. The Adaptation Field may or may not exist and is mainly used to fill in data that is less than 188 bytes. The Payload contains the PES data.
1. TS Header
TS packet header provides information about the transmission.
The length of the TS packet header is not fixed. The first 4 bytes are fixed, and there may be an adaptation field following the header. The 4 bytes are the minimum header length.
The structure fields of the packet header are as follows:
- sync_byte (Synchronization byte): Fixed at 0x47. This byte is recognized by the decoder to separate the packet header and payload.
- transport_error_indicator (Transport error flag): '1' indicates that there is at least one uncorrectable error bit in the related transport packet. Once set to 1, it cannot be reset to 0 until the error is corrected.
- payload_unit_start_indicator (Payload start flag): When set to 1, it indicates that the payload of the current TS packet contains the start position of PES or PSI. After the first 4 bytes, there will be an adaptation byte with a value of length for the length of the following adaptation field. Therefore, the position where the payload starts should be offset by 1+[length] bytes.
- transport_priority (Transport priority flag): '1' indicates that the priority of the current TS packet is higher than other TS packets with the same PID that do not have this bit set to '1'.
- PID: Indicates the type of data stored in the grouping effective payload.
- transport_scrambling_control (Scrambling control flag): Indicates the encryption mode of the effective payload of the TS stream grouping. Empty packets are '00'. If the adaptation field is included in the packet header, it should not be encrypted. Other values are user-defined.
- adaptation_field_control (Adaptation field control flag): Indicates whether the packet header has an adaptation field or effective payload. '00' is reserved for future use by ISO/IEC; '01' contains only effective payload without adaptation field; '10' contains only adaptation field without effective payload; '11' effective payload follows the adaptation field, and the first byte in the adaptation field indicates the length of the adaptation field, and the position where the payload starts should be offset by [length] bytes. Empty packets should be '10'.
- continuity_counter (Continuity counter): Increases with each TS stream grouping with the same PID and returns to 0 after reaching the maximum value. The range is 0~15.
2. TS Adaptation Field
The length of the Adaptation Field should include one byte for the Transport Error Indicator flag.
PCR (Program Clock Reference) is a program clock reference. PCR, DTS, and PTS are all sampling values of the same system clock. PCR is incremental, so it can be set to the DTS value, and the audio data does not need PCR.
When packing the TS stream, the PAT and PMT tables do not have an Adaptation Field. If the length is insufficient, it can be filled with 0xff.
Both the video stream and audio stream require an adaptation field, which is usually added to the first and last TS packets of a frame, but not added to the middle TS packets.
3. TS Payload
The information transmitted in the Payload of a TS packet includes two types: PES packets for video and audio, as well as auxiliary data, Program Specific Information (PSI).
TS packets can also be empty packets. Empty packets are used to fill the TS stream and may be inserted or deleted during demultiplexing.
The ES streams of video and audio need to be packaged into PES streams. Auxiliary data (such as teletext information) does not need to be packaged into PES packets.
PES Layer & ES Layer
1. PES Layer
The PES layer adds information such as time stamps to each video/audio frame. The content of a PES packet can be complex, but the most commonly used fields are:
- PES start code: Fixed at 0x000001.
- Stream ID: For audio, the value is between 0xc0-0xdf, usually 0xc0. For video, the value is between 0xe0-0xef, usually 0xe0.
- PES packet length: The length of the PES data that follows. A value of 0 indicates that the length is not limited, but only the video data length may exceed 0xffff.
- PES data length: The length of the data that follows, with a value of 5 or 10.
- PTS: A 33-bit value indicating the Presentation Time Stamp.
- DTS: A 33-bit value indicating the Decoding Time Stamp.
PTS (Presentation Time Stamp) is used for display timing, while DTS (Decoding Time Stamp) is used for decoding timing.
Both video and audio data require both PTS and DTS timestamps. However, for audio data, PTS and DTS are the same, so only PTS is required.
PTS and DTS are both used for B-frames. For I-frames and P-frames, PTS is equal to DTS. If a video stream does not contain B-frames, then PTS will always be equal to DTS.
When reading video frames from a file, the order of the frames is the same as the order of the DTS timestamps. The DTS algorithm is relatively simple, with an initial value plus an increment. The PTS calculation is more complex and requires an offset added to the DTS.
For audio PES, only PTS (same as DTS) is required, while for video I-frames and P-frames, both PTS and DTS timestamps are required. For video B-frames, only PTS (same as DTS) is required.
2. ES Layer
The ES layer refers to the elementary stream layer, which contains the raw audio and video data.
In general, video is encoded in H.264 format, while audio is encoded in AAC format.
Why Use TS File Format?
As a type of container file format, the TS videos have a wide range of applications, including:
1. Broadcasting and television: TS videos are commonly used in broadcasting systems such as DVB and ATSC. The format has error correction and stream synchronization functions, which can maintain transmission integrity when the signal is degraded. The most classic application of TS streams is in digital high-definition television that we use in our daily lives.
2. Real-time streaming media: Apple's HTTP Live Streaming media is based on TS files. In live broadcasting, video streams are sliced or segmented into file fragments for transmission, or played sequentially through given TS file addresses.
3. Blu-ray discs / Full HD videos: As a container format, TS can encapsulate MPEG-2 or VC-1 encodings, and is also a commonly used container format for Blu-ray discs and Full HD videos. Therefore, videos in the TS format usually have high video quality, large file size, and accurate restoration, making it a favorite format among high-definition enthusiasts.
4. Digital cameras: To be used in digital cameras, the TS format grouping has an added 4-byte time code. In addition, with the help of time codes, it can be used to synchronize content shot by multiple cameras.
The TS video format is used for several reasons:
- Error correction: TS videos are designed to handle errors better than other formats. This is crucial for data that's being streamed or broadcast over networks where data loss might occur.
- Multiplexing: TS videos can contain multiple audio and video streams, as well as metadata. This makes them ideal for broadcasting applications where you might need to include multiple languages, subtitles, or additional data services.
- Synchronization: The TS format includes timing information, ensuring that the audio and video streams can be correctly synchronized on playback.
- Compatibility: The TS video format is part of the MPEG-2 standard, which is widely supported in both hardware and software, making it a good choice for broadcasting.
- High Quality: TS videos maintain high-quality video and audio, making them suitable for professional broadcasting and production.
How to Play TS Videos?
Several programs support TS Videos:
- VLC Media Player
- Windows Media Player
- MPC-HC
- KMPlayer
- PotPlayer
Furthermore, we provide a free online M3U8 Player that supports WebRTC, FLV, and HLS live streaming, as well as on-demand playback in HLS, DASH, FLV, and MP4 formats for your convenience. Feel free to use it.
Some TS videos may require additional codecs to play properly. If you encounter any issues playing a TS file, you may need to install additional codecs or use a different media player.
How to Convert TS to MP4?
If you want to play TS video files on your mobile or other portable media player, the best way is to convert them to a more accessible format, such as MP4. While both TS and MP4 formats are used for storing video files and streaming media, they are vastly different in their applicable areas. TS is more suitable for professional streaming media playback, while MP4 is an excellent choice for storing and watching videos on computers and mobile devices. If you want to play TS video files on a mobile device, it's best to convert them to MP4 format for easier access and playback.
Below is a table explaining the salient difference between TS and MP4.
To convert TS to MP4, you can use video converter software. There are many video converter software available, such as HandBrake, Freemake Video Converter, Any Video Converter, and Wondershare UniConverter. The steps to convert TS to MP4 may vary slightly depending on the software you use.
Conclusion
In conclusion, the TS (Transport Stream) format plays a crucial role in the world of digital video broadcasting and streaming. If you have more extensive needs related to video asset management, such as swift uploading, transcoding, storage, and accelerated playback, EdgeOne VOD is the perfect solution.
Tencent EdgeOne VOD (Video on Demand) offers comprehensive VPaaS (Video Platform as a Service) solutions, including cloud video storage, video transcoding, and video playback acceleration, cater to audio/video capture, upload, storage, automated transcoding, accelerated playback, media asset management, and audio/video communications. VOD enables swift and flexible delivery of high-quality videos, allowing you to concentrate on your business, choose services as needed, and adapt to market changes with agility. On the demo page, you can conveniently experience the EdgeOne video services firsthand.
We have now launched a free trial, click here or contact us for more information. And feel free to begin your journey with us.