0% found this document useful (0 votes)
45 views

A Transport Stream

A Transport Stream (TS) is a digital container format used to transmit audio, video, and data in digital broadcasting and communication systems. It packages multiple audio/video streams with metadata and synchronization info. TS is commonly used in TV broadcasting and internet streaming. To read a TS file in Java, one can either manually handle TS packets using Java I/O or use a library like tsparser for easier abstraction. The total size of a TS file can also be calculated in Java by summing the sizes of individual TS packets read from the file input stream.

Uploaded by

Khondwani Banda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

A Transport Stream

A Transport Stream (TS) is a digital container format used to transmit audio, video, and data in digital broadcasting and communication systems. It packages multiple audio/video streams with metadata and synchronization info. TS is commonly used in TV broadcasting and internet streaming. To read a TS file in Java, one can either manually handle TS packets using Java I/O or use a library like tsparser for easier abstraction. The total size of a TS file can also be calculated in Java by summing the sizes of individual TS packets read from the file input stream.

Uploaded by

Khondwani Banda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

A Transport Stream (TS) is a digital container format used for transmitting audio, video, and

data in digital broadcasting and communication systems. It is a standard defined by the MPEG
(Moving Picture Experts Group) and is commonly employed for distributing multimedia
content over various networks, such as cable, satellite, terrestrial, and internet-based streaming
services.
Transport Streams are designed to efficiently package and deliver multiple audio and video
streams along with associated metadata and synchronization information. They are widely used
in television broadcasting (e.g., DVB - Digital Video Broadcasting) and internet streaming
(e.g., HTTP Live Streaming - HLS).
To read a Transport Stream (TS) using Java, you can use the Java I/O classes to open and read
the TS file byte by byte or use higher-level libraries that provide abstractions for reading TS
packets and extracting data. In this example, I'll demonstrate how to read a TS file using the
`java.io` package and handle TS packets manually. Keep in mind that dealing with TS packets
manually can be complex due to its structure, so using a dedicated library might be more
practical for real-world applications.

READING TRANSPORT STREAMS


1. Using Java I/O:

```java
import java.io.FileInputStream;
import java.io.IOException;

public class TSReader {

public static void main(String[] args) {


String tsFilePath = "path/to/your/transport-stream.ts";

try (FileInputStream fis = new FileInputStream(tsFilePath)) {


byte[] packet = new byte[188]; // TS packets are 188 bytes in size

while (fis.read(packet) != -1) {


// Process the TS packet data here
processTSPacket(packet);
}
} catch (IOException e) {
e.printStackTrace();
}
}

private static void processTSPacket(byte[] packet) {


// Implement your logic to handle TS packets here
// You can extract PIDs, payload, adaptation fields, etc.
}
}
```

2. Using a Java TS Library (e.g., tsparser):

There are third-party Java libraries available that provide higher-level abstractions for handling
Transport Streams. One such library is "tsparser," which simplifies the process of reading and
analyzing TS packets.

To use the tsparser library, you can add it as a dependency to your project using a build tool
like Maven or Gradle. Here's a brief example:

```xml
<!-- Add tsparser dependency to your pom.xml if using Maven -->
<dependency>
<groupId>com.github.kokorin</groupId>
<artifactId>tsparser</artifactId>
<version>0.5.0</version>
</dependency>
```

```java
import com.github.kokorin.ts.StreamReader;
import com.github.kokorin.ts.Packet;

import java.io.FileInputStream;
import java.io.IOException;

public class TSReaderUsingLib {

public static void main(String[] args) {


String tsFilePath = "path/to/your/transport-stream.ts";

try (FileInputStream fis = new FileInputStream(tsFilePath)) {


StreamReader reader = new StreamReader(fis);

Packet packet;
while ((packet = reader.nextPacket()) != null) {
// Process the TS packet data here
processTSPacket(packet);
}
} catch (IOException e) {
e.printStackTrace();
}
}

private static void processTSPacket(Packet packet) {


// Implement your logic to handle TS packets here
// You can access packet information like PIDs, payload, adaptation fields, etc.
}
}
```
Using a dedicated library like tsparser can save you time and effort, as it abstracts away many
of the complexities of TS packet handling. Always make sure to check the library's
documentation for usage details and additional features it may provide.

Calculating the total size of the transport stream


To calculate the total size of a Transport Stream (TS) using Java, you can read the TS file and
sum the sizes of all the TS packets. Each TS packet is typically 188 bytes long, and you can
simply add up the sizes of all the packets in the file to get the total size.

Below is an example of how you can calculate the total size of a Transport Stream using Java:

```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TSTotalSizeCalculator {

public static void main(String[] args) {


// Replace "path/to/your/ts/file.ts" with the actual path of your Transport Stream file.
File tsFile = new File("path/to/your/ts/file.ts");

try {
long totalSize = calculateTSTotalSize(tsFile);
System.out.println("Total size of the Transport Stream: " + totalSize + " bytes");
} catch (IOException e) {
e.printStackTrace();
}
}

private static long calculateTSTotalSize(File tsFile) throws IOException {


FileInputStream fileInputStream = new FileInputStream(tsFile);
long totalSize = 0;

// TS packets are typically 188 bytes in size


int tsPacketSize = 188;

byte[] buffer = new byte[tsPacketSize];


int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
totalSize += bytesRead;
}

fileInputStream.close();

return totalSize;
}
}
```

In this example, we use a FileInputStream to read the TS file in chunks of 188 bytes (the size
of a TS packet) and then add up the number of bytes read until we reach the end of the file. The
total size is then returned as the result.

Please make sure to replace "path/to/your/ts/file.ts" with the actual path to your TS file before
running the code.

Keep in mind that this example assumes that the TS file is well-formed and has a valid TS
packet structure. If the file contains errors or is not a proper TS file, the calculated total size
may not be accurate. Additionally, some TS files may have additional metadata or padding,
which might slightly increase the total size.
REFERENCES

1. ExoPlayer GitHub Repository: https://github.com/google/ExoPlayer


2. ExoPlayer API Documentation: https://exoplayer.dev/doc/reference/
3. ExoPlayer Gradle Dependency:
https://mvnrepository.com/artifact/com.google.android.exoplayer/exoplayer-core

You might also like