Skip to content

Instantly share code, notes, and snippets.

@suzp1984
Created November 18, 2024 09:25
Show Gist options
  • Save suzp1984/37a356a3fec4e69531ec10fbcb4a627f to your computer and use it in GitHub Desktop.
Save suzp1984/37a356a3fec4e69531ec10fbcb4a627f to your computer and use it in GitHub Desktop.
mpeg-ts segment file recount countinuity_counter
// 1. go run . input.ts get all ts packet header
// 2. change map pid -> cc, start count ts per pid from m[pid]
// m := map[uint16]uint8{
// 256: 0,
// 257: 3,
// }
// 3. recount cc
// go run . input.ts output.ts
package main
import (
"fmt"
"io"
"os"
)
func print_ts() {
const BUF_SIZE = 188
file, err := os.Open(os.Args[1])
if err != nil {
fmt.Printf("open ts file %s error.\n", os.Args[1])
os.Exit(-2)
}
defer file.Close()
buffer := make([]byte, BUF_SIZE)
for {
bytes_read, err := file.Read(buffer)
if err != nil {
// && err != io.EOF
fmt.Println(err)
fmt.Println("before exit, bytes read ", bytes_read)
// break
if err != io.EOF {
os.Exit(-3)
}
return
}
// fmt.Println("bytes read ", bytes_read)
sync_byte := buffer[0]
transport_error_indicator := uint8(buffer[1] & 0x80 >> 7)
payload_unit_start_indicator := uint8(buffer[1] & 0x40 >> 6)
transport_priority := uint8(buffer[1] & 0x20 >> 5)
pid := uint16(buffer[1]&0x1f)<<8 + uint16(buffer[2])
transport_scrambling_control := uint8(buffer[3] & 0xC0 >> 6)
adaptation_field_control := uint8(buffer[3] & 0x30 >> 4)
continuity_counter := uint8(buffer[3] & 0x0f)
fmt.Printf("sync_byte=%x, transport_error_indicator=%d, payload_unit_start_indicator=%d, transport_priority=%d, pid=%d, transport_scrambling_control=%x, adaptation_field_control=%x, continuity_counter=%d \n",
sync_byte, transport_error_indicator, payload_unit_start_indicator, transport_priority, pid, transport_scrambling_control, adaptation_field_control, continuity_counter)
}
}
func main() {
fmt.Println("args len = ", len(os.Args))
for i, v := range os.Args {
fmt.Printf("args[%d]=%s\n", i, v)
}
if len(os.Args) < 2 {
fmt.Println("os.Args len < 2, [app input_ts output_ts]")
os.Exit(-1)
}
fmt.Println("open ts file: ", os.Args[1])
if len(os.Args) == 2 {
print_ts()
return
}
fmt.Println("open output file; ", os.Args[2])
const BUF_SIZE = 188
file, err := os.Open(os.Args[1])
if err != nil {
fmt.Printf("open ts file %s error.\n", os.Args[1])
os.Exit(-2)
}
defer file.Close()
output_file, err := os.Create(os.Args[2])
if err != nil {
fmt.Printf("open output file error: %s\n", err)
os.Exit(-2)
}
defer output_file.Close()
// pid -> cc
m := map[uint16]uint8{
256: 0,
257: 3,
}
buffer := make([]byte, BUF_SIZE)
for {
bytes_read, err := file.Read(buffer)
if err != nil {
// && err != io.EOF
fmt.Println(err)
fmt.Println("before exit, bytes read ", bytes_read)
// break
if err != io.EOF {
os.Exit(-3)
}
return
}
// fmt.Println("bytes read ", bytes_read)
sync_byte := buffer[0]
transport_error_indicator := uint8(buffer[1] & 0x80 >> 7)
payload_unit_start_indicator := uint8(buffer[1] & 0x40 >> 6)
transport_priority := uint8(buffer[1] & 0x20 >> 5)
pid := uint16(buffer[1]&0x1f)<<8 + uint16(buffer[2])
transport_scrambling_control := uint8(buffer[3] & 0xC0 >> 6)
adaptation_field_control := uint8(buffer[3] & 0x30 >> 4)
continuity_counter := uint8(buffer[3] & 0x0f)
fmt.Printf("sync_byte=%x, transport_error_indicator=%d, payload_unit_start_indicator=%d, transport_priority=%d, pid=%d, transport_scrambling_control=%x, adaptation_field_control=%x, continuity_counter=%d \n", sync_byte, transport_error_indicator, payload_unit_start_indicator, transport_priority, pid, transport_scrambling_control, adaptation_field_control, continuity_counter)
// write buffer to output file.
if pid == 256 || pid == 257 {
continuity_counter = m[pid]
buffer[3] = (buffer[3] & 0xf0) | continuity_counter
m[pid]++
if m[pid] > 15 {
m[pid] = 0
}
}
byte_write, err := output_file.Write(buffer)
if err != nil {
fmt.Println("write output file error: ", byte_write, err)
os.Exit(-4)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment