What is TS File Format and How to Play TS Videos?

EdgeOne-Dev Team
Dec 5, 2024

What is TS File Format and How to Play TS Videos?
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.

transport stream syntax diagram
transport stream syntax diagram

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:

  1. PES start code: Fixed at 0x000001.
  2. Stream ID: For audio, the value is between 0xc0-0xdf, usually 0xc0. For video, the value is between 0xe0-0xef, usually 0xe0.
  3. 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.
  4. PES data length: The length of the data that follows, with a value of 5 or 10.
  5. PTS: A 33-bit value indicating the Presentation Time Stamp.
  6. 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:

  1. 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.
  2. 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.
  3. Synchronization: The TS format includes timing information, ensuring that the audio and video streams can be correctly synchronized on playback.
  4. 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.
  5. 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 Download TS Files?

Downloading TS (Transport Stream) files, which are often used in streaming protocols like HLS (HTTP Live Streaming), involves fetching the individual TS segments and optionally combining them into a single video file. Here are several methods to download TS files:

Method 1: Using FFmpeg

FFmpeg is a powerful multimedia framework that can handle various video and audio formats, including TS files.

1. Install FFmpeg

  • Windows: Download the FFmpeg executable from the official website and add it to your system PATH.
  • Mac: Use Homebrew to install FFmpeg: brew install ffmpeg.
  • Linux: Use your package manager, e.g., sudo apt-get install ffmpeg for Debian-based systems.

2. Download and Combine TS Files

Open a terminal or command prompt and run the following command:

ffmpeg -i "http://example.com/path/to/playlist.m3u8" -c copy output.mp4

Replace http://example.com/path/to/playlist.m3u8 with the URL of the M3U8 file that references the TS segments, and output.mp4 with your desired output file name.

Method 2: Using a Browser Extension

There are several browser extensions available that can help you download TS files directly from your web browser.

1. Install a Browser Extension

  • Chrome: Extensions like "Video DownloadHelper" or "HLS Downloader".
  • Firefox: Extensions like "Video DownloadHelper" or "HLS Downloader".

2. Download the TS Files

  • Navigate to the webpage containing the TS files or the M3U8 playlist.
  • Use the extension to detect and download the TS files. Follow the extension's instructions to save the files to your computer.

Method 3: Using Command-Line Tools

There are command-line tools specifically designed for downloading TS files.

1. Install a Tool

youtube-dl: A popular command-line program that can download videos from various websites, including those using M3U8 and TS files. Follow the instructions on the official website to install youtube-dl.

2. Download the TS Files

Open a terminal or command prompt and run the following command:

youtube-dl "http://example.com/path/to/playlist.m3u8"

Replace http://example.com/path/to/playlist.m3u8 with the URL of the M3U8 file.

Method 4: Manually Downloading and Combining TS Files

If you have direct URLs to the TS files, you can download them manually and combine them.

1. Download TS Files

Use a download manager or your browser to download each TS file individually.

2. Combine TS Files

Use FFmpeg to combine the downloaded TS files into a single video file. Create a text file (e.g., filelist.txt) with the following content:

file 'segment1.ts'
file 'segment2.ts'
file 'segment3.ts'
...

Then run the following command:

ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp4

Replace filelist.txt with the name of your text file and output.mp4 with your desired output file name.

Important Considerations

  • Legal Issues: Ensure you have the right to download the content. Downloading copyrighted material without permission is illegal.
  • Quality: The quality of the downloaded video depends on the quality of the TS segments.
  • Dependencies: Some methods may require additional software or dependencies to be installed on your system.

By following these methods, you can download TS files and optionally combine them into a single video file on your computer.

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.

FAQs

1. What is a TS file?

A TS (Transport Stream) file is a standard format for transmitting audio, video, and data over digital broadcast systems such as DVB, ATSC, and IPTV. It is commonly used for streaming and broadcasting purposes.

2. What are the common uses of TS files?

TS files are primarily used for:

  • Broadcasting digital television and radio.
  • Streaming video over the internet (e.g., HLS).
  • Storing video on DVDs.

3. How can I open a TS file?

You can open TS files with various media players that support the format, such as:

  • VLC Media Player
  • Windows Media Player (with appropriate codecs)
  • KMPlayer
  • GOM Player
  • PotPlayer

4. How do I convert a TS file to another format?

You can use video conversion software to convert TS files to other formats like MP4, AVI, or MKV. Popular tools include:

  • FFmpeg: A command-line tool for converting multimedia files.
ffmpeg -i input.ts -c:v libx264 -c:a aac output.mp4
  • HandBrake: A user-friendly video converter.
  • Any Video Converter: A versatile video conversion tool.

5. Why are TS files used in streaming?

TS files are used in streaming because they are designed to handle errors and packet loss, making them suitable for unreliable networks. They also support real-time data transmission, which is essential for live streaming.

6. What is the difference between TS and MP4?

  • TS: Designed for streaming and broadcasting, supports error correction, and is suitable for real-time transmission.
  • MP4: A multimedia container format used for storing video, audio, and other data. It is widely supported and used for offline playback.

7. Can I edit TS files?

Yes, you can edit TS files using video editing software that supports the format, such as:

  • Adobe Premiere Pro
  • Avidemux
  • FFmpeg (for basic editing tasks)

8. How do I merge multiple TS files?

You can merge multiple TS files using FFmpeg. Create a text file (e.g., filelist.txt) with the following content:

file 'segment1.ts'
file 'segment2.ts'
file 'segment3.ts'
...

Then run the following command:

ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.ts

Replace filelist.txt with the name of your text file and output.ts with your desired output file name.

9. Are TS files compressed?

TS files can contain compressed video and audio streams, but the TS format itself is not a compression format. It is a container that can hold various types of compressed data.

10. What is the file extension for TS files?

The file extension for Transport Stream files is .ts.

11. How do I play TS files on my TV?

You can play TS files on your TV by:

  • Using a media player that supports TS files and connecting it to your TV.
  • Converting the TS files to a format supported by your TV (e.g., MP4) and playing them via USB or network.

12. What is the difference between TS and M2TS?

  • TS: A standard format for transmitting audio, video, and data over digital broadcast systems.
  • M2TS: A variant of the TS format used for Blu-ray Disc Audio-Video (BDAV) and AVCHD. It includes additional information for Blu-ray and AVCHD playback.

13. Can I stream TS files over the internet?

Yes, TS files are commonly used for streaming over the internet, especially with protocols like HLS (HTTP Live Streaming).

14. What is the structure of a TS file?

A TS file consists of packets, each typically 188 bytes in size. These packets contain headers and payloads, which include video, audio, and data streams.

15. How do I troubleshoot playback issues with TS files?

  • Check the file integrity: Ensure the TS file is not corrupted.
  • Update codecs: Make sure you have the necessary codecs installed.
  • Use a different player: Try using a different media player that supports TS files.
  • Convert the file: If playback issues persist, consider converting the TS file to another format.